WAI-ARIA in a Nutshell
Assistive technologies rely on and take advantage of HTML built using semantic elements appropriate for the content, likenav
, header
, main
, button
, ul
, and dl
—they provide people dependent on assistive technologies the best possible experience. However, even the best markup can't always adequately describe the content and/or its state on a web page. WAI-ARIA allows you to add more meaning to existing HTML markup with roles and their supporting states and properties—there are over 100 combined, including:
- tree
- banner
- group
- aria-live
- aria-expanded
Your HTML Markup Just Got More Meaningful
Consider the HTML markup for a tree made of nested lists and toggle controls. The markup would look something like this (if you already know something about ARIA, pretend you don't):<ul class="tree">
<li class="tree-item parent collapsed">
<ul class="sub-tree">
item 1
<li class="tree-item">item 1 child 1</li>
<li class="tree-item">item 1 child 2</li>
<li class="tree-item">item 1 child 3</li>
</ul>
</li>
<li class="tree-item">item 2</li>
</ul>
There's really nothing wrong with the HTML above. Using
ul
and li
elements instead of a bunch of div
elements is great, because ul
and li
elements should be used to build a list of items. Also, the class names are meaningful. However, classes like tree
, tree-item
, and collapsed
don't follow a spec and won't be consistently used from site to site where tree controls have been implemented—WAI-ARIA can help solve this problem. WAI-ARIA allows you to develop markup that looks like this:
<ul role="tree">
<li role="treeitem" aria-expanded="false" aria-level="1">
<ul role="group">
item 1
<li role="treeitem" aria-level="2">item 1 child 1</li>
<li role="treeitem" aria-level="2">item 1 child 2</li>
<li role="treeitem" aria-level="2">item 1 child 3</li>
</ul>
</li>
<li role="treeitem" aria-level="1">item 2</li>
</ul>
Most of the Classes Are Gone—What About My CSS?
You don't need classes to style your markup. Target your HTML elements, and target the WAI-ARIA roles, states and properties using the attribute selector. For example, you could style the markup above as follows (from my LESS sheet):ul[role="tree"] {
list-style-type: none;
&:focus {
outline:1px dotted #0000ff;
}
li {
list-style-type: none;
&[aria-selected="true"] {
color:#0000ff;
}
&[aria-expanded="false"] > ul {
display:none;
}
&[aria-expanded="true"] > ul {
display:block;
}
}
}