: 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>
Alternatively: nth-child as: nth-of-type
<style>
body :nth-of-type(2) {
height: 30px;
background: blue;
color: #fff;
}
</style>