简体   繁体   English

选择器li + li麻烦

[英]Selector li + li trouble

I use to use a background image usually on my li + li for a little sliver seperator on my nav menu and I was wanting to try to create a separator without a image (horrible explanation). 我通常在li + li上使用背景图像在导航菜单上使用一些条子分离器,而我想尝试创建一个没有图像的分离器(可怕的解释)。

I previously was using 我以前在用

#header li + li {
    background: url('http://i.imgur.com/IdVT0cL.png') 8px 8px / 1px 10px no-repeat;
    padding-left: 20px;
}

So I tried 所以我尝试了

#header li + li {
    background: #00B25C 8px 8px / 1px 10px no-repeat;
    padding-left: 20px;
}

And it's highlighting the all the list items except the first one. 并且突出显示除第一个项目外的所有列表项目。 Why is this? 为什么是这样? Here's my JSFiddle: http://jsfiddle.net/yhLLb/ 这是我的JSFiddle: http : //jsfiddle.net/yhLLb/

If I got you right, you could use css' :before-selector to achieve a separator without using an image: 如果我说对了,您可以使用css':before-selector来实现分隔符,而无需使用图像:

#navBar li+li:before {
    content: "|";
    /*background: #00B25C 8px 8px / 1px 10px no-repeat;*/
}

See this fiddle: http://jsfiddle.net/yhLLb/2/ 看到这个小提琴: http : //jsfiddle.net/yhLLb/2/

Cheers Florian 干杯弗洛里安

The answer to your question, why is it highlighting all the list items except the first one, can be explained by the spec for the CSS adjacent selector. 您的问题的答案为何为何突出显示除第一个项目以外的所有列表项,可以通过CSS相邻选择器的规范来解释。 http://www.w3.org/TR/CSS2/selector.html#adjacent-selectors http://www.w3.org/TR/CSS2/selector.html#adjacent-selectors

Thus, the following rule states that when a P element immediately follows a MATH element, it should not be indented: 因此,以下规则指出,当P元素紧随MATH元素时,不应缩进:

math + p { text-indent: 0 } 数学+ p {文本缩进:0}

This fiddle highlights all of the menu items, not sure if this is what you wanted to achieve though, as #navBar li would suffice, there is no need to select the adjacent element? 这个小提琴突出显示了所有菜单项,但不确定是否是您想要实现的,因为#navBar li就足够了,是否无需选择相邻元素? http://jsfiddle.net/yhLLb/1/ http://jsfiddle.net/yhLLb/1/

ie

#navBar li, #navBar li + li {
    background: #00B25C 8px 8px / 1px 10px no-repeat;
}

is the same as 是相同的

#navBar li {
    background: #00B25C 8px 8px / 1px 10px no-repeat;
}

Here is another way that you could do it too. 这是您也可以执行的另一种方法。

html html

    <div id="navBar">
    <ul>
        <li>Home</li>
        <li>Portfolio</li>
        <li>Contact</li>
        <li class="last">About</li>
    </ul>
    </div>

Css 的CSS

#navBar {
    margin-right: 20px;
    background: #3840F8;
    float: right;
    line-height: 40px;
    overflow: hidden;
    height: 40px;
}
#navBar ul {
    padding-left: 20px;
    padding-right: 20px;
}
#navBar li {
    text-align: center;
    border-right: 1px solid #888;
    padding: 0 10px;
    line-height: 10px;
    float: left;
    list-style: none;
    color: #fff;
}
#navBar li.last {
     border-right: none;
}

JSFiddle JSFiddle

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

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