精品免费在线观看-精品欧美-精品欧美成人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久久免费精品高清特色大片 | 日韩视频中文字幕专区 | 国产91青青成人a在线 | 草草青青 | 亚洲va中文字幕欧美不卡 | 久久久精品午夜免费不卡 | 国产精品一区二区av | 亚洲国产精品91 | 中文在线免费不卡视频 | 91入口 | 国产精品无卡无在线播放 | 国产一卡 | 亚洲精品国产第一区第二区国 | 公又粗又长又大又深好爽日本 | 国精品一区二区三区 | 成人久久精品一区二区三区 | www.黄色片.com | 伊人天天 | 一区在线视频 | 一级黄色片免费看 | 美女隐私视频黄www免费 | 91午夜精品亚洲一区二区三区 | 欧美亚洲国产成人精品 | 精品在线91| 久久成人在线 | 久久久久国产成人精品 | 三级午夜宅宅伦不卡在线 | 亚洲国产欧美一区 | 日韩在线观看高清 | 日本黄色激情片 | 黄色片一级 | 国产午夜亚洲精品久久www | 超高清欧美videos360 | 小优视频高清视频在线看 | a级午夜毛片免费一区二区 a级午夜理论免费毛片 | 国产一区在线播放 | 在线观看 日韩 | 黄色影院在线观看视频 | 337p粉嫩大胆噜噜噜鲁 |