不是:第一子选择(not:first-child selector)

2019-06-18 01:23发布

我有一个div包含几个标签ul标签。

我能为第一组CSS属性ul仅标签:

div ul:first-child {
    background-color: #900;
}

然而,我以下尝试为对方设置的CSS属性ul除了第一个不工作的标签:

div ul:not:first-child {
    background-color: #900;
}

div ul:not(:first-child) {
    background-color: #900;
}

div ul:first-child:after {
    background-color: #900;
}

我怎么能写在CSS:“每一个元素,除第一个”?

Answer 1:

一个您发布的版本的实际工作为所有现代浏览器(如CSS选择3级的支持 ):

div ul:not(:first-child) {
    background-color: #900;
}

如果您需要支持旧的浏览器,或者如果你是受阻:not选择的限制 (只接受一个简单的选择作为参数),那么你可以使用另一种方法:

定义具有更大的范围比你想要什么,然后规则“撤销”它有条件,限制了它的范围,你打算做什么:

div ul {
    background-color: #900;  /* applies to every ul */
}

div ul:first-child {
    background-color: transparent; /* limits the scope of the previous rule */
}

当限制范围使用默认值,您所设置的每个CSS属性。



Answer 2:

这CSS2解决方案(“任何ul另一个之后ul ”)的作品,也和被更多的浏览器支持。

div ul + ul {
  background-color: #900;
}

不像:not:nth-sibling中, 相邻的兄弟选择由IE7 +支持。

如果你的JavaScript在页面加载后改变这些特性,你应该看看在这个IE7IE8实现一些已知的bug。 看到此链接 。

对于任何静态网页,这应该很好地工作。



Answer 3:

因为:not不被接受IE6-8,我建议你:

div ul:nth-child(n+2) {
    background-color: #900;
}

所以,你选择的每一个ul在它的父元素除了第一个

请参阅克里斯Coyer的“有用的:第n个孩子食谱”文章更nth-child 的例子 。



Answer 4:

div li~li {
    color: red;
}

Supports IE7



Answer 5:

not(:first-child)似乎不工作了。 至少与较新版本的Chrome和Firefox的。

相反,试试这个:

ul:not(:first-of-type) {}


Answer 6:

我没有运气上面的一些,

这只是实际工作对我来说是一个

ul:not(:first-of-type) {}

这为我工作,当我试图在页面不是由利润率左选项来实现显示的第一个按钮。

这是我第一次尝试的选项,但它没有工作

ul:not(:first-child)



Answer 7:

你可以使用任何选择与not

p:not(:first-child){}
p:not(:first-of-type){}
p:not(:checked){}
p:not(:last-child){}
p:not(:last-of-type){}
p:not(:first-of-type){}
p:not(:nth-last-of-type(2)){}
p:not(nth-last-child(2)){}
p:not(:nth-child(2)){}


Answer 8:

为我所用ul:not(:first-child)是一个完美的解决方案。

div ul:not(:first-child) {
    background-color: #900;
}

这是为什么呢,因为用一个完美的ul:not(:first-child) ,我们可以在内部元素应用CSS。 像li, img, span, a标签等。

但使用其他解决方案时:

div ul + ul {
  background-color: #900;
}

div li~li {
    color: red;
}

ul:not(:first-of-type) {}

div ul:nth-child(n+2) {
    background-color: #900;
}

这些限制只微升水平CSS。 假设我们不能在应用CSS li为'DIV + UL UL礼”。

对于内部级元素的第一个解决方案完美的作品。

div ul:not(:first-child) li{
        background-color: #900;
    }

等等 ...



文章来源: not:first-child selector