0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

CSS :focus-within擬似クラスセレクタ

0
Last updated at Posted at 2026-08-12

😄この投稿は、codingeverybody.jpのコンテンツをもとに作成しています。

定義と使い方

:focus-within擬似クラスは、要素自身またはその子要素のいずれかがフォーカスされている間、その親要素を選択します。このセレクタは、親要素にスタイルを適用できる点で、:focus擬似クラスとは異なります。

---- HTML ----
<form>
    <fieldset>
        <legend>名前を入力してください。</legend>
        <label for="user-name">Username</label>
        <input type="text" id="user-name" placeholder="名前を入力...">
    </fieldset>
</form>

---- CSS ----
fieldset {
    padding: 1em;
}
fieldset:focus-within { /* 自身または子要素がフォーカスされている間に選択される */
    background-color: yellow;
}

:focus-within擬似クラスは、その要素の子要素のいずれかがフォーカスされている間も適用されるため、主に視覚的なウェブアクセシビリティの向上に使用され、キーボードでサイトを操作したり、アクセシビリティを考慮したデザインを実装する際に役立ちます。

参考資料

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?