nth 类型元素选择器
当我们要一组 class
同名,或者连续的一组元素的其中,或者某种规律的元素单独样式的时候,不妨看看这类的元素选择器。
nth-child(n)
、 nth-last-child(n)
、nth-of-type(n)
都是用来匹配父元素内部子元素的。不过也有些区别:nth-child
按照个数来算;nth-of-type
按照类型来计算;nth-last-child(n)
从最后子元素往前开始计算。
.item:nth-child(2n+1){
}
.item:nth-of-type(n){
}
.item:nth-last-child(2n){
}
n 从 开始计数的正整数。
选择 demo 内第 3 个子元素背景为红色。
.item{
width: px;
height: px;
text-align: center;
line-height: px;
border: px solid #ccc;
background: #f2f2f2;
}
.item:nth-child(3){
background: red;
}
图:
<!DOCTYPE html>
<html lang="en">
<head>
< charset="UTF-8">
< name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.item{
width: px;
height: px;
text-align: center;
line-height: px;
border: px solid #ccc;
background: #f2f2f2;
}
.item:nth-child(3){
background: red;
}
</style>
</head>
<body>
<div class="demo">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
</div>
</body>
</html>
.item{
width: px;
height: px;
text-align: center;
line-height: px;
border: px solid #ccc;
background: #f2f2f2;
}
.item:nth-last-child(2){
background: red;
}
图
<!DOCTYPE html>
<html lang="en">
<head>
< charset="UTF-8">
< name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.item{
width: px;
height: px;
text-align: center;
line-height: px;
border: px solid #ccc;
background: #f2f2f2;
}
.item:nth-last-child(2){
background: red;
}
</style>
</head>
<body>
<div class="demo">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
</div>
</body>
</html>
.item{
width: px;
height: px;
text-align: center;
line-height: px;
border: px solid #ccc;
background: #f2f2f2;
}
.item:nth-of-type(3){
background: red;
}
图
<!DOCTYPE html>
<html lang="en">
<head>
< charset="UTF-8">
< name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.item{
width: px;
height: px;
text-align: center;
line-height: px;
border: px solid #ccc;
background: #f2f2f2;
}
.item:nth-of-type(3){
background: red;
}
</style>
</head>
<body>
<div class="demo">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
</div>
</body>
</html>
<div class="demo">
<p class="item">我是 p </p>
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
</div>
<div class="demo">
<p class="item-2">我是 p </p>
<div class="item-2">1</div>
<div class="item-2">2</div>
<div class="item-2">3</div>
<div class="item-2">4</div>
</div>
.demo{
float: left;
}
.item,.item-2{
width: px;
height: px;
text-align: center;
line-height: px;
border: px solid #ccc;
background: #f2f2f2;
}
.item:nth-of-type(3){
background: red;
}
.item-2:nth-child(3){
background: red;
}
图
通过图我们就清楚的明白他们的差异了。
简述实例展现,通过实例分析他们两个的区别
<!DOCTYPE html>
<html lang="en">
<head>
< charset="UTF-8">
< name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.demo{
float: left;
}
.item,.item-2{
width: px;
height: px;
text-align: center;
line-height: px;
border: px solid #ccc;
background: #f2f2f2;
}
.item:nth-of-type(3){
background: red;
}
.item-2:nth-child(3){
background: red;
}
</style>
</head>
<body>
<div class="demo">
<p class="item">我是 p </p>
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
</div>
<div class="demo">
<p class="item-2">我是 p </p>
<div class="item-2">1</div>
<div class="item-2">2</div>
<div class="item-2">3</div>
<div class="item-2">4</div>
</div>
</body>
</html>
下面是让所有偶数的背景变红。
.item{
width: px;
height: px;
text-align: center;
line-height: px;
border: px solid #ccc;
background: #f2f2f2;
}
.item:nth-of-type(2n){
background: red;
}
图:
<!DOCTYPE html>
<html lang="en">
<head>
< charset="UTF-8">
< name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.item{
width: px;
height: px;
text-align: center;
line-height: px;
border: px solid #ccc;
background: #f2f2f2;
}
.item:nth-of-type(2n){
background: red;
}
</style>
</head>
<body>
<div class="demo">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
</div>
</body>
</html>