<ol>
    <li>li</li>
    <li>li</li>
    <li>li</li>
    <li>li</li>
    <li>li</li>
</ol>
ol > :nth-child(2) { /* <ol>の子要素の中で2番目に位置する要素を選択 */
    background-color: yellow;
}
実際に適用した結果
/* 正の整数:n番目 */
:nth-child(1) /* 任意の子要素の中から1番目の要素を選択 */
li:nth-child(1) /* 任意の子要素の中から1番目にあたる<li>要素を選択 */

/* 正の整数:n番目 */
:nth-child(7) /* 任意の子要素の中から7番目の要素を選択 */
li:nth-child(7) /* 任意の子要素の中から7番目にあたる<li>要素を選択 */

/* キーワード:even / odd */
:nth-child(odd) /* 任意の子要素の中から奇数(1,3,5,...)番目の要素を選択 */
li:nth-child(even) /* 任意の子要素の中から偶数(2,4,6,...)番目にあたる<li>要素を選択 */

/* 数式:An+B */
:nth-child(2n) /* 任意の子要素の中から2の倍数番目の要素を選択 */
li:nth-child(2n) /* 任意の子要素の中から2の倍数番目にあたる<li>要素を選択 */

:nth-child(3n) /* 任意の子要素の中から3の倍数番目の要素を選択 */
li:nth-child(3n) /* 任意の子要素の中から3の倍数番目にあたる<li>要素を選択 */

:nth-child(3n+1) /* 任意の子要素の中から3の倍数に1を加えた番目の要素を選択 */
:nth-child(3n-1) /* 任意の子要素の中から3の倍数から1を引いた番目の要素を選択 */
:nth-child(index | odd | even | an+b) {
    /* ... */
}
<ol>
    <li>li</li>
    <li>li</li>
    <li>li</li>
</ol>
li:nth-child(2) { /* 非負の整数を指定可能 */
    border: 3px solid blue;
}
実際に適用した結果 インデックスは1から始まります。
<ol>
    <li>li</li>
    <li>li</li>
    <li>li</li>
    <li>li</li>
</ol>
li:nth-child(even) {
    background-color: yellowgreen;
}
実際に適用した結果
:nth-child(5n)
/*
 * 0[=5×0]、5[=5×1]、10[=5×2]、15[=5×3]、...番目の要素を表します。
 * 要素のインデックスは1から始まるため、0番目の要素は存在せず、結果として5、10、15、...番目の要素を表します。
 * 結論として、5nは5の倍数番目の要素を選択します。
*/

:nth-child(5n-1)
/*
 * -1[=5×0-1]、4[=5×1-1]、9[=5×2-1]、14[=5×3-1]、...番目の要素を表します。
 * 要素のインデックスは1から始まるため、-1番目の要素は存在せず、結果として4、9、14、...番目の要素を表します。
 * 結論として、5n-1は5の倍数番目の1つ前にあたる要素を選択します。
*/

:nth-child(5n+1)
/*
 * 1[=5×0+1]、6[=5×1+1]、11[=5×2+1]、16[=5×3+1]、...番目の要素を表します。
 * 1、6、11、16、...番目の要素を表します。
 * 結論として、5n+1は5の倍数番目の1つ後にあたる要素を選択します。
*/

:nth-child(n)
/*
 * 0[0]、1[1]、2[2]、3[3]、...番目の要素を表します。
 * 要素のインデックスは1から始まるため、0番目の要素は存在せず、結果として1、2、3、...番目の要素を表します。
 * 結論として、nはすべての要素を表します。
*/
<ol>
    <li>li</li>
    <li>li</li>
    <li>li</li>
    <li>li</li>
    <li>li</li>
    <li>li</li>
    <li>li</li>
    <li>li</li>
    <li>li</li>
    <li>li</li>
    <li>li</li>
    <li>li</li>
    <li>li</li>
    <li>li</li>
    <li>li</li>
</ol>   
ol {
    margin: 0;
    padding: 0;
    list-style: none;
    display: grid;
    grid-template-columns: 1fr 1fr 1fr 1fr 1fr;
    gap: 7px;
}
ol li {
    aspect-ratio: 1/0.5;
    border: 1px solid red;
    display: flex;
    justify-content: center;
    align-items: center;
    border-radius: 10px;
}
ol li:nth-child(5n-1) { /* 5の倍数番目の1つ前にあたる要素を選択 */
    background-color: yellowgreen;
}
実際に適用した結果

最終更新日: 2025-12-06

caniuse.comで詳しい情報をご確認ください。