Skip to content

Commit c163ec2

Browse files
author
Alice Boxhall
committed
Explain :focus-visible intent, add examples and UA guidance
1 parent ec5e7c9 commit c163ec2

File tree

1 file changed

+70
-48
lines changed

1 file changed

+70
-48
lines changed

selectors-4/Overview.bs

Lines changed: 70 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2237,62 +2237,84 @@ The Focus-Indicated Pseudo-class: '':focus-visible''</h3>
22372237

22382238
The <dfn id='focus-visible-pseudo'>:focus-visible</dfn> pseudo-class applies
22392239
while an element matches the '':focus'' pseudo-class
2240-
<em>and</em> the UA determines via heuristics
2240+
<em>and</em> the user agent determines via heuristics
22412241
that the focus should be made evident on the element.
2242-
(Many browsers show a “focus ring” by default in this case.)
22432242

2244-
<div class=example>
2245-
For example, UAs typically display focus indicators on text fields
2246-
any time they're focused,
2247-
to draw attention to the fact that keyboard input will affect their contents.
2248-
2249-
On the other hand, UAs typically only display focus indicators on buttons
2250-
when they were focused by a keyboard interaction
2251-
(such as tabbing through the document)--
2252-
because it's not always immediately obvious
2253-
where the focus has gone after such an interaction,
2254-
but is sufficiently self-evident when the button was focused
2255-
by more obviously-targetted interactions,
2256-
like clicking on the button with a mouse pointer.
2243+
<div class=note>
2244+
The intent of '':focus-visible'' is
2245+
to allow authors to provide <em>clearly identifiable</em> focus styles
2246+
which are visible when a user is likely to need to understand where focus is,
2247+
and not visible in other cases.
22572248
</div>
22582249

2259-
<div class=advisement>
2260-
Page authors should follow these guidelines
2261-
when deciding whether to use '':focus'' or '':focus-visible''
2262-
to style the focused state of an element:
2263-
2264-
* If the element has “native” focus indicator behavior
2265-
(such as text fields or buttons),
2266-
use '':focus-visible''.
2267-
* Otherwise, if the element is emulating a text input,
2268-
or something else that is <em>intended</em> to receive keyboard interaction,
2269-
use '':focus''.
2270-
* Otherwise,
2271-
use '':focus-visible''.
2250+
<div class="example">
2251+
In this example,
2252+
all focusable elements get a strong yellow outline on '':focus-visible'',
2253+
and links get both a yellow outline and a yellow background on '':focus-visible''.
2254+
These styles are consistent throughout the page and are easily visible
2255+
due to their bold styling,
2256+
but do not appear unless the user is likely to need to understand
2257+
where page focus is.
2258+
2259+
<pre>
2260+
:root {
2261+
focus-gold: #ffbf47;
2262+
}
2263+
2264+
:focus-visible {
2265+
outline: 3px solid --var(focus-gold);
2266+
}
2267+
2268+
a:focus-visible {
2269+
background-color: --var(focus-gold);
2270+
}
2271+
</pre>
22722272
</div>
22732273

2274-
When UAs choose to specially indicate focus on an element,
2275-
or whether they specially indicate focus at all,
2276-
is UA-dependent.
2277-
Different UAs,
2278-
the same UA on different operating systems,
2279-
or the same UA on the same OS,
2280-
but with different user settings,
2281-
can make different choices as to when an element matches '':focus-visible''.
2274+
User agents may choose their own heuristics for when to match '':focus-visible'';
2275+
however, the following <em>may</em> be used as a starting point:
2276+
<ul>
2277+
<li>Any element which supports user input
2278+
(such as an ''&lt;input&gt;'' element,
2279+
or editing host)
2280+
should <strong>always</strong> match '':focus-visible'' when focused.</li>
2281+
<li>If the user interacts with the page via the keyboard,
2282+
the currently focused element should match '':focus-visible''
2283+
(i.e. keyboard usage may change whether this pseudo-class matches
2284+
even if it doesn't affect '':focus'').
2285+
<li>If the user interacts with the page via a pointing device,
2286+
such that the focus is moved to a new element
2287+
which does <em>not</em> support user input,
2288+
the newly focused element
2289+
should <strong>not</strong> not match '':focus-visible''.</li>
2290+
<li>If the active element matches '':focus-visible'',
2291+
and a script causes focus to move elsewhere,
2292+
the newly focused element should match '':focus-visible''.</li>
2293+
<li>Conversely, if the active element does not match '':focus-visible'',
2294+
and a script causes focus to move elsewhere,
2295+
the newly focused element should <strong>not</strong> match '':focus-visible''.</li>
2296+
</ul>
2297+
2298+
<div class=note>
2299+
User agents should also use '':focus-visible'' to specify the default focus style,
2300+
so that authors using '':focus-visible'' will not also need to disable
2301+
the default '':focus'' style.
2302+
</div>
22822303

22832304
<div class=example>
2284-
The following guidelines are suggested heuristics
2285-
for choosing when to apply '':focus-visible''
2286-
to elements without “native” focus indicator behavior:
2287-
2288-
* If the element received focus via a keyboard interaction,
2289-
including indirectly,
2290-
such as triggering a dialog
2291-
by pressing a button using the keyboard,
2292-
apply '':focus-visible''.
2293-
* If a keyboard event occurs while an element is focused,
2294-
even if the element wasn't focused by a keyboard interaction,
2295-
apply '':focus-visible''.
2305+
In this example,
2306+
authors use a '':not'' pseudo-selector to remove default user agent focus styling
2307+
if '':focus-visible'' is supported,
2308+
and provide enhanced focus styling via '':focus-visible''.
2309+
2310+
<pre>
2311+
:focus:not(:focus-visible) {
2312+
outline: 0;
2313+
}
2314+
2315+
:focus-visible {
2316+
outline: 3px solid --var(focus-gold);
2317+
}
22962318
</div>
22972319

22982320
<h3 id="the-focus-within-pseudo">

0 commit comments

Comments
 (0)