3
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【CSS】子要素の数に応じてスタイルを適用する方法

Last updated at Posted at 2024-02-18

特定の数の場合にのみ適用

以下のように記述すると子要素が指定した数の場合にのみスタイルを適用できます。

ul:has(> :nth-child(N):last-child) li {  }

子要素が2つの場合にのみスタイルを適用するには以下のように記述します。

ul:has(> :nth-child(2):last-child) li {  }

特定の数以上の場合にのみ適用

以下のように:has()を使用することで指定した数以上子要素が存在する場合にのみスタイルを適用できます。

ul:has(> :nth-child(N)) li { }

2つ以上ある場合にスタイルを適用するには以下のようにします。

ul:has(> :nth-child(2)) li { }

3
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?