aboutsummaryrefslogtreecommitdiff
path: root/files/tr/web/css/_colon_focus/index.html
blob: 05ebc35eadda6a2ab32674537411650453ca5d3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
---
title: ':focus'
slug: 'Web/CSS/:focus'
tags:
  - ':focus'
  - CSS
  - Layout
  - Pseudo-class
  - Reference
  - Selector
  - Web
translation_of: 'Web/CSS/:focus'
---
<div>{{CSSRef}}</div>

<p>The <strong><code>:focus</code></strong> <a href="/en-US/docs/Web/CSS">CSS</a> <a href="/en-US/docs/Web/CSS/Pseudo-classes" title="Pseudo-classes">pseudo-class</a> represents an element (such as a form input) that has received focus. It is generally triggered when the user clicks or taps on an element or selects it with the keyboard's <kbd>Tab</kbd> key.</p>

<pre class="brush: css no-line-numbers notranslate">/* Selects any &lt;input&gt; when focused */
input:focus {
  color: red;
}</pre>

<div class="blockIndicator note">
<p><strong>Note:</strong> This pseudo-class applies only to the focused element itself. Use {{CSSxRef(":focus-within")}} if you want to select an element that <em>contains</em> a focused element.</p>
</div>

<h2 id="Syntax">Syntax</h2>

{{CSSSyntax}}

<h2 id="Examples">Examples</h2>

<h3 id="HTML">HTML</h3>

<pre class="brush: html no-line-numbers notranslate">&lt;input class="red-input" value="I'll be red when focused."&gt;&lt;br&gt;
&lt;input class="blue-input" value="I'll be blue when focused."&gt;</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">Result</h3>

<p>{{EmbedLiveSample("Examples")}}</p>

<h2 id="Accessibility_concerns">Accessibility concerns</h2>

<p>Make sure the visual focus indicator can be seen by people with low vision.  This will also benefit anyone use a screen in a brightly lit space (like outside in the sun). <a href="https://www.w3.org/WAI/WCAG21/Understanding/non-text-contrast.html">WCAG 2.1 SC 1.4.11 Non-Text Contrast</a> requires that the visual focus indicator be at least 3 to 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>Never just remove the focus outline (visible focus indicator) without replacing it with a focus outline that will pass <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">Specifications</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName("HTML WHATWG", "scripting.html#selector-focus", ":focus")}}</td>
   <td>{{Spec2("HTML WHATWG")}}</td>
   <td>Defines HTML-specific semantics.</td>
  </tr>
  <tr>
   <td>{{SpecName("CSS4 Selectors", "#focus-pseudo", ":focus")}}</td>
   <td>{{Spec2("CSS4 Selectors")}}</td>
   <td>No change.</td>
  </tr>
  <tr>
   <td>{{SpecName("CSS3 Selectors", "#the-user-action-pseudo-classes-hover-act", ":focus")}}</td>
   <td>{{Spec2("CSS3 Selectors")}}</td>
   <td>No change.</td>
  </tr>
  <tr>
   <td>{{SpecName("CSS2.1", "selector.html#dynamic-pseudo-classes", ":focus")}}</td>
   <td>{{Spec2("CSS2.1")}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<div>


<p>{{Compat("css.selectors.focus")}}</p>
</div>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{CSSxRef(":focus-visible")}} {{Experimental_Inline}}</li>
 <li>{{CSSxRef(":focus-within")}}</li>
</ul>