From fb18db7f19cbfa77e7ce3063e96829d4bbf68e87 Mon Sep 17 00:00:00 2001
From: Matt King
- Note: This is a draft of a new section.
- Please provide feedback in
- issue 1050.
-
- Providing elements with accessible names, and where appropriate, accessible descriptions is one of the most fundamental and important responsibilities authors have when developing accessible web experiences.
- While doing so is straightforward for most elements, technical mistakes that can have devastating impacts on users of assistive technologies are easy to make and unfortunately common.
- To help authors provide effective and reliable accessible names and descriptions, this section explains their purpose, when authors need to provide them, how browsers assemble them, and rules for coding and composing them.
- It also provides guidance for effectively employing the following naming and describing techniques and WAI-ARIA properties:
+ Providing elements with accessible names, and where appropriate, accessible descriptions is one of the most important responsibilities authors have when developing accessible web experiences.
+ While doing so is straightforward for most elements, technical mistakes that can completely block users of assistive technologies are easy to make and unfortunately common.
+ To help authors effectively provide accessible names and descriptions, this section explains their purpose, when authors need to provide them, how browsers assemble them, and rules for coding and composing them.
+ It also guides authors in the use of the following naming and describing techniques and WAI-ARIA properties:
An accessible description is also an author-provided string that is rendered by assistive technologies.
Authors supply a description when there is a need to associate additional information with an element, such as instructions or format requirements for an input field.
- Because they are usually significantly longer than names, assistive technologies present descriptions differently from names and other element attributes.
- For instance, a screen reader may announce a description of an element after its name, role, value (if any), and relevant states or properties.
+
+ assistive technologies present names differently from descriptions.
+ For instance, screen readers typically announce the name and role of an element first, e.g., a button named
Because there are several elements and attributes for specifying text to include in an accessible name or description string, and because authors can combine them in a practically endless number of ways, browsers implement fairly complex algorithms for assembling the strings.
-The sections on accessible name calculation and accessible description calculation provide detailed explanations of the algorithms.
-However, most authors do not need detailed understanding of the algorithms since nearly all circumstances where a name or description is useful are supported by the coding patterns described in the naming techniques and describing techniques sections.
+The sections on accessible name calculation and accessible description calculation explain the algorithms and how they implement precedence.
+However, most authors do not need such detailed understanding of the algorithms since nearly all circumstances where a name or description is useful are supported by the coding patterns described in the naming techniques and describing techniques sections.
- In HTML documents, whenever possible, rely on HTML naming techniques, such as the HTML Examples
Providing Accessible Names and Descriptions
Providing Accessible Names and Descriptions
@@ -3750,8 +3744,15 @@ aria-describedby
.aria-details
.What ARE Accessible Names and Descriptions?
Mute Conversation
could be spoken as Mute Conversation button
.
+ If an element has a state, it could be announced either before or after the name and role; after name and role is the typical default.
+ For example, a switch button named Mute Conversation
in the off
state could be announced as Mute Conversation switch button off
.
+ Because descriptions are optional strings that are usually significantly longer than names, they are presented last, sometimes after a slight delay.
+ For example, Mute Conversation Switch button off, Silences alerts and notifications about activity in this conversation.
+ To reduce verbosity, some screen readers do not announce descriptions by default but instead inform users of their presence so that users can press a key that will announce the description.
What ARE Accessible Names and Descriptions?
How Are Name and Description Strings Derived?
Rule 2: Prefer Visible Text
Rule 3: Prefer Native Techniques
label
element, alt
attribute for images, caption
element for tables, etc.
+ In HTML documents, whenever possible, rely on HTML naming techniques, such as the HTML label
element for form elements and caption
element for tables.
While less flexible, their simplicity and reliance on visible text help ensure robust accessible experiences.
+ Several of the naming techniques highlight specific accessibility advantages of using HTML features instead of ARIA attributes.
Naming with Child Content
In two special cases, certain descendants are ignored: group
descendants of treeitem
elements and menu
descendants of menuitem
elements are omitted from the calculation.
For example, in the following tree
, the name of the first tree item is Fruits
; Apples
, Bananas
, and Oranges
are omitted.
-<ul role="tree">
+ <ul role="tree">
<li role="treeitem">Fruits
<ul role="group">
<li role="treeitem">Apples</li>
@@ -3873,8 +3874,7 @@ Naming with Child Content
<li role="treeitem">Oranges</li>
</ul>
</li>
-</ul>
-
+</ul>
If an element with one of the above roles that supports naming from child content is named by using aria-label
or aria-labelledby
, content contained in the element and its descendants is hidden from assistive technology users unless the descendant content is referenced by aria-labelledby
.
It is strongly recommended to avoid using either of these attributes to override content of one of the above elements except in rare circumstances where hiding content from assistive technology users is beneficial.
@@ -3898,11 +3898,9 @@
aria-label
aria-label
and the content of the element.
For example, the name of the following navigation region is "Product".
-
- <nav aria-label="Product">
- <!-- list of navigation links to product pages -->
- </nav>
-
+ <nav aria-label="Product">
+ <!-- list of navigation links to product pages -->
+</nav>
When encountering this navigation region, a screen reader user will hear the name and role of the element, e.g., "Product navigation region", and then be able to read through the links contained in the region.
@@ -3923,19 +3921,15 @@aria-labelledby
- <span id="night-mode-label">Night mode</span>
- <span role="switch" aria-checked="false" tabindex="0" aria-labelledby="night-mode-label"></span>
-
+ <span id="night-mode-label">Night mode</span>
+<span role="switch" aria-checked="false" tabindex="0" aria-labelledby="night-mode-label"></span>
Note that while using aria-labelledby
is similar in this situation to using an HTML label
element with the for
attribute, one significant difference is that browsers do not automatically make clicking on the labeling element activate the labeled element; that is an author responsibility.
However, HTML label
cannot be used to label a span
element.
Fortunately, an HTML input
with type="checkbox"
allows the ARIA switch
role, so when feasible, using the following approach creates a more robust solution.
- <label for="night-mode">Night mode</label>
- <input type="checkbox" role="switch" id="night-mode">
-
+ <label for="night-mode">Night mode</label>
+<input type="checkbox" role="switch" id="night-mode">
The aria-labelledby
property is useful in a wide variety of situations because:
It has the highest precedence when browsers calculate accessible names, i.e., it overrides names from child content and all other naming attributes, including aria-label
.
aria-labelledby
An example of referencing a hidden element with aria-labelledby
could be a label for a night switch control:
<span id="night-mode-label" hidden>Night mode</span>
- <input type="checkbox" role="switch" aria-labelledby="night-mode-label">
+<input type="checkbox" role="switch" aria-labelledby="night-mode-label">
In some cases, the most effective name for an element is its own content combined with the content of another element.
Because aria-labelledby
has highest precedence in name calculation, in those situations, it is possible to use aria-labelledby
to reference both the element itself and the other element.
In the following example, the "Read more..." link is named by the element itself and the article’s heading, resulting in a name for the link of "Read more... 7 ways you can help save the bees".
- <h2 id="bees-heading">7 ways you can help save the bees</h2>
- <p>Bees are disappearing rapidly. Here are seven things you can do to help.</p>
- <p><a id="bees-read-more" aria-labelledby="bees-read-more bees-heading">Read more...</a></p>
-
+ <h2 id="bees-heading">7 ways you can help save the bees</h2>
+<p>Bees are disappearing rapidly. Here are seven things you can do to help.</p>
+<p><a id="bees-read-more" aria-labelledby="bees-read-more bees-heading">Read more...</a></p>
When multiple elements are referenced by aria-labelledby
, text content from each referenced element is concatenated in the order specified in the aria-labelledby
value.
If an element is referenced more than one time, only the first reference is processed.
@@ -3990,21 +3982,17 @@
label
element as follows gives the checkbox an accessible name.
-
- <label>
- <input type="checkbox" name="subscribe">
- subscribe to our newsletter
- </label>
-
+ <label>
+ <input type="checkbox" name="subscribe">
+ subscribe to our newsletter
+</label>
A form control can also be associated with a label by using the for
attribute on the label
element.
This allows the label and the form control to be siblings or have different parents in the DOM, but requires adding an id
attribute to the form control, which can be error-prone.
When possible, use the above encapsulation technique for association instead of the following for
attribute technique.
- <input type="checkbox" name="subscribe" id="subscribe_checkbox">
- <label for="subscribe_checkbox">subscribe to our newsletter</label>
-
+ <input type="checkbox" name="subscribe" id="subscribe_checkbox">
+<label for="subscribe_checkbox">subscribe to our newsletter</label>
Using the label
element is an effective technique for satisfying Rule 2: Prefer Visible Text.
It also satisfies Rule 3: Prefer Native Techniques.
@@ -4019,14 +4007,12 @@
fieldset
element can be used to group form controls, and the legend
element can be used to give the group a name.
For example, a group of radio buttons can be grouped together in a fieldset
, where the legend
element labels the group for the radio buttons.
-
- <fieldset>
- <legend>Select your starter class</legend>
- <label><input name="starter-class" value="green"> Green</label>
- <label><input name="starter-class" value="red"> Red</label>
- <label><input name="starter-class" value="blue"> Blue</label>
- </fieldset>
-
+ <fieldset>
+ <legend>Select your starter class</legend>
+ <label><input type="radio" name="starter-class" value="green"> Green</label>
+ <label><input type="radio" name="starter-class" value="red"> Red</label>
+ <label><input type="radio" name="starter-class" value="blue"> Blue</label>
+</fieldset>
This grouping technique is particularly useful for presenting multiple choice questions. It enables authors to associate a question with a group of answers. @@ -4035,27 +4021,32 @@
Similar benefits can be gained from grouping and naming other types of related form fields using fieldset
and legend
.
- <fieldset>
- <legend>Shipping address</legend>
- <p><label>Full name <input name="name" required></label></p>
- <p><label>Address line 1 <input name="address-1" required></label></p>
- <p><label>Address line 2 <input name="address-2"></label></p>
- ...
- </fieldset>
- <fieldset>
- <legend>Billing address</legend>
- ...
- </fieldset>
-
+ <fieldset>
+ <legend>Shipping address</legend>
+ <p><label>Full name <input name="name" required></label></p>
+ <p><label>Address line 1 <input name="address-1" required></label></p>
+ <p><label>Address line 2 <input name="address-2"></label></p>
+ ...
+</fieldset>
+<fieldset>
+ <legend>Billing address</legend>
+ ...
+</fieldset>
Using the legend
element to name a fieldset
element satisfies Rule 2: Prefer Visible Text and Rule 3: Prefer Native Techniques.
When an accessible name is not provided using one of the primary techniques (e.g., the aria-label
or aria-labelledby
attributes), or native markup techniques (e.g., the HTML label
element, or the alt
attribute of the HTML img
element), browsers calculate an accessible name from other attributes as a fallback mechanism.
- It is recommended authors prefer the explicit labeling techniques described above over fallback techniques described in this section.
+ Because the attributes used in fallback name calculation are not intended for naming, they typically yield low quality accessible names that are not effective.
+So, As advised by Rule 4: Avoid Browser Fallback, prefer the explicit labeling techniques described above over fallback techniques described in this section.
@@ -4104,10 +4107,10 @@
For example, a fieldset
element without a legend
element child, but with a title
attribute, gets its accessible name from the title
attribute.
<fieldset title="Select your starter class">
- <label><input name="starter-class" value="green"> Green</label>
- <label><input name="starter-class" value="red"> Red</label>
- <label><input name="starter-class" value="blue"> Blue</label>
- </fieldset>
+ <label><input type="radio" name="starter-class" value="green"> Green</label>
+ <label><input type="radio" name="starter-class" value="red"> Red</label>
+ <label><input type="radio" name="starter-class" value="blue"> Blue</label>
+</fieldset>
For the HTML input
and textarea
elements, the placeholder
attribute is used as a fallback labeling mechanism if nothing else (including the title
attribute) results in a label.
@@ -4951,8 +4954,7 @@
treeitem
role, descendant content of child group
elements are not included.
For example, in the following tree
, the name of the first tree item is Fruits;
Apples,
Bananas, and
Orangesare automatically omitted. -
-<ul role="tree">
+ <ul role="tree">
<li role="treeitem">Fruits
<ul role="group">
<li role="treeitem">Apples</li>
@@ -4960,14 +4962,12 @@ Accessible name calculation
<li role="treeitem">Oranges</li>
</ul>
</li>
-</ul>
-
+</ul>
Similarly, when calculating a name from content for the menuitem
role, descendant content of child menu
elements are not included.
So, the name of the first parent menuitem
in the following menu
is Fruits
.
-<ul role="menu">
+ <ul role="menu">
<li role="menuitem">Fruits
<ul role="menu">
<li role="menuitem">Apples</li>
@@ -4975,8 +4975,7 @@ Accessible name calculation
<li role="menuitem">Oranges</li>
</ul>
</li>
-</ul>
-
+</ul>
In this example, the label for the button is computed by first following the aria-labelledby
reference to the parent element, and then computing the label for that element from the child nodes, first visiting the button
element again but ignoring the aria-labelledby
reference and instead using the aria-label
, and then visiting the next child (the text node). The resulting label is Remove meeting: Daily status report
.
- <div id="meeting-1">
- <button aria-labelledby="meeting-1" aria-label="Remove meeting:">X</button>
- Daily status report
- </div>
-
+ <div id="meeting-1">
+ <button aria-labelledby="meeting-1" aria-label="Remove meeting:">X</button>
+ Daily status report
+</div>
aria-describedby
- <button aria-describedby="trash-desc">Move to trash</button>
- ...
- <p id="trash-desc">Items in the trash will be permanently removed after 30 days.</p>
-
+ <button aria-describedby="trash-desc">Move to trash</button>
+...
+<p id="trash-desc">Items in the trash will be permanently removed after 30 days.</p>
Descriptions are reduced to text strings. For example, if the description contains an HTML img
element, a text equivalent of the image is computed.
- <button aria-describedby="trash-desc"> Move to <img src="bin.svg" alt="trash"></button>
- ...
- <p id="trash-desc">Items in <img src="bin.svg" alt="the trash"> will be permanently removed after 30 days.</p>
-
+ <button aria-describedby="trash-desc"> Move to <img src="bin.svg" alt="trash"></button>
+...
+<p id="trash-desc">Items in <img src="bin.svg" alt="the trash"> will be permanently removed after 30 days.</p>
As with aria-labelledby
, it is possible to reference an element using aria-describedby
even if that element is hidden.
@@ -5078,56 +5071,28 @@
aria-describedby
input
element is Your username is the name that you use to log in to this service.-
- <label for="username">Username</label>
- <input id="username" name="username" aria-describedby="username-desc">
- <button aria-expanded="false" aria-controls="username-desc" aria-label="Help about username">?</button>
- <p id="username-desc" hidden>
- Your username is the name that you use to log in to this service.
- </p>
-
-
-
- aria-details
- In some cases, a plain text description is insufficient. - The aria-details property can be used in such situations. - In the following example, a text field for a passenger’s name (when booking a flight) has a description that is a list of three items, and contains a link to an external document with further details. -
- -
- <ul id="full-name-desc">
- <li>The passenger's name must match the name in their passport.</li>
- <li>The name must consist of only characters in the A-Z range.</li>
- <li><a href="faq.html#name">What if the name in the passport contains other characters?</a></li>
- </ul>
- <fieldset>
- <legend>Passenger 1 (adult)</legend>
- <p>
- <label>Full name <input name="full-name" aria-details="full-name-desc"></label>
- </p>
- ...
- </fieldset>
-
-
-
- If both aria-details
and aria-describedby
are specified on an element, browsers ignore the aria-describedby
value when calculating the accessible description.
- Specifying both is potentially useful if there is a need to provide a fallback description for browsers and assistive technologies that do not yet support aria-details
.
-
<label for="username">Username</label>
+<input id="username" name="username" aria-describedby="username-desc">
+<button aria-expanded="false" aria-controls="username-desc" aria-label="Help about username">?</button>
+<p id="username-desc" hidden>
+ Your username is the name that you use to log in to this service.
+</p>
Like the accessible name calculation, the accessible description calculation produces a text string.
- However, there is one exception: when an element is described using the aria-details
attribute the assistive technology is provided with a reference instead of constructing a flattened string.
The accessible description calculation algorithm is the same as the accessible name calculation algorithm except for a few branch points that depend on whether a name or description is being calculated.
In particular, when accumulating text for an accessible description, the algorithm uses aria-describedby
instead of aria-labelledby
.
- When aria-details
is not used, user agents construct an accessible description string for an element by walking through a list of potential description methods and using the first that generates a description.
+ User agents construct an accessible description string for an element by walking through a list of potential description methods and using the first that generates a description.
The algorithm they follow is defined in the accessible name specification.
It is roughly like the following: