精品免费在线观看-精品欧美-精品欧美成人bd高清在线观看-精品欧美高清不卡在线-精品欧美日韩一区二区

17站長網

17站長網 首頁 編程教程 CSS3教程 查看內容

nth 類型元素選擇器

nth 元素選擇

當我們要一組 class 同名,或者連續的一組元素的其中一個,或者某種規律的元素添加單獨樣式的時候,不妨看看這類的元素選擇器。

1. 官方定義

  • nth-child(n) 選擇器匹配屬于其父元素的第 N 個子元素;

  • nth-last-child(n) 選擇器匹配屬于其元素的第 N 個子元素的每個元素,從最后一個子元素開始計數;

  • nth-of-type(n) 選擇器匹配屬于父元素的特定類型的第 N 個子元素的每個元素。

2. 解釋

nth-child(n)、 nth-last-child(n) 、nth-of-type(n) 都是用來匹配父元素內部子元素的。不過也有些區別:
nth-child 按照個數來算;
nth-of-type 按照類型來計算;
nth-last-child(n) 從最后一個子元素往前開始計算。

3. 語法

.item:nth-child(2n+1){
}
.item:nth-of-type(n){
}
.item:nth-last-child(2n){
}
n 從  開始計數的正整數。

4. 兼容性

IEEdgeFirefoxChromeSafariOperaiosandroid
allallallallallallallall

5. 實例

選擇 demo 內第 3 個子元素背景為紅色。

  1. 使用 nth-child。

.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>
    <Meta charset="UTF-8">
    <Meta 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>
  1. 使用 nth-last-child。

.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>
    <Meta charset="UTF-8">
    <Meta 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>
  1. 使用nth-of-type。

.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>
    <Meta charset="UTF-8">
    <Meta 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>

6. 經驗分享

  1. 在實例中我們看到 nth-of-type 和 nth-child 同樣都使用的是 (3), 那么它們的不同是什么呢?下面這個例子我們一起看下:

<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;
       }

效果圖

編程之家

`nth-of-type` 和 `nth-child` 效果圖

通過效果圖我們就清楚的明白他們的差異了。
簡述實例展現效果,通過實例分析他們兩個的區別

<!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>
        .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>
    <Meta charset="UTF-8">
    <Meta 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>
  1. 使用 nth-of-type(3n+1) 起作用,而 nth-of-type(1+3n) 不起作用,所以 n 一定要放在最前面。

返回頂部
主站蜘蛛池模板: 欧美日韩乱码毛片免费观看 | 欧美特黄一免在线观看 | 色视网站| 精品欧美一区二区vr在线观看 | 国产精品久久久久久久福利院 | 四虎884aa永久播放地址http | 欧美激情视频一区二区免费 | 能看的黄色网址 | 日韩高清一区 | 国产首页| 久 在线播放 | 高清免费国产在线观看 | 热门国产xvideos中文 | 超级97碰碰碰碰久久久久最新 | 黄色影院免费 | 黄网免费 | 久久精品草 | 黑人巨大vs北条麻妃在线播放 | 色婷婷色综合 | 国产亚洲精品成人a在线 | 日本人与黑人做爰视频网站 | 综合亚洲精品一区二区三区 | 欧美在线免费观看视频 | 俄罗斯小younv | 国内永久第一免费福利视频 | 91摄影师与白嫩模特观看 | 精品视频久久久久 | 国产精品美女网站在线观看 | 成人一区二区丝袜美腿 | 精品外国呦系列在线观看 | 大学生a级毛片免费观看 | aⅴ一区二区三区无卡无码 aⅴ在线免费观看 | 小明永久免费 | 青青草精品在线视频 | 污污小视频在线观看 | 国产黄色自拍 | 自偷自拍亚洲欧美清纯唯美 | 久久精品视频久久 | 亚洲久草视频 | 在线高清免费爱做网 | 国产精品美女在线 |