关于nth-child的疑惑

本文通过实例解析了 CSS 中的 :nth-child() 选择器的工作原理,特别是其如何应用于不同层级的元素,以及如何正确指定公式来选择目标元素。

正好很久没写代码了,也想试试回答下这个问题,于是就搜索了下,于是就找到了 W3School的CSS3 :nth-child() 选择器,看到了这样一个代码:

p:nth-child(-n+3){
	background:#ff0000;
}
class="editr__result active" name="editr_09a81fc" src="http://blog.cssforest.org/demo/%E5%85%B3%E4%BA%8Enth-child%E7%9A%84%E7%96%91%E6%83%91/index.html" style="border-width: 1px 1px 0px; border-style: solid; border-color: rgb(204, 204, 204); padding: 0px; top: 0px; width: 478.797px; height: 448.875px; box-sizing: border-box; position: absolute !important; display: block; left: 319.188px; border-radius: 3px; margin-bottom: 5px; max-width: 100%; background: rgb(39, 40, 34);">
1
2
3
4
5
     < h1 > </ h1 >
     < p > </ p >
     < p > </ p >
     < p > </ p >
     < p > </ p >
1
2
3
4
5
6
7
/* @reset */
body { background-color: #FFFFFF; }
/* ==== demo ==== */
p :nth-child( -n+ 3) {
     background: #ff0000;
}
1

感觉有点奇怪,我原本以为应该会是前三个段落被选中,像这样:

class="editr__result active" name="editr_4389dc4" src="http://blog.cssforest.org/demo/%E5%85%B3%E4%BA%8Enth-child%E7%9A%84%E7%96%91%E6%83%91/index.html" style="border-width: 1px 1px 0px; border-style: solid; border-color: rgb(204, 204, 204); padding: 0px; top: 0px; width: 798px; height: 448.875px; box-sizing: border-box; position: absolute !important; display: block; border-radius: 3px; margin-bottom: 5px; max-width: 100%; background: rgb(39, 40, 34);">

一定是哪里不对了。来看看它的说明:

:nth-child(n) 选择器匹配属于其父元素的第 N 个子元素,不论元素的类型。 n 可以是数字、关键词或公式。

MSN文档对:nth-child的说明

伪类:nth-clild(an+b)匹配在文档树中前面有an+b-1个兄弟元素的元素,此时n大于或等于0,并且该元素具有父元素。简而言之,该选择器匹配多个位置满足an+b的子元素。

span:nth-child(-n+3) 匹配前三个子元素中的span元素。

注意到一个特点——“具有父元素”,于是我们给这个例子中的<p>加个父元素试试:

class="editr__result active" name="editr_ff56209" src="http://blog.cssforest.org/demo/%E5%85%B3%E4%BA%8Enth-child%E7%9A%84%E7%96%91%E6%83%91/index.html" style="border-width: 1px 1px 0px; border-style: solid; border-color: rgb(204, 204, 204); padding: 0px; top: 0px; width: 478.797px; height: 448.875px; box-sizing: border-box; position: absolute !important; display: block; left: 319.188px; border-radius: 3px; margin-bottom: 5px; max-width: 100%; background: rgb(39, 40, 34);">
1
2
3
4
5
6
7
     < h1 > </ h1 >
     < div >
         < p > </ p >
         < p > </ p >
         < p > </ p >
         < p > </ p >
     </ div >
1
2
3
4
5
6
7
/* @reset */
body { background-color: #FFFFFF; }
/* ==== demo ==== */
p :nth-child( -n+ 3) {
     background: #ff0000;
}
1

element:nth-child(an + b) { /*规则*/ }

成功。也就是说nth-child从最大的父元素”body”开始,匹配“an+b”个元素,如果里面包含”element”,则对其应用样式规则。

class="editr__result active" name="editr_3bc4dac" src="http://blog.cssforest.org/demo/%E5%85%B3%E4%BA%8Enth-child%E7%9A%84%E7%96%91%E6%83%91/index.html" style="border-width: 1px 1px 0px; border-style: solid; border-color: rgb(204, 204, 204); padding: 0px; top: 0px; width: 478.797px; height: 448.875px; box-sizing: border-box; position: absolute !important; display: block; left: 319.188px; border-radius: 3px; margin-bottom: 5px; max-width: 100%; background: rgb(39, 40, 34);">
1
2
3
4
5
6
7
8
9
10
11
     < h1 > </ h1 >
     < div >
         < p > </ p >
         < p > </ p >
         < p > </ p >
         < p > </ p >
     </ div >
     < p > </ p >
     < p > </ p >
     < p > </ p >
     < p > </ p >
1
2
3
4
5
6
7
/* @reset */
body { background-color: #FFFFFF; }
/* ==== demo ==== */
p :nth-child( -n+ 3) {
     background: #ff0000;
}
1

以上,如果你想更可控的应用到“element”元素上,可以试试:

p:nth-of-type(-n+3){
	background:#ff0000;
}
class="editr__result active" name="editr_f9834b8" src="http://blog.cssforest.org/demo/%E5%85%B3%E4%BA%8Enth-child%E7%9A%84%E7%96%91%E6%83%91/index.html" style="border-width: 1px 1px 0px; border-style: solid; border-color: rgb(204, 204, 204); padding: 0px; top: 0px; width: 478.797px; height: 448.875px; box-sizing: border-box; position: absolute !important; display: block; left: 319.188px; border-radius: 3px; margin-bottom: 5px; max-width: 100%; background: rgb(39, 40, 34);">
1
2
3
4
5
6
7
8
9
10
11
     < h1 > </ h1 >
     < div >
         < p > </ p >
         < p > </ p >
         < p > </ p >
         < p > </ p >
     </ div >
     < p > </ p >
     < p > </ p >
     < p > </ p >
     < p > </ p >
1
2
3
4
5
6
7
8
9
10
/* @reset */
body  {
     background-color#FFFFFF;
}
/* ==== demo ==== */
p :nth-of-type( -n+ 3{
     background#ff0000;
}
1

最后,在Useful :nth-child Recipes中的例子由于都是使用li,所以很容易就忽略了上面出现的问题,也是提个醒吧。

### CSS 列表项的选择方法 在前端开发中,CSS 提供了一系列伪类选择器来帮助开发者精确控制 HTML 元素的样式。以下是关于 `first-child`、`last-child`、`nth-child` 等伪类的具体应用方式。 #### 使用 `:first-child` 和 `:last-child` `:first-child` 用于匹配父元素中的第一个子元素[^1]。例如: ```css li:first-child { color: red; } ``` 上述代码会将 `<ul>` 或 `<ol>` 中的第一个 `<li>` 的文字颜色设置为红色。 同样地,`:last-child` 可以用来匹配最后一个子元素。例如: ```css li:last-child { font-weight: bold; } ``` 这段代码会让列表的最后一项加粗显示。 --- #### 使用 `:nth-child()` `:nth-child()` 是一种更强大的选择器,可以基于索引来选择特定的子元素[^2]。它的语法如下: ```css :nth-child(an+b) ``` 其中: - `a` 表示周期间隔, - `b` 表示起始位置。 ##### 常见用法实例 1. **选择第四个子元素** 如果想选中列表中的第四项,可以直接写成: ```css li:nth-child(4) { background-color: yellow; } ``` 2. **选择偶数项 (`even`)** 要选择所有偶数编号的列表项,可以用以下代码: ```css li:nth-child(even) { text-decoration: underline; } ``` 3. **选择奇数项 (`odd`)** 同样地,对于奇数编号的列表项,可以选择它们并施加样式: ```css li:nth-child(odd) { border: 1px solid black; } ``` 4. **按固定模式选择(如每四次循环一次)** 若要每隔三个项目之后再选中下一个项目,则可使用表达式 `4n+1` 来表示这种规律性的选取行为: ```css li:nth-child(4n+1) { color: blue; } ``` --- #### 特定数值的选择 如果需要针对某个具体序号进行操作,比如只改变第五个 `<li>` 的外观,那么只需要简单指定即可: ```css li:nth-child(5){ font-size: larger; } ``` 以上就是利用 CSS 对于不同类型的列表项实施个性化定制的一些基本技巧。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值