
- CSS 教程
- CSS - 首頁
- CSS - 路線圖
- CSS - 簡介
- CSS - 語法
- CSS - 選擇器
- CSS - 包含
- CSS - 測量單位
- CSS - 顏色
- CSS - 背景
- CSS - 字型
- CSS - 文字
- CSS - 圖片
- CSS - 連結
- CSS - 表格
- CSS - 邊框
- CSS - 塊級邊框
- CSS - 內聯邊框
- CSS - 外邊距
- CSS - 列表
- CSS - 內邊距
- CSS - 游標
- CSS - 輪廓
- CSS - 尺寸
- CSS - 捲軸
- CSS - 內聯塊
- CSS - 下拉選單
- CSS - 可見性
- CSS - 溢位
- CSS - 清除浮動
- CSS - 浮動
- CSS - 箭頭
- CSS - 調整大小
- CSS - 引號
- CSS - 順序
- CSS - 位置
- CSS - 連字元
- CSS - 懸停
- CSS - 顯示
- CSS - 聚焦
- CSS - 縮放
- CSS - 平移
- CSS - 高度
- CSS - 連字元字元
- CSS - 寬度
- CSS - 不透明度
- CSS - Z-Index
- CSS - 底部
- CSS - 導航欄
- CSS - 覆蓋層
- CSS - 表單
- CSS - 對齊
- CSS - 圖示
- CSS - 圖片庫
- CSS - 註釋
- CSS - 載入器
- CSS - 屬性選擇器
- CSS - 組合器
- CSS - 根元素
- CSS - 盒模型
- CSS - 計數器
- CSS - 剪下
- CSS - 書寫模式
- CSS - Unicode-bidi
- CSS - min-content
- CSS - 全部
- CSS - 內嵌
- CSS - 隔離
- CSS - 滾動溢位
- CSS - Justify Items
- CSS - Justify Self
- CSS - 製表符大小
- CSS - 指標事件
- CSS - Place Content
- CSS - Place Items
- CSS - Place Self
- CSS - 最大塊級尺寸
- CSS - 最小塊級尺寸
- CSS - 混合模式
- CSS - 最大內聯尺寸
- CSS - 最小內聯尺寸
- CSS - 偏移量
- CSS - 重音顏色
- CSS - 使用者選擇
- CSS 高階
- CSS - 網格
- CSS - 網格佈局
- CSS - Flexbox
- CSS - 可見性
- CSS - 定位
- CSS - 圖層
- CSS - 偽類
- CSS - 偽元素
- CSS - @規則
- CSS - 文字效果
- CSS - 分頁媒體
- CSS - 列印
- CSS - 佈局
- CSS - 驗證
- CSS - 圖片精靈
- CSS - 重要性
- CSS - 資料型別
- CSS3 教程
- CSS3 - 教程
- CSS - 圓角
- CSS - 邊框圖片
- CSS - 多背景
- CSS - 顏色
- CSS - 漸變
- CSS - 盒陰影
- CSS - 盒裝飾中斷
- CSS - 游標顏色
- CSS - 文字陰影
- CSS - 文字
- CSS - 2D 變換
- CSS - 3D 變換
- CSS - 過渡
- CSS - 動畫
- CSS - 多列
- CSS - 盒尺寸
- CSS - 工具提示
- CSS - 按鈕
- CSS - 分頁
- CSS - 變數
- CSS - 媒體查詢
- CSS - 函式
- CSS - 數學函式
- CSS - 遮罩
- CSS - 形狀
- CSS - 樣式圖片
- CSS - 特異性
- CSS - 自定義屬性
- CSS 響應式
- CSS RWD - 簡介
- CSS RWD - 視口
- CSS RWD - 網格檢視
- CSS RWD - 媒體查詢
- CSS RWD - 圖片
- CSS RWD - 影片
- CSS RWD - 框架
- CSS 工具
- CSS - PX 到 EM 轉換器
- CSS - 顏色選擇器和動畫
- CSS 資源
- CSS - 有用資源
- CSS - 討論
CSS 偽類 - :nth-child()
CSS :nth-child() 偽類根據元素在其父元素的子元素中的位置來選擇元素。:nth-child() 偽類接受一個引數,該引數可以是數字、關鍵字或數學表示式。
語法
:nth-child(<nth> [of <complex-selector-list>]?) { /* ... */ }
使用 :nth-child() CSS 偽類時,子元素計數包括所有兄弟姐妹子元素(任何元素型別),但只有與選擇器其他元件匹配的元素才會被選中。
可能的值
-
odd - 此值選擇所有奇數編號的子元素,例如 1、3、5 等。
-
even - 此值選擇所有偶數編號的子元素,例如 2、4、6 等。
-
函式表示法 (<an+b>) - 此值從其父容器中選擇每個第 an+b 個子元素,其中“a”是正整數,“n”是從 0 開始的計數器變數。“b”是另一個正整數。
下表描述了 CSS 選擇器列表及其描述
選擇器 | 描述 |
---|---|
tr:nth-child(odd) 或 tr:nth-last-child(2n+1) | HTML 表格中的所有奇數行。 |
tr:nth-child(even) 或 tr:nth-last-child(2n) | HTML 表格中的所有偶數行。 |
:nth-child(7) | 選擇第七個元素。 |
:nth-child(5n) | 選擇是 5 的倍數的元素,例如第 5 個、第 10 個、第 15 個等。 |
:nth-child(n+6) | 選擇從第六個元素開始的所有元素。 |
:nth-child(3n+4) | 選擇是 3 的倍數加 4 的元素,例如第 4 個、第 7 個、第 10 個、第 13 個等。 |
:nth-child(-n+3) | 選擇前三個元素。 |
p:nth-child(n) | 選擇所有在一組兄弟姐妹中的 <p> 元素。 |
p:nth-child(1) 或 p:nth-child(0n+1) | 選擇所有其父元素的第一個子元素的 <p> 元素。 |
p:nth-child(n+8):nth-child(-n+15) | 選擇一組兄弟姐妹元素的第八到第十五個 <p> 元素。 |
CSS :nth-child 示例
以下是如何僅選擇和設定段落元素樣式的示例:
<html> <head> <style> p, em { display: inline-block; border: 3px solid black; margin: 5px; padding: 5px; } .box1 p:nth-child(2n+1), .box2 p:nth-child(2n+1), .box3 p:nth-of-type(2n + 1) { background-color: pink; color: blue; } </style> </head> <body> <h4>Styling all odd-numbered (1,3,5,7) paragraphs with a pink background and blue font color.</h4> <div class="box1"> <p>Orange</p> <p>Apple</p> <p>Mango</p> <p>Grapes</p> <p>Banana</p> <p>Watermelon</p> <p>Cheery</p> <p>Pear</p> </div> <h4>Styling all odd-numbered paragraphs (1, 3, 7, etc.) with a pink background and blue font color, except for sentence 5, which is not a paragraph element.</h4> <div class="box2"> <p>Orange</p> <p>Apple</p> <p>Mango</p> <p>Grapes</p> <em>Banana</em> <p>Watermelon</p> <p>Cheery</p> <p>Pear</p> </div> <h4>Styling paragraphs (1, 4, 6, 8 ) with a pink background and blue except sentence 3 because it's not a paragraph element.</h4> <div class="box3"> <p>Orange</p> <p>Apple</p> <em>Mango</em> <p>Grapes</p> <p>Banana</p> <p>Watermelon</p> <p>Cheery</p> <p>Pear</p> </div> </body> </html>
CSS :nth-child() - 列表示例
以下是如何設定 ol 列表中 li 元素樣式的示例:
<html> <head> <style> li:nth-child(1) { font-weight: bold; color: red; } li:nth-child(3n+4){ background-color: pink; } li:nth-child(n+6){ font-weight: bold; } </style> </head> <body> <p>Styling first list as bold and red.</p> <p>Styling every third list item starting with the fourth list item with a pink background.</p> <p>Styling all list items from the sixth list item onwards as bold.</p> <ol> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> <li>Item 4</li> <li>Item 5</li> <li>Item 6</li> <li>Item 7</li> <li>Item 8</li> <li>Item 9</li> <li>Item 10</li> </ol> </body> </html>
CSS :nth-child() - <selector>
帶有 of <selector> 元素的 :nth-child() 偽類的語法如下
li:nth-child(even of .fruits) { /* Your styles here */ }
將選擇器 li.fruits 移到函式之外將選擇所有具有 fruits 類的 li 元素,而不管它們在子元素列表中的位置。
li.fruits: nth-child(-n + 3);
以下是如何使用 :nth-child(even of .fruits) 偽類的示例。這將選擇 .fruits 元素的所有偶數子元素:
<html> <head> <style> ul { list-style-type: none; } li { display: inline-block; border: 3px solid black; margin: 5px; padding: 5px; } .fruits { background-color: pink; } li:nth-child(even of .fruits) { font-weight: bold; color: blue; } </style> </head> <body> <h3>Selects all even-numbered child elements of the class .fruits.</h3> <ul> <li>Orange</li> <li class="fruits">Apple</li> <li class="fruits">Mango</li> <li class="fruits">Grapes</li> <li class="fruits">Banana</li> <li>Watermelon</li> <li class="fruits">Cheery</li> <li class="fruits">Pear</li> </ul> </body> </html>
CSS :nth-child() - <selector> 與 <selector> nth child
以下是如何使用 :nth-child() 偽類根據元素在其兄弟姐妹中的位置(相對於另一個元素)來選擇元素的示例:
<html> <head> <style> ul { list-style-type: none; } li { display: inline-block; border: 3px solid black; margin: 5px; padding: 5px; } .fruits { background-color: pink; } ul.list1 > li:nth-child(-n + 2 of .fruits) { color: blue; } ul.list2 > li.fruits:nth-child(-n + 4) { color: blue; } </style> </head> <body> <p>Styling the first two child elements (with .fruits class) in the list.</p> <ul class="list1"> <li>Orange</li> <li class="fruits">Apple</li> <li>Mango</li> <li class="fruits">Grapes</li> <li class="fruits">Banana</li> <li>Watermelon</li> <li>Cheery</li> <li class="fruits">Pear</li> </ul> <p>Select the first four child elements that are both .fruits elements and in the .list2 list.</p> <ul class="list2"> <li class="fruits">Orange</li> <li>Apple</li> <li class="fruits">Mango</li> <li>Grapes</li> <li>Banana</li> <li class="fruits">Watermelon</li> <li>Cheery</li> <li class="fruits">Pear</li> </ul> </body> </html>
CSS :nth-child() - 用於修復表格條帶的選擇器
以下是如何使用 :nth-child() 偽類設定表格中偶數行的樣式的示例,即使某些行被隱藏:
<html> <head> <style> table { margin: 20px; } .even-rows > tbody > tr:nth-child(even) { background-color: pink; } .even-rows-hidden > tbody > tr:nth-child(even of :not([hidden])) { background-color: pink; } </style> </head> <body> <p>Selector selects all even rows in the tbody of a table.</p> <table class="even-rows"> <thead> <tr> <th>First Names</th> <th>Last Names</th> </tr> </thead> <tbody> <tr> <td>Jhon</td> <td>Sean</td> </tr> <tr> <td>Rocky</td> <td>Luis</td> </tr> <tr hidden> <td>Oliver</td> <td>David</td> </tr> <tr> <td>Marry</td> <td>Alamnd</td> </tr> <tr> <td>Dora</td> <td>Ann</td> </tr> <tr> <td>Tonny</td> <td>Rai</td> </tr> </tbody> </table> <p>Selector selects all even rows in the tbody of a table, but skips any rows that are hidden.</p> <table class="even-rows-hidden"> <thead> <tr> <th>First Names</th> <th>Last Names</th> </tr> </thead> <tbody> <tr> <td>Jhon</td> <td>Sean</td> </tr> <tr> <td>Rocky</td> <td>Luis</td> </tr> <tr hidden> <td>Oliver</td> <td>David</td> </tr> <tr> <td>Marry</td> <td>Alamnd</td> </tr> <tr> <td>Dora</td> <td>Ann</td> </tr> <tr> <td>Tonny</td> <td>Rai</td> </tr> </tbody> </table> </body> </html>
廣告