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

我只能为第一个ul标签设置CSS属性:

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

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

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:“每个元素,除了第一个”?


当前回答

你可以像下面这样使用:not你可以使用:not()里面的任何选择器

any_CSS_selector:not(any_other_CSS_selector){
    /*YOUR STYLE*/
}

你也可以使用:not without parent selector。

   :not(:nth-child(2)){
        /*YOUR STYLE*/
   }

更多的例子

any_CSS_selector:not(:first-child){
    /*YOUR STYLE*/
}
any_CSS_selector:not(:first-of-type){
    /*YOUR STYLE*/
}
any_CSS_selector:not(:checked){
    /*YOUR STYLE*/
}
any_CSS_selector:not(:last-child){
    /*YOUR STYLE*/
}
any_CSS_selector:not(:last-of-type){
    /*YOUR STYLE*/
}
any_CSS_selector:not(:first-of-type){
    /*YOUR STYLE*/
}
any_CSS_selector:not(:nth-last-of-type(2)){
    /*YOUR STYLE*/
}
any_CSS_selector:not(:nth-last-child(2)){
    /*YOUR STYLE*/
}
any_CSS_selector:not(:nth-child(2)){
    /*YOUR STYLE*/
}

其他回答

li + li {
    background-color: red;
}

正如我使用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;
}

and

div li~li {
    color: red;
}

and

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

and

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

这些只限制ul级别的CSS。假设我们不能将CSS应用于li作为' div ul + ul li'。

对于内部关卡元素,第一个解决方案非常有效。

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

等等……

由于:not不被IE6-8所接受,我建议你这样做:

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

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

参考Chris Coyer的“有用的:第n个孩子的食谱”一文,了解更多关于第n个孩子的例子。

这个CSS2解决方案(“任意ul接另一个ul”)也可以工作,并且得到更多浏览器的支持。

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

与:not和:n -sibling不同,相邻兄弟选择器被IE7+支持。

如果在页面加载后JavaScript更改了这些属性,那么您应该查看IE7和IE8实现中的一些已知错误。请看这个链接。

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

我在上面的一些问题上没有运气,

这是唯一一个对我有效的方法

ul:没有(first-of-type) {}

当我试图让页面上显示的第一个按钮不受左距选项的影响时,这对我来说是有效的。

这是我第一次尝试的选择,但没有成功

ul:没有(第一个孩子)