Skip to content

Commit

Permalink
Add sufficient technique for 2.4.7 using :focus-visible (#3272)
Browse files Browse the repository at this point in the history
Two years late, but: here's the technique I intended to write for
#301 (comment)

(but a recent lengthy discussion on mailing list reminded me that back
in #301 the WG had indeed agreed this interpretation)

[Preview of new
techinque](https://raw.githack.com/w3c/wcag/patrickhlauke-focus-visible-technique/techniques/css/C45.html)

---------

Co-authored-by: Alastair Campbell <ac@alastc.com>
Co-authored-by: Mike Gower <mikegower@gmail.com>
  • Loading branch information
3 people committed May 14, 2024
1 parent ce91353 commit a2b2f22
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 27 deletions.
9 changes: 9 additions & 0 deletions techniques/css/C15.html
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ <h3>Expected Results</h3>
</ul>
</section>
</section>
<section id="related">
<h2>Related Techniques</h2>
<ul>
<li><a href="../general/G165">G165</a></li>
<li><a href="../general/G195">G195</a></li>
<li><a href="C40">C40</a></li>
<li><a href="C45">C45</a></li>
</ul>
</section>
<section id="resources">
<h2>Resources</h2>
<ul>
Expand Down
3 changes: 3 additions & 0 deletions techniques/css/C40.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ <h2>Related Techniques</h2>
<ul>
<li><a href="../general/G149">G149</a></li>
<li><a href="../general/G195">G195</a></li>
<li><a href="C15">C15</a></li>
<li><a href="../general/G165">G165</a></li>
<li><a href="C45">C45</a></li>
</ul>
</section>
<section id="resources">
Expand Down
98 changes: 98 additions & 0 deletions techniques/css/C45.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<title>Using CSS :focus-visible to provide keyboard focus indication</title>
<link rel="stylesheet" type="text/css" href="../../css/editors.css"/>
</head>
<body>
<h1>Using <abbr title="Cascading Style Sheets">CSS</abbr> <code class="language-css">:focus-visible</code> to provide keyboard focus indication</h1>
<section id="meta">
<h2>Metadata</h2>
<p id="id"></p>
<p id="technology">CSS</p>
<p id="type"></p>
</section>
<section id="applicability">
<h2>When to Use</h2>
<p>All technologies that support CSS.</p>
</section>
<section id="description">
<h2>Description</h2>
<p>The objective of this technique is to provide custom styles for interactive elements when they receive keyboard focus. In this example, this is achieved using the CSS <code class="language-css">:focus-visible</code> pseudo-class.</p>
<p>Styles defined with the regular <code class="language-css">:focus</code> pseudo-class are applied whenever an element has focus, regardless of how it received focus. In contrast, user agents apply additional heuristics and logic to decide when to show <code class="language-css">:focus-visible</code> styles – in particular, browsers always show these styles when a user is navigating using the keyboard, but will generally <em>not</em> show them as a result of a mouse/pointer interaction (with the exception of elements that also support keyboard input, such as <code>&lt;input&gt;</code> elements). This allows authors to provide strong visible focus indication for keyboard users, without forcing them on mouse/pointer users who may not necessarily want or need them (for instance, because they find them visually distracting).</p>
<p>This satisfies the requirement of providing a <q>mode of operation where the keyboard focus indicator is visible</q>.</p>
<div class="note">
<p>This technique is only sufficient <em>if</em> it uses styles that provide a <em>visible</em> focus indicator.</p>
</div>
<div class="note">
<p>There may be situations where mouse/pointer users could also benefit from having a visible focus indicator, even though they did not set focus to an element using the keyboard. As a best practice, consider still providing an explicit <code class="language-css">:focus</code> style for these cases.</p>
</div>
</section>
<section id="examples">
<h2>Examples</h2>
<section class="example">
<h3>Using <abbr title="Cascading Style Sheets">CSS</abbr> <code class="language-css">:focus-visible</code> to provide keyboard focus indication</h3>
<p>In this example, we have a standard <code>&lt;button&gt;</code> control in the HTML.</p>
<p>By default, in all modern browsers, this button does not show any focus indication when activated using a mouse/pointer. Browsers show their default focus outline/indication when a user sets focus on the button using <kbd>Tab</kbd>.</p>
<p>In order to make this focus indication more prominent, we use the <code class="language-css">:focus-visible</code> pseudo-class selector to define a more intentional and pronounced focus style.</p>
<p>Working example: <a href="../../working-examples/css-focus-visible/">Using CSS <code class="language-css">:focus-visible</code> to provide keyboard focus indication</a>.</p>
<pre><code>&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;meta charset="utf-8"&gt;
&lt;meta name="viewport" content="width=device-width,
initial-scale=1, shrink-to-fit=no"&gt;
&lt;title&gt;Using CSS :focus-visible to
provide keyboard focus indication&lt;/title&gt;
&lt;style&gt;
/* Specific :focus-visible styles */

:focus-visible {
outline: 5px solid red;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;button&gt;Example button&lt;/button&gt;
&lt;/body&gt;
&lt;/html&gt;</code></pre>
</section>
</section>
<section id="tests">
<h2>Tests</h2>
<section class="test-procedure">
<h3>Procedure</h3>
<p>For each user interface component that can receive keyboard focus:</p>
<ol>
<li>Set focus to the interface component using the keyboard (generally, navigating to the component using <kbd>Tab</kbd>/<kbd>Shift+Tab</kbd>)</li>
<li>Verify that once the component has received focus, a focus indicator is visible</li>
</ol>
</section>
<section class="test-results">
<h3>Expected Results</h3>
<ul>
<li>Check 2 is true.</li>
</ul>
</section>
</section>
<section id="related">
<h2>Related Techniques</h2>
<ol>
<li><a href="C15">C15</a></li>
<li><a href="../general/G165">G165</a></li>
<li><a href="../general/G195">G195</a></li>
<li><a href="C40">C40</a></li>
</ol>
</section>
<section id="resources">
<h2>Resources</h2>
<ol>
<li><a href="https://www.w3.org/TR/selectors-4/#the-focus-visible-pseudo">CSS Selectors Level 4 - <code class="language-css">:focus-visible</code> pseudo-class</a>.</li>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:focus-visible"><abbr title="Mozilla Developer Network">MDN</abbr> - <code class="language-css">:focus-visible</code> pseudo-class</a>.</li>
</ol>
</section>
</body>
</html>
6 changes: 5 additions & 1 deletion techniques/general/G165.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@
</ul>
</section>
</section><section id="related"><h2>Related Techniques</h2><ul>
<li><a href="../general/G149">G149</a></li>
<li><a href="G149">G149</a></li>
<li><a href="../css/C15">C15</a></li>
<li><a href="../client-side-script/SCR31">SCR31</a></li>
<li><a href="../css/C15">C15</a></li>
<li><a href="G195">G195</a></li>
<li><a href="../css/C40">C40</a></li>
<li><a href="../css/C45">C45</a></li>
</ul></section></body></html>
6 changes: 4 additions & 2 deletions techniques/general/G195.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ <h3>Menus</h3>
</ul>
</section>
</section><section id="related"><h2>Related Techniques</h2><ul>
<li><a href="../general/G149">G149</a></li>
<li><a href="../general/G165">G165</a></li>
<li><a href="G149">G149</a></li>
<li><a href="G165">G165</a></li>
<li><a href="../css/C15">C15</a></li>
<li><a href="../client-side-script/SCR31">SCR31</a></li>
<li><a href="../C40">C40</a></li>
<li><a href="../C45">C45</a></li>
</ul></section></body></html>
39 changes: 15 additions & 24 deletions understanding/20/focus-visible.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ <h2>Intent of Focus Visible</h2>
</p>

<p>“Mode of operation” accounts for user agents which may not always show a focus indicator, or only show the focus indicator when the keyboard is used. User agents may optimise when the focus indicator is shown, such as only showing it when a keyboard is used. Authors are responsible for providing at least one mode of operation where the focus is visible. In most cases there is only one mode of operation so this success criterion applies. The focus indicator must not be time limited, when the keyboard focus is shown it must remain.</p>

<div class="note">
<p>There may be situations where mouse/pointer users could also benefit from having a visible focus indicator, even though they did not set focus to an element using the keyboard. As a best practice, consider still providing an explicit focus indicator for these cases.</p>
</div>

<p>Note that a keyboard focus indicator can take different forms.<span class="wcag22"> While Focus Visible does not specify what that form is, <a href="focus-appearance">2.4.13 Focus Appearance (Level AAA)</a> provides guidance on creating a consistent, visible indicator.</span></p>
<p>Note that a keyboard focus indicator can take different forms. <span class="wcag22">While Focus Visible does not specify what that form is, <a href="focus-appearance">2.4.13 Focus Appearance (Level AAA)</a> provides guidance on creating a consistent, visible indicator.</span></p>

</section>
<section id="benefits">
Expand Down Expand Up @@ -83,52 +87,46 @@ <h2>Techniques for Focus Visible</h2>

<section id="sufficient">
<h3>Sufficient Techniques for Focus Visible</h3>


<ul>

<li>


<a href="../Techniques/general/G149" class="general">Using user interface components that are highlighted by the user agent when they receive
focus
</a>

</li>

<li>


<a href="../Techniques/css/C15" class="css">Using CSS to change the presentation of a user interface component when it receives
focus
</a>

</li>

<li>


<a href="../Techniques/general/G165" class="general">G165: Using the default focus indicator for the platform so that high visibility default
focus indicators will carry over
</a>

</li>

<li>


<a href="../Techniques/general/G195" class="general">Using an author-supplied, highly visible focus indicator</a>

</li>

</li>
<li>
<a href="../../techniques/css/C40">Creating a two-color focus indicator to ensure sufficient contrast</a>
</li>

<li>

<a href="../../techniques/css/C45">Using CSS :focus-visible to provide keyboard focus indication</a>
</li>
<li>
<a href="../Techniques/client-side-script/SCR31" class="script">Using script to change the background color or border of the element with focus</a>

</li>

</ul>

</section>

<section id="advisory">
Expand All @@ -138,28 +136,21 @@ <h3>Additional Techniques (Advisory) for Focus Visible</h3>

<section id="failure">
<h3>Failures for Focus Visible</h3>


<ul>

<li>


<a href="../Techniques/failures/F55" class="failure">Failure of Success Criterion 2.1.1, 2.4.7, and 3.2.1 due to using script to remove
focus when focus is received
</a>

</li>

<li>

<a href="../Techniques/failures/F78" class="failure">Failure due to styling element outlines and borders in a way that overrides or renders
non-visible the default visual focus indicator
</a>

</li>

</ul>

</section>

</section>
Expand Down
44 changes: 44 additions & 0 deletions working-examples/css-focus-visible/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Using CSS :focus-visible to provide keyboard focus indication</title>
<style>

/* Specific :focus-visible styles */

:focus-visible {
outline: 5px solid red;
}

/* General styles for the example */

body {
font-family: Noto Sans,Trebuchet MS,Helvetica Neue,Arial,sans-serif;
background-color: #fafafc;
color: #222;
margin: 0;
padding: 1em;
}

pre {
background-color: #f0f0f0;
border: 1px solid #888;
padding: 0.5em;
}

</style>
</head>

<body>
<h1>Using <abbr title="Cascading Style Sheets">CSS</abbr> <code class="language-css">:focus-visible</code> to provide keyboard focus indication</h1>
<p>In this example, we have a standard <code>&lt;button&gt;</code> control in the HTML.</p>

<button>Example button</button>

<p>By default, in all modern browsers, this button does not show any focus indication when activated using a mouse/pointer. Browsers show their default focus outline/indication when a user sets focus on the button using <kbd>Tab</kbd>.</p>
<p>In order to make this focus indication more prominent, we use the <code class="language-css">:focus-visible</code> pseudo-class selector to define a more intentional and pronounced focus style.</p>
<pre><code>:focus-visible { outline: 5px solid red; }</code></pre>
</body>
</html>

0 comments on commit a2b2f22

Please sign in to comment.