CSS伪元素:在&之后:在Internet Explorer中以不同的方式工作

Rud*_*cia 6 html css internet-explorer pseudo-element

我有一个非常具体的问题,因为我正在尝试使用纯CSS而不使用图像或javascript来创建具有角度的导航.我已经弄清楚了它是完美的,但我遇到的问题是IE 9和Chrome看起来不同.我可以通过改变伪元素的边距来使两者都工作,但我更愿意使用单个解决方案来兼顾两者.如果有人能够弄清楚为什么边距不同,并给我一个单独的CSS解决方案,在两个浏览器中工作都会很棒.否则,我将不得不为IE添加一个单独的类,并使用单独的CSS条目强制它工作.

这是使用arrow-nav的jsfiddle

这是HTML

<ul>
    <li><a href="#" >Some Navigation</a></li>
    <li><a href="#">More navigation</a></li>
    <li><a href="#">Another Nav</a></li>
    <li><a href="#">Test Nav</a></li>
    <li><a href="#">A Test Navigation</a></li>
</ul>
Run Code Online (Sandbox Code Playgroud)

CSS

ul {
    float:right;
    list-style-type:none;
    margin:0;
    padding:0;
    width: 300px;
}
ul li a {
    float:left;
    width:300px;
}
ul li{
    float:left;
    width: 300px;
    height:50px;
    line-height:50px;
    text-indent:10%;
    background: grey;
    margin-bottom:10px;
    border:1px solid black;
}
ul li:before {
    content:"";
    position:absolute;
    margin-top:-1px;
    border-top: 26px solid transparent;
    border-bottom: 26px solid transparent;
    border-right: 21px solid black;
    margin-left:-22px;
}
ul li:after {
    content:"";
    position:absolute;
    border-top: 25px solid transparent;
    border-bottom: 25px solid transparent;
    border-right: 20px solid grey;
    margin-left:-320px;
}
Run Code Online (Sandbox Code Playgroud)

rya*_*ill 8

您可以通过在伪元素上使用绝对定位来解决此问题.要使其正常工作,请将ul li相对位置设置为相对位置(这将导致绝对位于其中的元素相对于li).然后,更新要使用的伪元素的位置,left而不是margin-left:

ul li{
    position: relative; // Add this.
    float:left;
    width: 300px;
    height:50px;
    line-height:50px;
    text-indent:10%;
    background: grey;
    margin-bottom:10px;
    border:1px solid black;
}
ul li:before {
    content:"";
    position:absolute;
    margin-top:-1px;
    border-top: 26px solid transparent;
    border-bottom: 26px solid transparent;
    border-right: 21px solid black;
    left:-22px; // Update from margin-left to left
}
ul li:after {
    content:"";
    position:absolute;
    border-top: 25px solid transparent;
    border-bottom: 25px solid transparent;
    border-right: 20px solid grey;
    left:-20px; // Update from margin-left to left
}
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/ryanbrill/jSdWR/5/

  • 奇怪的是,我已经使用CSS很长一段时间了,并且从未意识到给父元素位置相对允许子元素的绝对定位与父元素而不是文档一起工作.我总是避免绝对定位,因为它只使用文档.这正是我想要的,并且更新我当前的代码很简单.谢谢! (4认同)