You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: curriculum/challenges/english/blocks/lecture-introduction-to-aria/672a549231b8728f7171ed9d.md
+347-2Lines changed: 347 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,9 @@ challengeType: 19
5
5
dashedName: what-are-aria-roles
6
6
---
7
7
8
-
# --description--
8
+
# --interactive--
9
+
10
+
**NOTE**: Some of these interactive examples might include CSS and JavaScript code. Don't worry about trying to understand the code. The previews are there to help illustrate some of the main points discussed in the lesson. To see the previews, you will need to enable the interactive editor.
9
11
10
12
ARIA stands for Accessible Rich Internet Applications.
11
13
@@ -15,11 +17,57 @@ HTML has semantic and non-semantic elements, based on whether they convey meanin
15
17
16
18
Many semantic HTML elements already have an ARIA role assigned by default. For example, the `button` element has a default ARIA role of `button`.
17
19
20
+
:::interactive_editor
21
+
22
+
```html
23
+
<button>Example button</button>
24
+
```
25
+
26
+
:::
27
+
18
28
But non-semantic elements don't have a role. For example, screen readers will not know how to interpret the purpose of a `div` if you don't specify its role explicitly.
19
29
30
+
```html
31
+
<div>
32
+
<!-- more elements go here -->
33
+
</div>
34
+
```
35
+
20
36
To specify the ARIA role of an element, you just need to add the `role` attribute, like this `role="ARIA role"`, where value is the name of a role in the ARIA specification.
21
37
22
-
It is important to note that specifying a role on an element only does one thing: It informs assistive technology of the purpose of the element. It does not add any functionality or behavior to the element. If people expect a role to behave a certain way, it is up to you, the developer, to add that expected behavior. For example, adding a `role` of `button` to a `div` does not automatically make it clickable with a mouse or usable with a keyboard. It is the responsibility of the developer to add the expected behavior that allows the `div` to act like a button, and in most cases, it is just better to use the `button` element.
38
+
Here is an example of using the `role` attribute with a value of `"alert"`:
39
+
40
+
:::interactive_editor
41
+
42
+
```html
43
+
<linkrel="stylesheet"href="styles.css">
44
+
45
+
<divclass="alert"id="exp-warning"role="alert">
46
+
<spanclass="hidden">Your log in session will expire in 3 minutes.</span>
47
+
</div>
48
+
```
49
+
50
+
```css
51
+
.alert {
52
+
background-color: #fff3cd;
53
+
border: 1pxsolid#ffeeba;
54
+
color: #856404;
55
+
padding: 1em;
56
+
margin-top: 1em;
57
+
border-radius: 4px;
58
+
font-weight: bold;
59
+
}
60
+
61
+
.alertspan {
62
+
display: inline-block;
63
+
}
64
+
```
65
+
66
+
:::
67
+
68
+
It is important to note that specifying a role on an element only does one thing: It informs assistive technology of the purpose of the element. It does not add any functionality or behavior to the element. If people expect a role to behave a certain way, it is up to you, the developer, to add that expected behavior.
69
+
70
+
For example, adding a `role` of `button` to a `div` does not automatically make it clickable with a mouse or usable with a keyboard. It is the responsibility of the developer to add the expected behavior that allows the `div` to act like a button, and in most cases, it is just better to use the `button` element.
23
71
24
72
There are six main categories of ARIA roles:
25
73
@@ -40,26 +88,323 @@ You should specify the roles that don't have an equivalent semantic element. For
40
88
41
89
There are other similar roles but these are the most commonly used ones. This is an example of a `div` with the `math` ARIA role. The `div` contains a mathematical equation.
42
90
91
+
:::interactive_editor
92
+
43
93
```html
94
+
<linkrel="stylesheet"href="styles.css">
95
+
44
96
<divrole="math"aria-label="x squared + y squared = 3">
45
97
x<sup>2</sup> + y<sup>2</sup> = 3
46
98
</div>
47
99
```
48
100
101
+
```css
102
+
.math-expression {
103
+
font-family: "Times New Roman", serif;
104
+
font-size: 1.2rem;
105
+
background-color: #f4f4f4;
106
+
border-left: 4pxsolid#007acc;
107
+
padding: 0.75em1em;
108
+
margin-top: 1em;
109
+
display: inline-block;
110
+
}
111
+
112
+
.math-expressionsup {
113
+
font-size: 0.8em;
114
+
}
115
+
```
116
+
117
+
:::
118
+
49
119
You'll also notice that the `div` has an `aria-label` attribute. The value of this attribute should be a string that represents the expression.
50
120
51
121
Widget roles define the purpose and functionality of interactive elements, like scrollbars.
52
122
53
123
Examples of widget roles include `scrollbar`, `searchbox`, `separator` (when focusable), `slider`, `spinbutton`, `switch`, `tab`, `tabpanel`, and `treeitem`.
Some of these roles have equivalent semantic elements. You should prioritize the semantic element over the role if there is one. For example, you should favor using the HTML `button` element over adding a `role` of `button` to a `div`.
56
195
57
196
Landmark roles classify and label the primary sections of a web page. Screen readers use them to provide convenient navigation to important sections of a page. You should use them sparingly to keep the overall layout simple and easy to understand. Examples of landmark roles are `banner`, `complementary`, `contentinfo`, `form`, `main`, `navigation`, `region`, and `search`. Each of these roles has a corresponding HTML equivalent, such as `header`, `footer`, `aside`, `form`, `main`, `nav`, `section`, and `search`. If you use the proper HTML elements to define the sections of your page then it is not necessary to explicitly add the `role` attribute to these elements.
58
197
198
+
Here is an example of a banner:
199
+
200
+
:::interactive_editor
201
+
202
+
```html
203
+
<linkrel="stylesheet"href="styles.css">
204
+
205
+
206
+
<divrole="banner"class="site-banner">
207
+
<h1>Accessible Web Design</h1>
208
+
<nav>
209
+
<ul>
210
+
<li><ahref="#">Home</a></li>
211
+
<li><ahref="#">Articles</a></li>
212
+
<li><ahref="#">About</a></li>
213
+
<li><ahref="#">Contact</a></li>
214
+
</ul>
215
+
</nav>
216
+
</div>
217
+
```
218
+
219
+
```css
220
+
.site-banner {
221
+
background-color: #007acc;
222
+
color: #fff;
223
+
padding: 1em1.5em;
224
+
border-radius: 4px;
225
+
}
226
+
227
+
.site-bannerh1 {
228
+
margin: 000.5em;
229
+
font-size: 1.5em;
230
+
}
231
+
232
+
.site-bannernavul {
233
+
list-style: none;
234
+
margin: 0;
235
+
padding: 0;
236
+
display: flex;
237
+
gap: 1em;
238
+
}
239
+
240
+
.site-bannernava {
241
+
color: #fff;
242
+
text-decoration: none;
243
+
font-weight: 500;
244
+
}
245
+
246
+
.site-bannernava:hover {
247
+
text-decoration: underline;
248
+
}
249
+
```
250
+
251
+
:::
252
+
59
253
Live region roles define elements with content that will change dynamically. This way, screen readers and other assistive technologies can announce changes to users with visual disabilities. These roles include: `alert`, `log`, `marquee`, `status`, and `timer`.
statusMessage.textContent="Your upload has completed successfully.";
306
+
});
307
+
```
308
+
309
+
:::
310
+
61
311
Window roles define sub-windows, like pop up modal dialogues. These roles include `alertdialog` and `dialog`. Please note that it is now considered a best practice to use the HTML `dialog` element and its associated JavaScript methods instead of manually creating a dialog.
62
312
313
+
Here is an example of using a `dialog` role for a custom dialog:
And finally, we have abstract roles. These roles help organize the document. They're only meant to be used internally by the browser, not by developers, so you should know that they exist but you shouldn't use them on your websites or web applications.
64
409
65
410
With ARIA roles, you can create accessible and inclusive websites and web applications. They provide semantic information about the purpose and function of HTML elements.
0 commit comments