日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

nth选择器详解

發布時間:2023/12/14 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 nth选择器详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

先來認識幾個常用的

  • :first-child選擇某個元素的第一個子元素
  • :last-child選擇某個元素的最后一個子元素
  • :nth-child()選擇某個元素的一個或多個特定的子元素
  • :nth-last-child()選擇某個元素的一個或多個特定的子元素,從這個元素的最后一個子元素開始算
  • :nth-of-type()選擇指定的元素
  • :nth-last-of-type()選擇指定的元素,從元素的最后一個開始計算
  • :first-of-type選擇一個上級元素下的第一個同類子元素
  • :last-of-type選擇一個上級元素的最后一個同類子元素

實例

基礎代碼:

<title>nth選擇器</title><style>.box1 p{background-color: red;margin-top: 20px;}</style> </head> <body><div class="box1"><p>111</p><p>222</p><p>333</p><p>444</p><p>555</p></div> </body> </html>


以下前兩個示例都是基于這個html

1. :first-child

代碼:

.box1 p:first-child{background-color: blue; }

結果:

last-child與它相反,選擇的是最后一個

2. :nth-child()

代碼:

.box1 p:nth-child(2){background-color: blue; }

結果:

代碼:

.box1 p:nth-child(2n + 1){ // n表示從零開始的自然數background-color: blue; }

結果:

nth-last-child()與它相反,從最后一個開始算起

3. :nth-of-type()

下面的示例都是基于這個html

代碼:

<style>.box1 p{background-color: red;margin-top: 20px;}.box1 h1:nth-of-type(2n + 1){background-color: blue;}</style> </head> <body><div class="box1"><h1>h111</h1><h1>h222</h1><h1>h333</h1><p>111</p><p>222</p><p>333</p><p>444</p><p>555</p></div> </body> </html>

結果:

代碼:

.box1 p:nth-of-type(2n + 1){background-color: blue; }

結果:

:nth-last-of-type()從最后一個算起

4. :first-of-type

代碼:

.box1 h1:first-of-type{background-color: blue; }

結果:

代碼:

.box1 p:first-of-type{background-color: blue; }

結果:

:last-of-type選擇最后一個

總結

以上是生活随笔為你收集整理的nth选择器详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。