- CSS
-
2024-10-16
CSSのnth-child
・nth-of-type
等の擬似要素を使えば兄弟要素の何番目の要素に対してのみスタイルを適用する等、理解して使えば柔軟なスタイル設定が可能です。
自身の備忘録のためにも書き留めておきたいと思います。
:first-child, :first-of-type
兄弟要素のうち最初の要素を指定します。
■□□□□□□□□□□
:last-child, :last-of-type
兄弟要素のうち最後の要素を指定します。
□□□□□□□□□□■
:nth-child(A), :nth-of-type(A)
兄弟要素のうちA番目の要素を指定します。
□□■□□□□□□□□
:nth-last-child(n), :nth-last-of-type(n)
兄弟要素のうち最後からn番目の要素を指定します。
□□□□□□□□■□□
:nth-child(-n+A), :nth-of-type(-n+A)
兄弟要素のうち先頭からA個の要素を指定します。
■■■□□□□□□□□
:nth-child(n+A), :nth-of-type(n+A)
