CSS ::before ::after pseudo-element of class not working

dub stylee

I am trying to add a ::before and ::after pseudo-element to a menu heading. The pseudo-elements work fine for a regular link outside of the menu. However, when I am trying to apply them to a menu item, the background property is set, but the ::before and ::after properties are not.

Here is the relevant CSS:

#cssmenu {
    background-color: #FFFFCC;
    clear: both;
    display: inline;
    float: left;
    list-style: none;
    overflow: hidden;
    text-align: center;
    text-transform: uppercase;
    width: 100%;
}
#cssmenu li {
    display: inline-block;
}
#cssmenu li a {
    display: inline-block;
}
#cssmenu li ul {
    /*margin-top: 0px; submenu location relative to bottom of parent */
    display: none;
}
#cssmenu li:hover ul {
    display: block;
    position: absolute;
}
#cssmenu li ul li a {
    width: 200px;
}
#cssmenu li:hover > a {
    /* shadow around parent when hover */
    box-shadow: 5px 5px 25px #000;
    -moz-box-shadow: 5px 5px 25px #000;
    -webkit-box-shadow: 5px 5px 25px #000;
}
#cssmenu li a {
    color: #000;
    font-weight: bold;
    text-decoration: none;
    padding: 0 12px;
    line-height: 24px;
}
#cssmenu li a:hover {
    background-color: #99CC99;
    /*padding: 12px;  link background when hover over link */
}
#cssmenu li ul {
    background: rgba(255,255,255,0.9);  child menu background w/ transparency 
    padding: 10px 5px;

    box-shadow: 5px 5px 25px #BBB;
    -moz-box-shadow: 5px 5px 25px #BBB;
    -webkit-box-shadow: 5px 5px 25px #BBB;

    border-radius: 0px 15px 15px 15px;
    -moz-border-radius: 0px 15px 15px 15px;
    -webkit-border-radius: 0px 5px 5px 5px;
}
/* display sub-menu as list */
#cssmenu li ul li {
    display: block;
    text-align: left;
}
#cssmenu li ul li a, #nav li ul li a:hover {
    background: transparent;
    color: #000;
    width: 180px;
    font-size: 0.95em;
    font-weight: normal;
    padding: 6px;
}
#cssmenu li ul li a:hover {
    /*text-decoration: underline;*/
    box-shadow: none;
    -moz-box-shadow: none;
    -webkit-box-shadow: none;
    border-radius: 0;
    -moz-border-radius: 0;
    -webkit-border-radius: 0;
}
.menuheader {
    /* allow for the pseudo-elements which do not have layout due to absolute positioning */
    margin: 12px 15px;
    border: 0;
    background: url("../../../images/buttonslice24.png") 24px 0 repeat-x ;
}
.menuheader::before,
.menuheader::after {
    content: ' ';
    position: absolute;
    top: 0;
    width: 12px;
    height: 24px;
}
.menuheader::before {
    left: -12px;
    background: url("../../../images/buttonleft24.png") 0 0;
    no-repeat;
}
.menuheader::after {
    right: -12px;
    background: url("../../../images/buttonright24.png") 100% 0;
    no-repeat;
}

And the relevant HTML:

<div id="cssmenu">
  <ul>
    <li><a class="menuheader" href="../../../contact-us.aspx">Contact</a></li>
    <li><a class="menuheader" href="../../../">Home</a></li>
    <li><a class="menuheader">Store</a>
      <ul>
        <li><a href="../../../shipping-policy.aspx">Shipping &amp; Return Policy</a></li>
      </ul>
    </li>
    <li><a class="menuheader" href="../../../account.aspx">Account</a></li>
    <li><a class="menuheader" href="../../../cart.aspx">View Cart</a></li>
  </ul>
</div>

JSFiddle

I have also tried to reference the menuheader class items by #cssmenu ul li a instead, and that actually works (sort of). Instead of the ::before and ::after images showing next to the menu item itself, they show next to the first item in the drop-down menu.

I have read other questions on here regarding pseudo-elements, as well as http://www.w3schools.com/css/css_pseudo_elements.asp and as far as I can tell, the ::before and ::after should be able to be applied to an <a> element. This may be some simple issue, but I am just not seeing the issue here.

Jasper

If you're going to absolutely position the pseudo elements, you need to give their parent a position value, same as with non-pseudo elements.

Just add:

.menuheader {
    position : relative;
}

Here is an updated version of your JSFiddle (the only changes are adding the above code and making the background of the pseudo elements a solid color for demonstration): http://jsfiddle.net/99Aq8/1/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

CSS ::before ::after pseudo-element of class not working

From Dev

::before/::after pseudo class not working on video element

From Dev

CSS pseudo element ::before is not working properly

From Dev

Why ::before pseudo-element not working with :visited pseudo-class?

From Dev

CSS ::after pseudo element clear not working

From Dev

CSS pseudo element not working

From Dev

CSS before and after pseudo elements not working in Edge and IE 11

From Dev

Issue with Pseudo Class Before and After

From Dev

:after pseudo element not working in Safari

From Dev

Firefox ::after pseudo element not working

From Dev

after pseudo element not working in webview

From Dev

:after pseudo element not working in Safari

From Dev

CSS pseudo-class :required does not work together with pseudo-element ::before

From Dev

CSS a:active pseudo class not Working

From Dev

How to set :disabled pseudo element/class on :before pseudo element

From Dev

Why is my ::before pseudo-element not working?

From Dev

CSS selector for :before pseudo-element when there is one class but NOT another class

From Dev

CSS selector for :before pseudo-element when there is one class but NOT another class

From Dev

Select text in ::before or ::after pseudo-element

From Dev

Is there a third pseudo element, like after and before

From Dev

Pseudo-element :after is adding before instead

From Dev

Fixing before pseudo element after clicking

From Dev

CSS, using display:table with before pseudo element

From Dev

CSS Transition on the 'content' property of :before pseudo element

From Dev

Creating a shape using a :before pseudo element in CSS

From Dev

CSS Transition on the 'content' property of :before pseudo element

From Dev

Responsive CSS transform for before pseudo-element

From Dev

CSS :before and :after not working?

From Dev

Do not style pseudo-class :before/:after

Related Related

  1. 1

    CSS ::before ::after pseudo-element of class not working

  2. 2

    ::before/::after pseudo class not working on video element

  3. 3

    CSS pseudo element ::before is not working properly

  4. 4

    Why ::before pseudo-element not working with :visited pseudo-class?

  5. 5

    CSS ::after pseudo element clear not working

  6. 6

    CSS pseudo element not working

  7. 7

    CSS before and after pseudo elements not working in Edge and IE 11

  8. 8

    Issue with Pseudo Class Before and After

  9. 9

    :after pseudo element not working in Safari

  10. 10

    Firefox ::after pseudo element not working

  11. 11

    after pseudo element not working in webview

  12. 12

    :after pseudo element not working in Safari

  13. 13

    CSS pseudo-class :required does not work together with pseudo-element ::before

  14. 14

    CSS a:active pseudo class not Working

  15. 15

    How to set :disabled pseudo element/class on :before pseudo element

  16. 16

    Why is my ::before pseudo-element not working?

  17. 17

    CSS selector for :before pseudo-element when there is one class but NOT another class

  18. 18

    CSS selector for :before pseudo-element when there is one class but NOT another class

  19. 19

    Select text in ::before or ::after pseudo-element

  20. 20

    Is there a third pseudo element, like after and before

  21. 21

    Pseudo-element :after is adding before instead

  22. 22

    Fixing before pseudo element after clicking

  23. 23

    CSS, using display:table with before pseudo element

  24. 24

    CSS Transition on the 'content' property of :before pseudo element

  25. 25

    Creating a shape using a :before pseudo element in CSS

  26. 26

    CSS Transition on the 'content' property of :before pseudo element

  27. 27

    Responsive CSS transform for before pseudo-element

  28. 28

    CSS :before and :after not working?

  29. 29

    Do not style pseudo-class :before/:after

HotTag

Archive