CSS ::before ::after 类的伪元素不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24295822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 02:35:31  来源:igfitidea点击:

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

csspseudo-element

提问by dub stylee

I am trying to add a ::beforeand ::afterpseudo-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 backgroundproperty is set, but the ::beforeand ::afterproperties are not.

我正在尝试向菜单标题添加一个::before::after伪元素。伪元素适用于菜单外的常规链接。但是,当我尝试将它们应用于菜单项时,background属性已设置,但::before::after属性未设置。

Here is the relevant CSS:

这是相关的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:

以及相关的 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

JSFiddle

I have also tried to reference the menuheaderclass items by #cssmenu ul li ainstead, and that actually works (sort of). Instead of the ::beforeand ::afterimages showing next to the menu item itself, they show next to the first item in the drop-down menu.

我也尝试通过引用menuheader类项#cssmenu ul li a,这实际上有效(有点)。它们不是显示在菜单项本身旁边的::before::after图像,而是显示在下拉菜单中的第一项旁边。

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

我在这里阅读了有关伪元素的其他问题,以及http://www.w3schools.com/css/css_pseudo_elements.asp,据我所知,::before::after应该能够应用于<a>元素。这可能是一些简单的问题,但我只是没有在这里看到这个问题。

回答by Jasper

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

如果你要绝对定位伪元素,你需要给它们的父元素一个position值,与非伪元素相同。

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/

这是您的 JSFiddle 的更新版本(唯一的变化是添加上面的代码并使伪元素的背景成为演示的纯色):http: //jsfiddle.net/99Aq8/1/