CSS匹配父元素的第n个子元素

这里使用到nth-child() 选择器
在这里插入图片描述

例一:

父元素的第二个子元素的每个 p 的背景色:

p:nth-child(2)
{
background:#ff0000;
}

例二:

选中第二个 th

<style type = "text/css">
	table tr th:nth-child(2)
</style>

<table>
	<tr>	
		<th>第一个</th>		
		<th>第二个</th>		
		<th>第三个</th>	
	</tr>
</table>

使用:

分别设置summary-li下三个span的样式

.summary-li:not(:first-child) span:nth-child(1) {
    width: 90px;
}

.summary-li:not(:first-child) span:nth-child(2) {
    width: 50px;
    margin-left: 45px;
}

.summary-li:not(:first-child) span:nth-child(3) {
    margin-left: 60px;
}
posted @ 2021-05-28 11:50  →_→BéLieve  阅读(6)  评论(0)    收藏  举报  来源