--- title: ':focus' slug: 'Web/CSS/:focus' tags: - CSS - Layout - Reference - Web - ウェブ - セレクター - レイアウト - 擬似クラス - 疑似クラス translation_of: 'Web/CSS/:focus' --- <div>{{CSSRef}}</div> <p><strong><code>:focus</code></strong> は <a href="/ja/docs/Web/CSS">CSS</a> の <a href="/ja/docs/Web/CSS/Pseudo-classes" title="Pseudo-classes">擬似クラス</a>で、フォーカスを持っている (フォームの入力のような) 要素を表します。普通はユーザーが要素をクリックやタップをしたり、キーボードの[タブ]キーで選択したりしたときです。</p> <pre class="brush: css no-line-numbers notranslate">/* フォーカスを持つ <input> 要素を選択 */ input:focus { color: red; }</pre> <div class="blockIndicator note"> <p><strong>メモ:</strong> この擬似クラスはフォーカスを持つ要素に対してのみ適用されます。フォーカスを持つ要素を<em>含む</em>要素を選択したい場合は、 {{CSSxRef(":focus-within")}} を使用してください。</p> </div> <h2 id="Syntax" name="Syntax">構文</h2> {{CSSSyntax}} <h2 id="Example" name="Example">例</h2> <h3 id="HTML">HTML</h3> <pre class="brush: html no-line-numbers notranslate"><input class="red-input" value="I'll be red when focused."><br> <input class="blue-input" value="I'll be blue when focused."></pre> <h3 id="CSS">CSS</h3> <pre class="brush: css highlight[1, 6] notranslate">.red-input:focus { background: yellow; color: red; } .blue-input:focus { background: yellow; color: blue; }</pre> <h3 id="Result" name="Result">結果</h3> <p>{{EmbedLiveSample("Example")}}</p> <h2 id="Accessibility_Concerns" name="Accessibility_Concerns">アクセシビリティの考慮事項</h2> <p>視覚的なフォーカスインジケーターが、弱視の人々からも見えるように確認してください。これは外光が明るい場所 (太陽の下の屋外など) で画面を使用するすべての人にも利益になります。 <a href="https://www.w3.org/WAI/WCAG21/Understanding/non-text-contrast.html">WCAG 2.1 SC 1.4.11 Non-Text Contrast</a> は、視覚的なフォーカスインジケーターを少なくとも 3:1 にすることを要求しています。</p> <ul> <li>Accessible Visual Focus Indicators: <a href="https://www.deque.com/blog/give-site-focus-tips-designing-usable-focus-indicators/">Give Your Site Some Focus! Tips for Designing Useful and Usable Focus Indicators</a></li> </ul> <h3 id="focus_outline_none"><code>:focus { outline: none; }</code></h3> <p><a href="https://www.w3.org/WAI/WCAG21/Understanding/non-text-contrast.html">WCAG 2.1 SC 1.4.11 Non-Text Contrast</a> を満たすフォーカスの輪郭線で置き換えることなく、フォーカスの輪郭線 (視覚的なフォーカスインジケーター) を単に削除しないでください。</p> <ul> <li>Quick Tip: <a href="https://a11yproject.com/posts/never-remove-css-outlines/">Never remove CSS outlines</a></li> </ul> <h2 id="Specifications" name="Specifications">仕様書</h2> <table class="standard-table"> <thead> <tr> <th scope="col">仕様書</th> <th scope="col">状態</th> <th scope="col">備考</th> </tr> </thead> <tbody> <tr> <td>{{SpecName("HTML WHATWG", "scripting.html#selector-focus", ":focus")}}</td> <td>{{Spec2("HTML WHATWG")}}</td> <td>HTML に依存した意味論を定義。</td> </tr> <tr> <td>{{SpecName("CSS4 Selectors", "#focus-pseudo", ":focus")}}</td> <td>{{Spec2("CSS4 Selectors")}}</td> <td>変更なし。</td> </tr> <tr> <td>{{SpecName("CSS3 Selectors", "#the-user-action-pseudo-classes-hover-act", ":focus")}}</td> <td>{{Spec2("CSS3 Selectors")}}</td> <td>変更なし。</td> </tr> <tr> <td>{{SpecName("CSS2.1", "selector.html#dynamic-pseudo-classes", ":focus")}}</td> <td>{{Spec2("CSS2.1")}}</td> <td>初回定義。</td> </tr> </tbody> </table> <h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2> <div> <p>{{Compat("css.selectors.focus")}}</p> </div> <h2 id="See_also" name="See_also">関連情報</h2> <ul> <li>{{CSSxRef(":focus-visible")}} {{Experimental_Inline}}</li> <li>{{CSSxRef(":focus-within")}}</li> </ul>