青草久久影院-青草久久伊人-青草久久久-青草久久精品亚洲综合专区-SM双性精跪趴灌憋尿调教H-SM脚奴调教丨踩踏贱奴

17站長(zhǎng)網(wǎng)

nth 類型元素選擇器

nth 元素選擇

當(dāng)我們要一組 class 同名,或者連續(xù)的一組元素的其中一個(gè),或者某種規(guī)律的元素添加單獨(dú)樣式的時(shí)候,不妨看看這類的元素選擇器。

1. 官方定義

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

  • nth-last-child(n) 選擇器匹配屬于其元素的第 N 個(gè)子元素的每個(gè)元素,從最后一個(gè)子元素開(kāi)始計(jì)數(shù);

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

2. 解釋

nth-child(n)、 nth-last-child(n) 、nth-of-type(n) 都是用來(lái)匹配父元素內(nèi)部子元素的。不過(guò)也有些區(qū)別:
nth-child 按照個(gè)數(shù)來(lái)算;
nth-of-type 按照類型來(lái)計(jì)算;
nth-last-child(n) 從最后一個(gè)子元素往前開(kāi)始計(jì)算。

3. 語(yǔ)法

.item:nth-child(2n+1){
}
.item:nth-of-type(n){
}
.item:nth-last-child(2n){
}
n 從  開(kāi)始計(jì)數(shù)的正整數(shù)。

4. 兼容性

IEEdgeFirefoxChromeSafariOperaiosandroid
allallallallallallallall

5. 實(shí)例

選擇 demo 內(nèi)第 3 個(gè)子元素背景為紅色。

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

效果圖:

編程之家

第三個(gè)背景變紅效果圖
<!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;
}

效果圖

編程之家

第三個(gè)背景變紅效果圖
<!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;
}

效果圖

編程之家

第三個(gè)背景變紅效果圖
<!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. 經(jīng)驗(yàn)分享

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

<div class="demo">
    <p class="item">我是 p 標(biāo)簽</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 標(biāo)簽</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` 效果圖

通過(guò)效果圖我們就清楚的明白他們的差異了。
簡(jiǎn)述實(shí)例展現(xiàn)效果,通過(guò)實(shí)例分析他們兩個(gè)的區(qū)別

<!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 標(biāo)簽</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 標(biāo)簽</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>

下面是讓所有偶數(shù)的背景變紅。

.item{
            width: px;
            height: px;
            text-align: center;
            line-height: px;
            border: px solid #ccc;
            background: #f2f2f2;
        }
        .item:nth-of-type(2n){
            background: red;
        }

效果圖:

編程之家

偶數(shù)的背景變紅 效果圖
<!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 一定要放在最前面。

返回頂部
主站蜘蛛池模板: 女人精69xxxxx | 黄 色 网 站 免 费 涩涩屋 | 月夜直播免费观看全集 | 亚洲精品久久一区二区三区四区 | 99视频免视看 | 国产精品视频免费观看 | 麻豆AV久久AV盛宴AV | 久九九精品免费视频 | 美女被爽cao免费漫画 | 欧美成人精品高清在线观看 | 凹凸精品视频分类视频 | 国产精品99久久久久久WWW | 动漫AV纯肉无码AV电影网 | 日韩精品在线看 | 小SAO货叫大声点妓女 | 2019天天射干网站 | 一本道无码字幕在线看 | 男人和女人全黄一级毛片 | 国产免费人视频在线观看免费 | 后入内射国产一区二区 | 和姐姐做插得很深 | 色狗av影院| 一本久道视频无线视频 | 成人区在线观看免费视频 | 天天躁人人躁人人躁狂躁 | 国产一级做a爰片久久毛片男 | 日本高清免费看 | 中文字幕乱偷无码AV蜜桃 | xxnx18日本| 精品视频在线观看视频免费视频 | 亚洲高清免费在线观看 | 午夜性色一区二区三区不卡视频 | free性中国hd护士高清 | 5G在线观看免费年龄确认 | 国产亚洲视频在线观看 | 艳照门在线播放 | 国产午夜精品视频在线播放 | 久久是热这里只有精品 | 手机在线国产视频 | 精品国产乱码久久久久乱码 | 九九99热久久999精品 |