:nth-child() 选择器,问题

问题1,
可能存在的顺序问题

<!DOCTYPE html>
<html>

<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
	<style type="text/css">
		.out .other {
			background-color: red;
		}



		.out .inner:nth-child(2) {
			background-color: green;
		}

		.out .inner:first-child {
			background-color: blue;
		}

		.out .inner:nth-child(3) {
			background-color: yellow;
		}
	</style>
	<title>nth-child</title>
</head>

<body>

	<div class="out">
		<!-- <p> -->
		<div class="other">
			我是随便的一个div
		</div>
		<div class="inner">
			我是内层第一个div
		</div>
		<div class="inner">
			我是内层第二个div
		</div>
		<!-- </p> -->
	</div>

	<script src="./js/jquery-3.4.1.js" defer></script>
	<script type="text/javascript"></script>
</body>

</html>

图1
正常逻辑来想,.out里面的第一个inner在样式中设置得.out .inner:first-child 是background-color:blue,单实际情况,取的是.out .inner:nth-child(2)中的样式

给我的感觉更像是用的nth-of-type方法在处理

简单来说,我是这么理解的,因为.inner,上,有一个.other的div,那么在用nth-child做筛选的时候,先判断了,标签的名称(目前案例中都是div),在从.out中进行遍历div,所以,我们以为的第一个.inner,实际中.out的第二个div,则.out .inner:nth-child(2)生效;又因为first-child的class是other不是inner,不匹配,所以.out .inner:first-child,不生效

如果换成.out .other:first-child {background-color: blue;},则样式会生效,显示在.other上

问题2:
可能存在的嵌套问题

<!DOCTYPE html>
<html>

<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
	<style type="text/css">
		.out .other {
			background-color: red;
		}

		.out .inner:nth-child(2) {
			background-color: green;
		}

		.out .inner:first-child {
			background-color: blue;
		}

		.out .inner:nth-child(3) {
			background-color: yellow;
		}
	</style>
	<title>nth-child</title>
</head>

<body>

	<div class="out">
		<p>
			<div class="other">
				我是随便的一个div
			</div>
			<div class="inner">
				我是内层第一个div
			</div>
			<div class="inner">
				我是内层第二个div
			</div>
		</p>
	</div>

	<script src="./js/jquery-3.4.1.js" defer></script>
	<script type="text/javascript"></script>
</body>

</html>

这段代码与第一段,代码的区别就是,在内层3个div外,套了一层p,效果图如下
图2

结果是,我发现了内层第二个div,采用的样式是.out .inner:nth-child(3) 中的样式,我是这么理解的,遍历取样式的时候,外层的p标签算了一层,所以index(序号)就+1了

如果将css样式换成

		.out .other {
			background-color: red;
		}

		.out .inner:nth-of-type(2) {
			background-color: green;
		}

		.out .inner:first-child {
			background-color: blue;
		}

		.out .inner:nth-of-type(3) {
			background-color: yellow;
		}

则会显示如下
图3
简单来说,就是:nth-of-type(),用标签类型来判断,而不是dom的顺序了

<think>好的,我现在需要详细解释CSS中的:nth-child伪类选择器的使用方法,并提供示例。首先,我得回忆一下相关知识,并参考提供的引用内容,确保信息准确。 首先,根据引用[1]和引用[4],:nth-child(n)属于结构伪类选择器,根据元素在父元素中的位置选择。需要明确它的语法和参数n的不同情况。比如n可以是数字、关键字(如odd、even)或者公式(如2n+1)。同时,引用[3]和引用[5]提到,:nth-child在选择时先确定位置,再匹配元素类型,而:nth-of-type则是先确定类型再计算位置,这点需要区分清楚。 接下来,我需要组织内容结构。先介绍基本语法,然后分情况讨论n的不同取值,比如数字、关键字、公式,每种情况都给出示例。同时,要提到注意事项,比如元素类型的影响,以及与其他选择器的区别,如引用[5]中的对比。 可能需要检查每个示例是否正确,比如公式的正确写法,比如2n+1代表奇数,而2n是偶数。同时,注意在代码块中使用正确的CSS语法,比如li:nth-child(3) { ... }。 还要确保回答符合用户的要求,包括使用方法、示例,并且自然地引用相关引用内容。例如,在解释公式参数时,可以引用[3]中的示例,或者在讨论元素类型影响时引用[5]。 最后,生成相关问题,可能需要围绕其他伪类选择器、常见问题或实际应用来提问。比如如何选择偶数元素,或者与:nth-of-type的区别,这些都是用户可能感兴趣的延伸问题。 需要避免错误,比如混淆:nth-child:nth-of-type,确保每个示例的准确性,并正确应用引用标注。同时,保持语言简洁明了,结构清晰,符合中文表达习惯。</think>### CSS :nth-child伪类选择器详解 #### 基本概念 `:nth-child(n)` 是CSS结构伪类选择器,根据元素在父容器中**同级元素的位置**进行匹配,先确定位置再匹配类型[^5]。其语法为: $$选择器:nth-child(公式/关键字)$$ #### 参数`n`的3种形式 1. **数字** 直接指定具体位置(从1开始计数)。 ```css /* 选中第3个子元素 */ li:nth-child(3) { color: red; } ``` 2. **关键字** - `odd`:奇数位置 - `even`:偶数位置 ```css /* 表格奇数行背景色 */ tr:nth-child(odd) { background: #f0f0f0; } /* 等价于2n+1 */[^3] ``` 3. **公式** 使用`An+B`格式(A为周期,B为偏移量,n从0开始): ```css /* 选中第4、7、10...个子元素 */ div:nth-child(3n+4) { border: 1px solid blue; } ``` #### 典型应用场景 1. 斑马纹表格 ```css tr:nth-child(2n) { background: #f8f8f8; } /* 偶数行 */ ``` 2. 间隔列表项 ```css li:nth-child(4n+1) { margin-right: 0; } /* 每行第1个元素取消右侧间距 */ ``` 3. 首屏特殊样式 ```css /* 第5个之后的所有元素 */ .card:nth-child(n+5) { opacity: 0.8; } ``` #### 注意事项 1. **元素类型影响匹配** 若父容器中不同元素混合存在,`:nth-child`会先按位置选择,再校验元素类型是否匹配。例如: ```html <div class="container"> <p>段落1</p> <span>文本</span> <p>段落2</p> </div> ``` ```css p:nth-child(2) { /* 不会生效!因为第二个元素是<span> */ color: blue; } ``` 2. **与:nth-of-type区别** `:nth-of-type`先筛选元素类型再计算位置,如: ```css p:nth-of-type(2) { /* 选中第二个<p>元素 */} ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值