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-understanding-aria-expanded-aria-live-and-common-aria-states/672a5507d857a891139abc7f.md
+179-3Lines changed: 179 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,14 +5,16 @@ challengeType: 19
5
5
dashedName: what-is-the-aria-controls-attribute
6
6
---
7
7
8
-
# --description--
8
+
# --interactive--
9
9
10
10
The `aria-controls` attribute is used to create a programmatic relationship between an element that controls the presence of another element on the page, and the element it controls. This relationship can help screen reader users better understand how the control works. Common uses include a set of tabs that control the visibility of different sections of content, or a button that toggles the visibility of a menu.
11
11
12
-
Let's take a look at an example to see how this works.
13
-
In this example of a tabbed interface, we have a `div` element with a set of three buttons:
12
+
Let's take a look at an example to see how this works. In this example of a tabbed interface, we have a `div` element with a set of three buttons:
@@ -26,13 +28,73 @@ In this example of a tabbed interface, we have a `div` element with a set of thr
26
28
</div>
27
29
```
28
30
31
+
```css
32
+
[role="tablist"] {
33
+
display: flex;
34
+
border-bottom: 2pxsolid#ccc;
35
+
margin-bottom: 1em;
36
+
gap: 4px;
37
+
}
38
+
39
+
[role="tab"] {
40
+
background: #f5f5f5;
41
+
border: 1pxsolid#ccc;
42
+
border-bottom: none;
43
+
border-radius: 4px4px00;
44
+
padding: 8px16px;
45
+
cursor: pointer;
46
+
font-size: 14px;
47
+
color: #333;
48
+
transition: background0.2s, color0.2s;
49
+
}
50
+
51
+
[role="tab"]:hover,
52
+
[role="tab"]:focus {
53
+
background: #e8f0fe;
54
+
outline: none;
55
+
}
56
+
57
+
[role="tab"][aria-selected="true"] {
58
+
background: #fff;
59
+
border-color: #ccc;
60
+
border-bottom: 2pxsolid#fff;
61
+
color: #000;
62
+
font-weight: 600;
63
+
position: relative;
64
+
top: 1px;
65
+
}
66
+
67
+
[role="tab"]:focus-visible {
68
+
outline: 2pxsolid#0078d4;
69
+
outline-offset: 2px;
70
+
}
71
+
72
+
```
73
+
74
+
:::
75
+
29
76
The `div` uses `role="tablist"` to indicate that it serves as a container element for a group of tabs.
30
77
31
78
Each button represents a tab and has a `role` attribute set to `tab`. In most tabbed interfaces, the first tab panel is visible by default, so the first tab button has an `aria-selected` attribute set to `true` to indicate that its associated section of content is currently visible. The `aria-controls` attribute is used to associate each button with the section of content that it controls.
32
79
33
80
Here are the three sections of content that correspond to the tabs:
Each section of content has a `role` attribute set to `tabpanel` and an `aria-labelledby` attribute that points to the corresponding tab to give the panel an accessible name. The `hidden` attribute is used to hide the sections of content that are not currently selected. Each section ID matches the value of the `aria-controls` attribute on the corresponding button. The `section1` ID matches the `aria-controls` attribute on the first button, `section2` matches the `aria-controls` attribute on the second button, and `section3` matches the `aria-controls` attribute on the third button. This is how the association between the tabs and their sections of content is established.
50
226
51
227
To switch between the different sections you will need to use JavaScript to update the `hidden` attribute on the section and the `aria-selected` attribute on the button based on which section is currently visible. Only one section can be visible at a time and it should not have the `hidden` attribute and `aria-selected` should be set to `true` on its button. The remaining hidden sections should all have the `hidden` attribute and `aria-selected` should be set to `false` on their buttons.
0 commit comments