自学内容网 自学内容网

【CSS】:nth-child和:nth-of-type

:nth-child,先根据条件再查找类名,如下例中,先查找奇数元素再满足div

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    div:nth-child(2n+1) {
      background: #f81;
    }
  </style>

</head>

<body>
  <div>1 - div1</div>
  <span>2 - span1</span>
  <div>3 - div2</div>
  <div>4 - div3</div>
  <span>5 - span2</span>
  <div>6 - div4</div>
</body>

</html>

所以样式如下:
在这里插入图片描述
:nth-of-type,先查找类名再查询符合条件的,如下例中,先查找div再满足是奇数div

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    div:nth-of-type(2n+1) {
      background: #f81;
    }
  </style>

</head>

<body>
  <div>1 - div1</div>
  <span>2 - span1</span>
  <div>3 - div2</div>
  <div>4 - div3</div>
  <span>5 - span2</span>
  <div>6 - div4</div>
</body>

</html>

所以样式如下:
在这里插入图片描述


原文地址:https://blog.csdn.net/owo_ovo/article/details/145144818

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!