nth-of-type 选择器只能用在标签后面
nth-child 可以用在类名后面
demo
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.container .tt:nth-child(5) {
/* 这是p4变红 */
color: red;
}
.container p.tt:nth-child(5) {
/* 这是p4变红 */
color: red;
}
.container p:nth-child(5) {
/* 这是p4变红 */
color: red;
}
.container p:nth-of-type(2) {
/* 这是p2变蓝 */
color: blue;
}
.container p.tt:nth-of-type(2) {
/* 无反应 */
color: blue;
}
.container .tt:nth-of-type(2) {
/* 无反应 */
color: blue;
}
</style>
</head>
<!--
nth-of-type 选择器只能用在标签后面
nth-child 可以用在类名后面
-->
<body>
<div class="container">
<span>这是span</span>
<p>这是p1</p>
<p>这是p2</p>
<p class="tt">这是p3</p>
<p class="tt">这是p4</p>
<p>这是p5</p>
</div>
</body>
</html>