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> 
廣告