Skip to content

Commit 56f34ad

Browse files
MewenLeHoAniort
andauthored
[Update] Article 'Accessible hiding' (#780)
* [Update] Article 'Accessible hiding' * Update * Update * Update * Update * Update * Update * Update * Update * Update src/fr/articles/masquage-accessible/index.njk Co-authored-by: Vincent Aniort <[email protected]> * Update after review * Fix lang in webography * Update updateDate parameter --------- Co-authored-by: Vincent Aniort <[email protected]>
1 parent f0788a5 commit 56f34ad

File tree

2 files changed

+94
-49
lines changed

2 files changed

+94
-49
lines changed

src/en/articles/accessible-hiding/index.njk

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,57 @@ title: "Accessible hiding and aria-hidden example"
33
abstract: "Elements not visible on the screen but accessible to assistive technology."
44
titleBeforeTag: true
55
date: "2018-01-12"
6+
updateDate: "2026-01-16"
67
tags:
78
- component
89
---
910

1011

1112
<h2>Introduction</h2>
12-
<p>Sometimes it is necessary to hide elements in a web page. The conventional way is to use <abbr title="Cascading Stylesheets">CSS</abbr> (<code>display:none;</code> and <code>visibility:hidden;</code>) or the <abbr>HTML</abbr>5 `hidden` attribute. These properties hide elements not only on the screen, but also for screen reader users. Thus, these elements will not be visible nor vocalized by Assistive technologies (AT).</p>
13+
<p>Sometimes it is necessary to hide elements in a web page. The conventional way is to use <abbr>CSS</abbr> (<code>display:none;</code> and <code>visibility:hidden;</code>) or the <abbr>HTML</abbr>5 <code>hidden</code> attribute. These properties hide elements not only on the screen, but also for screen reader users. Thus, these elements will not be visible nor vocalized by Assistive technologies (AT).</p>
1314
<p>In some cases, it may be useful to hide elements on the screen, but make sure they are still vocalised by screen readers. For this, an accessible hiding <abbr>CSS</abbr> class can be used.</p>
14-
<p>Finally, you may want to display elements on the screen but make sure they are not vocalized. In this case, you can use an <abbr title="Accessible Rich Internet Applications">ARIA</abbr> attribute (<code>aria-hidden</code>).</p>
15+
<p>Finally, you may want to display elements on the screen but make sure they are not vocalized. In this case, you can use <abbr>HTML</abbr>5 <code>inert</code> attribute or an <abbr>ARIA</abbr> attribute (<code>aria-hidden</code>).</p>
1516

1617
<h2>Accessible hiding</h2>
17-
<p>Accessible hiding allows an element to disappear from the screen, while being accessible to screen reader users. Technically, the solution is to add a <abbr>CSS</abbr> class on the element that should not be visible.</p>
18+
<p>Accessible hiding allows to hide an element from the screen, while keeping it available for assistive technologies, especially screen readers. Technically, the solution is to add a <abbr>CSS</abbr> class on the element that should not be visible.</p>
19+
1820
<p>There are several solutions for this type of <abbr>CSS</abbr> class:</p>
1921
<ul>
20-
<li>Setting the elements size to 0.</li>
22+
<li>Setting the element's size to 0.</li>
2123
<li>Positioning the element off the screen (<code>left: -1000px; top: -1000px;</code>).</li>
2224
<li>Using <code>text-indent</code> or <code>clip</code> properties.</li>
2325
<li>…</li>
2426
</ul>
2527
<p>Ideally, it is best to combine all the solutions to make sure it is working properly in all browsers.</p>
2628

2729

28-
<p>If you use a framework, it is very likely it already has a class for that. For example if you use <a href="http://boosted.orange.com">Boosted</a> or <a href="http://getbootstrap.com/">Bootstrap</a>, you can use the <code>visually-hidden</code> <abbr>CSS</abbr> class (“screen reader only”) which is defined as follows:</p>
30+
<p>If you use a framework, it is very likely it already has a class for that. For example if you use <a href="http://boosted.orange.com">Boosted</a> or <a href="http://getbootstrap.com/">Bootstrap</a>, you can use the <code>visually-hidden</code> <abbr>CSS</abbr> class (or <code>sr-only</code> for older versions) which is defined as follows:</p>
31+
2932
<pre><code class="css">
30-
.visually-hidden {
31-
position: absolute;
32-
position: absolute !important;
33-
width: 1px !important;
33+
.visually-hidden,
34+
.visually-hidden-focusable:not(:focus, :focus-within) {
35+
border: 0 !important;
36+
clip-path: inset(50%) !important;
3437
height: 1px !important;
35-
padding: 0 !important;
3638
margin: -1px !important;
3739
overflow: hidden !important;
38-
clip: rect(0,0,0,0) !important;
40+
padding: 0 !important;
41+
width: 1px !important;
3942
white-space: nowrap !important;
40-
border: 0 !important;
43+
}
44+
45+
.visually-hidden:not(caption),
46+
.visually-hidden-focusable:not(caption):not(:focus, :focus-within){
47+
position: absolute !important;
48+
}
49+
50+
.visually-hidden *,
51+
.visually-hidden-focusable:not(:focus, :focus-within) * {
52+
overflow: hidden !important;
4153
}
4254
</code></pre>
43-
<p>If you haven’t got an accessible hiding class yet, the easiest way is to copy and paste the code above.</p>
55+
56+
<p>If you haven't got an accessible hiding class yet, the easiest way is to copy and paste the code above.</p>
4457

4558
<h3>Implementation</h3>
4659
<p>Accessible hiding class can be used in many cases. For example, to clarify a link label:</p>
@@ -49,35 +62,43 @@ tags:
4962
&lt;a href="…"&gt;Learn more&lt;span <span class="important">class="visually-hidden"</span>&gt; about our mobile plans&lt;/span&gt;&lt;/a&gt;
5063
</code></pre>
5164

52-
<strong>Example: </strong>
65+
<strong>Example:</strong>
5366
<p><a href="#">Learn more<span class="visually-hidden"> about our mobile plans</span></a></p>
5467

5568
<p>We would have obtained the same result using the <code>aria-label</code> attribute on the link:</p>
5669
<pre><code class="html">
5770
&lt;a href="…" aria-label="Learn more about our mobile plans"&gt;Learn more&lt;/a&gt;
5871
</code></pre>
59-
<p>The accessible hiding class solution still offers an advantage compared to the <code>aria-label</code> solution. If the <abbr>CSS</abbr> is disabled (replaced with a custom <abbr>CSS</abbr> adapted to a certain disability, for example), the hidden accessibly text will be displayed.</p>
72+
73+
The accessible hiding class solution still offers several advantages compared to the <code>aria-label</code> solution. If the <abbr>CSS</abbr> is disabled (replaced with a custom <abbr>CSS</abbr> adapted to a certain disability, for example), the hidden accessibly text will be displayed. During automatic page translation, the <code>aria-label</code> will not be translated. Furthermore, since <code>aria-label</code> is not very visible in the code, developers may forget to update or modify it.
6074

6175
<h2><code>aria-hidden</code> attribute</h2>
6276
<p>This attribute will hide an element (or group of elements) to screen readers. It has however no effect on the display.</p>
6377

6478
<h3>Implementation</h3>
6579
<p>To hide an element to screen readers (and child elements), simply add the <code>aria-hidden="true"</code> attribute. Content can still be made accessible to ATs via <code>aria-describedby</code> or <code>aria-labelledby</code>.</p>
6680
<h4>Warning</h4>
67-
<p>If you put a focusable element in content (even on a parent node) with `aria-hidden =" true "`, it will be present in keyboard navigation but will be empty for <abbr>AT</abbr>. <em>So, don't do it.</em></p>
81+
<p>If you put a focusable element in content (even on a parent node) with <code>aria-hidden ="true"</code>, it will be present in keyboard navigation but will be empty for <abbr>AT</abbr>. <em>So, don't do it.</em></p>
6882

6983
<strong>Example: </strong>
7084
<p>The following screenshot shows a video player with a number of buttons (previous channel, next channel, pause…). This player is surrounded by two buttons also providing access to previous and next channels.</p>
71-
<p>Visually this is not a problem. Although, when listening to the page with a screen reader, one can find it curious to hear an Animals button vocalized at the beginning of the page, and a “Hunting and Fishing” button vocalized at the end of page.</p>
85+
<p>Visually this is not a problem. Although, when listening to the page with a screen reader, one can find it curious to hear an 'Animals' button vocalized at the beginning of the page, and a “Hunting and Fishing” button vocalized at the end of page.</p>
7286

7387
<img src="../images/player.png" alt="screenshot showing a video player">
7488

75-
<br><br>
76-
7789
<p>To avoid disturbing the user unnecessarily, the easiest way is to hide these buttons (for screen readers) with the <code>aria-hidden</code> attribute.</p>
7890

7991
<pre><code class="html">
8092
&lt;div class="icon icon-arrow-left2" <span class="important">aria-hidden="true"</span>&gt;
8193
&lt;div class="nextPreviousChannelName OrangeMedium" &gt;Animals&lt;/div&gt;
8294
&lt;/div&gt;
8395
</code></pre>
96+
97+
<h2>Webography</h2>
98+
<ul>
99+
<li><a href="https://www.ffoodd.fr/masquage-accessible-de-pointe/" lang="fr" href="fr">Masquage accessible de pointe (fr)</a></li>
100+
<li><a href="https://gist.github.com/ffoodd/000b59f431e3e64e4ce1a24d5bb36034">Improved .visually-hidden</a></li>
101+
<li><a href="https://www.stefanjudis.com/blog/the-inert-attribute-is-finally-coming-to-the-web/">The "inert" attribute is finally coming to the web</a></li>
102+
<li><a href="https://access42.net/attribut-aria-hidden-true-accessibilite/" lang="fr" href="fr">Comment utiliser l'attribut aria-hidden="true" de manière accessible ? (fr)</a></li>
103+
<li><a href="https://adrianroselli.com/2019/11/aria-label-does-not-translate.html">aria-label Does Not Translate</a></li>
104+
</ul>

0 commit comments

Comments
 (0)