css 关于 nth

 

nth

 

 
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>选择器区别演示</title>
<style>
.container {
padding: 20px;
background: #f3f3f3;
}

/* nth-child 选择器 */
.container :nth-child(2) {
color: red;
/* 会选中第2个子元素,不管是什么标签 */
}

/* nth-of-type 选择器 */
.container p:nth-of-type(2) {
color: blue;
/* 只会选中第2个p标签 */
}
</style>
</head>
<body>
<div class="container">
<p>第1个p标签</p>
<h3>这是一个h3标签</h3>
<p>第2个p标签</p>
<p>第3个p标签</p>
</div>
</body>
</html>

  

 

posted @ 2025-04-10 11:34  十三山入秋  阅读(6)  评论(0)    收藏  举报