nth-child(n)和nth-of-type(n)

: Nth-Child (n) : sub-elements parent element of the n-th

: Nth-of-type (n) : parent element certain sub-elements of the n-th

Note: n is from 0 onwards, the element index from 1 onwards

nth-child(n)和nth-of-type(n)例:

<head>
  <style>
  	body :nth-child(2) {
      height: 30px;
      background: blue;
      color: #fff;
  	}
  </style>
</head>
<body>
  <p>1 - p1</p>
  <div>2 - div1</div>
  <p>3 - p2</p>
  <div>4 - div2</div>
</body>

Here Insert Picture Description

Alternatively: nth-child as: nth-of-type

<style>
	body :nth-of-type(2) {
      height: 30px;
      background: blue;
      color: #fff;
  	}
</style>

Here Insert Picture Description

Published 48 original articles · won praise 52 · views 50000 +

Guess you like

Origin blog.csdn.net/letterTiger/article/details/89532666