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


当前回答

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

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

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

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

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

ul:没有(第一个孩子)

其他回答

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

也起作用了

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

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

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

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

你可以使用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)){}

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

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

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

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

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

ul:没有(第一个孩子)

div li~li {
    color: red;
}

支持IE7