A detail of nth-child and nth-of-type

A detail of nth-child and nth-of-type

The nth-of-type selector can only be used after tags

nth-child can be used after the class name

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>

Guess you like

Origin blog.csdn.net/mochenangel/article/details/118539087