精品免费在线观看-精品欧美-精品欧美成人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 一定要放在最前面。

返回頂部
主站蜘蛛池模板: 欧美国产一区二区三区 | 一区二区三区在线免费观看视频 | 涩涩99 | 亚洲精品中文字幕无乱码 | 色婷五月综激情亚洲综合 | 日韩不卡视频在线 | 中国一级特黄真人毛片免费看 | 在线观看免费情网站大全 | 国产精品成人第一区 | 日韩在线不卡视频 | 国产v国产v片大片线观看网站 | 中国性视频 | 亚洲精品中文字幕乱码一区二区 | 国产亚洲精品久久久久久久网站 | 亚洲国产欧美一区 | 国产一区二区精品久久91 | 久久www免费人成精品香蕉 | 免费看色片网站 | 亚洲欧美日韩久久一区 | 国产不卡一区二区视频免费 | 国内精品视频 | 国产精品天天影视久久综合网 | 91久久另类重口变态 | 久久在线免费观看 | 黄色综合网站 | 久草在线资源福利站 | 我要看黄色录像一级片 | 我要看黄色录像一级片 | 成人夜色视频在线观看网站 | 亚洲 国产 路线1路线2路线 | 黄色视屏免费观看 | 国产一区二区三区在线观看视频 | 国产色婷婷视频在线观看 | 一级日韩一级欧美 | 精品久久久久久午夜 | 久久香蕉国产在产线看观看 | 亚洲欧美日韩国产综合专区 | 真人毛片免费全部播放完整 | 久99re视频9在线观看 | 麻豆传媒免费入口 | 最新国产精品亚洲二区 |