HTML5 Intermediate Semantic Structure · 9 min read

ARIA Basics: Roles, Labels, and When Not to Use Them

Lesson 6 of 36 in the HTML5 learning path.

When should I add ARIA, and when is native HTML enough?

ARIA is a set of attributes that expose roles, names, and state to assistive technology when native HTML cannot say what a control is doing. A button already has a role. A nav already has a landmark. Adding role="button" or role="navigation" on top of those elements repeats information and can hide the built-in behavior you wanted. Useful cases are a name for a second nav, a pressed state, or a status line script will update.

Add ARIA after the HTML is as semantic as you can make it, and only for the gap that remains. Use it on a widget you had to build, such as a toggle that is still a real button, or a live message with no dedicated element. Do not use ARIA to repair a div that should have been a button. Native elements plus a few attributes are enough on a course page.

Syntax & Parameters

Syntax
<nav aria-label="Primary"></nav><button type="button" aria-pressed="false"></button><p role="status"></p>

Example

Interactive preview available
widget.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Pin a lesson</title>
  <style>
    body { margin: 0; font: 1.05rem/1.55 Georgia, "Times New Roman", serif; color: #1c1917; background: #faf7f2; }
    header { background: #1e3a5f; color: #f8fafc; padding: 1rem 1.5rem; }
    header a, .on-page a { color: #1d4ed8; margin-right: 1rem; }
    header a { color: #bfdbfe; }
    main { max-width: 42rem; margin: 0 auto; padding: 1.25rem 1.5rem 2rem; }
    h1 { font-size: 2rem; line-height: 1.2; margin: 0 0 0.75rem; }
    p { margin: 0 0 0.85rem; }
    button { font: inherit; padding: 0.45rem 0.85rem; }
    #save-state { font: 0.95rem/1.4 "Segoe UI", sans-serif; color: #1e3a5f; }
  </style>
</head>
<body>
  <header>
    <p>North Campus Learning</p>
    <!-- Two nav landmarks need distinct names. One nav would not need an extra label. -->
    <nav aria-label="Primary">
      <a href="/courses">Courses</a>
      <a href="/labs">Labs</a>
    </nav>
  </header>
  <main>
    <h1>Pin a lesson</h1>
    <nav class="on-page" aria-label="On this page">
      <a href="#save-state">Current state</a>
    </nav>
    <p>This control is a button element, so it does not need role="button". The pressed state is the gap native HTML does not expose, and aria-pressed carries that state.</p>
    <button type="button" id="pin" aria-pressed="false">Pin lesson</button>
    <p id="save-state" role="status">Lesson is not pinned.</p>
  </main>
  <script>
    var pin = document.getElementById("pin");
    var state = document.getElementById("save-state");
    pin.addEventListener("click", function () {
      var pressed = pin.getAttribute("aria-pressed") === "true";
      pin.setAttribute("aria-pressed", pressed ? "false" : "true");
      pin.textContent = pressed ? "Pin lesson" : "Unpin lesson";
      state.textContent = pressed ? "Lesson is not pinned." : "Lesson pinned for this browser.";
    });
  </script>
</body>
</html>

Expected Output

The page shows two navigation groups, a heading Pin a lesson, a button labeled Pin lesson, and the sentence Lesson is not pinned. A screen reader landmark list names the navigations Primary and On this page instead of two identical Navigation items.

Activating the button changes its accessible pressed state to true, its label to Unpin lesson, and the status text to Lesson pinned for this browser. That status update can be announced without a focus jump. Activating again restores Pin lesson, aria-pressed false, and Lesson is not pinned. The address bar does not change, because the control is type="button" rather than a submit button.

Line-by-Line Walkthrough

  1. The page starts with native regions: a header, a main, a heading, and a real button. ARIA is not used to invent those roles.
  2. Two nav elements would both be announced as navigation, so each gets an aria-label: Primary and On this page.
  3. The comment records why the labels exist. A page with one nav would skip the extra name.
  4. The pin control is a button with type="button". It does not use role="button", because the element already has that role.
  5. aria-pressed="false" is the gap. Native HTML has no pressed attribute, and the label alone cannot say whether the lesson is pinned.
  6. The status paragraph uses role="status" so a change to its text is announced without moving focus.
  7. The script reads aria-pressed, flips it between true and false, and updates the visible label and the status text together.
  8. The visible label changes from Pin lesson to Unpin lesson so the state is not carried by color or by ARIA alone.
  9. Nothing on the page uses a div with a click handler. If the control needs to be a button, the sample uses a button.

Activate the button with a mouse and with the keyboard. The pressed state, the label, and the status text should all move together, and focus should stay on the button.

Common Pitfalls & Security

Redundant ARIA is a common way to make a control worse. A role that contradicts the element, or a label that replaces clearer visible text, leaves assistive technology describing something the sighted classmate does not see.

  • role="button" on a div does not add keyboard support. You still have to handle Enter and Space, which a real button already does.
  • role="navigation" on a nav is noise. The element already exposes that landmark.
  • aria-label on a control that has visible text can override that text, so the spoken name and the printed name diverge.
  • aria-pressed with no script to update it leaves a toggle stuck on the value in the HTML.
  • Hiding the only instructions in an aria-label, with nothing visible, fails people who are not using a screen reader.

Best Practices & Modern Standards

HTML5 already exposes headings, landmarks, links, buttons, and form controls. Start there. Add ARIA only for a name, a state, or a live update that the native element does not provide. The first rule used by the ARIA specification is not to use ARIA when native HTML is enough.

Keep the visible label and the accessible name in agreement. Update aria-pressed in the same function that changes the button text. Use role="status" for a short confirmation that should be heard and also read on the page. Test with a keyboard before you add any further roles.

Real-World Use

The lesson page in the course player uses this pattern for Pin lesson. A student pins the page while reading on a shared lab computer. The button shows Unpin lesson, and the status line confirms the pin without opening a dialog or moving focus away from the control.

The same page has a primary nav and an on-this-page nav. The two aria-label values are what make those landmarks distinguishable in a screen reader rotor. The rest of the page stays native HTML: a header, a main, a heading, and a button.

Frequently Asked Questions

Should every nav have an aria-label?

Only when the page has more than one navigation landmark, or the nav has no visible name to tell it apart. A single primary nav can rely on the nav element alone. Extra labels on every landmark add chatter and can drift out of date.

Does aria-pressed replace the button text?

No. aria-pressed exposes on or off. The button still needs a visible name such as Pin lesson. Changing the text to Unpin lesson as well helps everyone, including people who do not hear the pressed state announced.

What is wrong with role="button" on a styled div?

The role changes the announcement and does not add keyboard behavior, form participation, or a disabled state. You would have to rebuild those pieces. A button element already has them. Use a div only when you truly have no native control, which this pin action does not require.

When is role="status" appropriate?

Use it for a brief, non-critical update that should be announced when the text changes, such as a saved or pinned confirmation. Do not put role="status" on a paragraph that updates many times a second. That floods the screen reader. Keep the message visible as well as announced.

Need more coins?

You do not have enough coins for this action.

Your balance: 0 coins

Share

Reactions

Loading reactions…