简体   繁体   English

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

[英]CSS Pseudo-elements :before & :after work differently in Internet Explorer

I have a very specific problem as I am trying to create a navigation that has angles using purely CSS without images or javascript. 我有一个非常具体的问题,因为我正在尝试使用纯CSS而不使用图像或javascript来创建具有角度的导航。 I have figured it out so that it works perfectly but the problem I am coming across is that IE 9 and Chrome look different. 我已经弄清楚了它是完美的,但我遇到的问题是IE 9和Chrome看起来不同。 I can get both to work by changing the margins of the pseudo elements but I would prefer a single solution to work in both. 我可以通过改变伪元素的边距来使两者都工作,但我更愿意使用单个解决方案来兼顾两者。 If anyone can figure out why the margins are different and give me a single CSS solution to work in both browsers that would be great. 如果有人能够弄清楚为什么边距不同,并给我一个单独的CSS解决方案,在两个浏览器中工作都会很棒。 Otherwise I will have to add a seperate class for IE and force it to work using a seperate CSS entry. 否则,我将不得不为IE添加一个单独的类,并使用单独的CSS条目强制它工作。

Here is the jsfiddle to work with arrow-nav 这是使用arrow-nav的jsfiddle

Here is the HTML 这是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>

The CSS 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;
}

You can fix this by using absolute positioning on the pseudo elements. 您可以通过在伪元素上使用绝对定位来解决此问题。 To make this work correctly, set the position of ul li to relative (which will cause elements positioned absolutely within it to be relative to the li). 为了使其正常工作,请将ul li的位置设置为relative(这将导致绝对位于其中的元素相对于li)。 Then, update the position of the pseudo elements to use left instead of margin-left : 然后,更新伪元素的位置以使用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
}

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

You need to specify the position for the elements (for example top and left values). 您需要指定元素的位置(例如, topleft值)。 And for some reason, which I don't fully understand - to add position: relative to the li (not the ul ): 由于某种原因,我不完全理解 - 添加position: relative对于li (不是ul ):

http://jsfiddle.net/jSdWR/4/ http://jsfiddle.net/jSdWR/4/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM