css选择第一个子元素或者非第一个子元素有哪些方法实现

如果我们要获取到ul标签下面的第一个li标签来设置样式,有哪些css方法来实现呢?

一 、获取第一个元素

1. 使用 :first-child

ul li:first-child {
 ......
}

2. 使用 :first-of-type

ul li:first-of-type {
 ......
}

3. 使用 :nth-child
ul li:nth-child(1){
......
}

4. 使用 :nth-of-type
ul li:nth-of-type(1){
......
}

二、 获取非第一个元素

1. li:not(:first-child) //或者 :li:not(:first-of-type)

2. li+li //相邻选择器

3. li ~ li  兄弟选择器
4. li:nth-child(n+2) //由于n是从0开始的

版权声明:本文为qq_35695023原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。