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

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 一定要放在最前面。

返回頂部
主站蜘蛛池模板: 日日碰狠狠添天天爽 | 妻子的秘密HD观看 | 久99久热只有精品国产99 | chinese国语露脸videos| 99热成人精品国产免男男 | 欧洲videosdesrxotv | 无码乱人伦一区二区亚洲一 | 三级黃60分钟 | 美女扒开腿让男生桶爽免费APP | 国产精品97久久AV色婷婷综合 | 国产区免费在线观看 | 区产品乱码芒果精品P站在线 | 亚洲国产日韩欧美高清片a 亚洲国产日韩a精品乱码 | 久草在线福利资站免费视频 | qvod免费电影 | 亚欧乱亚欧乱色视频 | 爆操大胸美女 | 欧美美女一区二区三区 | 曰本女人牲交视频免费 | 国产 亚洲 另类 欧美 在线 | 麻豆传煤网站网址入口在线下载 | zoovideo人与驴mp4 | 亚洲视频在线观看网站 | 国产在线精品亚洲第一区 | 亚洲成片在线看 | 三级成人电彭 | 超碰视频在线观看 | 99久久国产综合精品国 | 在线播放av欧美无码碰 | 国产精品999 | 黄瓜视频苹果直接安装 | 激情内射亚州一区二区三区爱妻 | 一本色道久久综合亚洲精品 | 国产色青青视频在线观看 | 香蕉久久一区二区三区啪啪 | 性欧美videosex18嫩 | 美女直播喷水 | 99国产精品久久久久久久日本竹 | 粗好大用力好深快点漫画 | 伊人久久大香线蕉综合网站 | 浴室里强摁做开腿呻吟的漫画男男 |