Skip to content

Commit 5cb52d9

Browse files
Alicetabatkins
authored andcommitted
Explain :focus-visible intent, add examples and UA guidance (w3c#2897)
More clearly explain the intent of this pseudo-class and provide better author and UA guidance.
1 parent b270d90 commit 5cb52d9

1 file changed

Lines changed: 78 additions & 49 deletions

File tree

selectors-4/Overview.bs

Lines changed: 78 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2237,62 +2237,91 @@ 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.
2257-
</div>
2243+
2244+
Note: 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.
2248+
2249+
<div class="example">
2250+
In this example,
2251+
all focusable elements get a strong yellow outline on '':focus-visible'',
2252+
and links get both a yellow outline and a yellow background on '':focus-visible''.
2253+
These styles are consistent throughout the page and are easily visible
2254+
due to their bold styling,
2255+
but do not appear unless the user is likely to need to understand
2256+
where page focus is.
2257+
2258+
<pre highlight=css>
2259+
:root {
2260+
focus-gold: #ffbf47;
2261+
}
22582262

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''.
2263+
:focus-visible {
2264+
outline: 3px solid --var(focus-gold);
2265+
}
2266+
2267+
a:focus-visible {
2268+
background-color: --var(focus-gold);
2269+
}
2270+
</pre>
22722271
</div>
22732272

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''.
2273+
User agents can choose their own heuristics for when to match '':focus-visible'';
2274+
however, the following (non-normative) suggestions can be used as a starting point:
2275+
2276+
* If a user has expressed a preference
2277+
(such as via a system preference or a browser setting)
2278+
to always see a visible focus indicator,
2279+
the user agent should honor this
2280+
by having '':focus-visible'' always match on the active element,
2281+
regardless of any other factors.
2282+
(Another option may be for the user agent to show its own focus indicator
2283+
regardless of author styles.)
2284+
* Any element which supports keyboard input
2285+
(such as an <{input}> element,
2286+
or any other element
2287+
which may trigger a virtual keyboard to be shown on focus
2288+
if a physical keyboard is not present)
2289+
should <strong>always</strong> match '':focus-visible'' when focused.
2290+
* If the user interacts with the page via the keyboard,
2291+
the currently focused element should match '':focus-visible''
2292+
(i.e. keyboard usage may change whether this pseudo-class matches
2293+
even if it doesn't affect '':focus'').
2294+
* If the user interacts with the page via a pointing device,
2295+
such that the focus is moved to a new element
2296+
which does <em>not</em> support user input,
2297+
the newly focused element
2298+
should <strong>not</strong> match '':focus-visible''.
2299+
* If the active element matches '':focus-visible'',
2300+
and a script causes focus to move elsewhere,
2301+
the newly focused element should match '':focus-visible''.
2302+
* Conversely, if the active element does not match '':focus-visible'',
2303+
and a script causes focus to move elsewhere,
2304+
the newly focused element should <strong>not</strong> match '':focus-visible''.
2305+
2306+
User agents should also use '':focus-visible'' to specify the default focus style,
2307+
so that authors using '':focus-visible'' will not also need to disable
2308+
the default '':focus'' style.
2309+
</div>
22822310

22832311
<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''.
2312+
In this example,
2313+
authors use a '':not()'' pseudo-class to remove default user agent focus styling
2314+
if '':focus-visible'' is supported,
2315+
and provide enhanced focus styling via '':focus-visible''.
2316+
2317+
<pre highlight=css>
2318+
:focus:not(:focus-visible) {
2319+
outline: 0;
2320+
}
2321+
2322+
:focus-visible {
2323+
outline: 3px solid --var(focus-gold);
2324+
}
22962325
</div>
22972326

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

0 commit comments

Comments
 (0)