EthicsComputerCourses
Join training +91 90532 10052

ARIA Roles & Attributes

HTML · Lesson 71 of 234 Intermediate 9 min read Updated

By the end of this lesson you will know what ARIA really changes in the browser and when to leave it out, and you will build a disclosure button and a live status message that screen readers announce correctly.

HTML Accessibility Fundamentals introduced ARIA with a short table of attributes. This lesson explains the model behind them. Advanced ARIA Implementation later covers widgets such as tabs and comboboxes.

What ARIA changes, and what it does not

From the DOM, the browser also builds an accessibility tree. Each node has a role (button, link, heading), a name ("Search") and states (expanded, checked, disabled). Screen readers and voice control software read this tree, not your HTML.

ARIA (Accessible Rich Internet Applications) is a W3C standard of attributes that edit the accessibility tree. It never adds behaviour, keyboard support or styling.

Think of a parcel at a courier office. ARIA is the label: writing "Fragile" adds no bubble wrap inside. A <div role="button"> is a box labelled "button" with no button inside: it is not focusable and Enter does nothing until you write that code.

The first rule of ARIA

The W3C note Using ARIA gives five rules. The first matters most: if a native HTML element or attribute already has the meaning and behaviour you need, use it instead of ARIA.

HTML
<!-- Avoid: ARIA pretends, and you must add tabindex and key handling -->
<div role="button" tabindex="0">Pay ₹499</div>

<!-- Prefer: focus, Enter, Space and the role come free -->
<button type="button">Pay ₹499</button>

<!-- Also avoid: the role is already built in, so this is just noise -->
<nav role="navigation">...</nav>

The other four rules, in short: do not change a native element's meaning unless you must; interactive ARIA controls must work with a keyboard; never hide a focusable element from assistive technology; and every interactive element needs an accessible name.

Roles, states and properties

ARIA has three kinds of information:

KindWhat it saysExamples
RoleWhat the element isrole="tablist", role="alert", role="img"
StateA condition that often changes while the user worksaria-expanded, aria-pressed, aria-checked, aria-current, aria-invalid
PropertyA fact that rarely changesaria-label, aria-labelledby, aria-describedby, aria-controls, aria-required

Roles fall into groups such as landmark, widget (tab, slider) and live region (status, alert). Your script must keep states up to date: a stale aria-expanded="false" on an open menu is a wrong label on the parcel.

Landmark roles come from HTML

Screen reader users jump between landmarks the way you skim a newspaper by its sections. HTML elements already map to landmark roles:

HTML elementImplicit landmark roleNote
<header>bannerOnly at page level, not inside <article>, <main> or similar
<nav>navigationLabel it when a page has more than one
<main>mainOne visible <main> per page
<aside>complementaryAt page level
<footer>contentinfoSame rule as <header>
<section>regionOnly when it has an accessible name
<form>formOnly when it has an accessible name
<search>searchWraps a site search form

Name repeated landmarks, such as <nav aria-label="Main"> and <nav aria-label="Footer">, or users hear "navigation, navigation". More in HTML Document Outline & Landmarks.

Naming: aria-label, aria-labelledby and aria-describedby

The accessible name is what a screen reader says for an element, such as "Search, button". aria-labelledby wins over aria-label, which wins over native sources such as a <label>, the element's text or an image's alt.

HTML
<!-- aria-labelledby: reuse visible text as the name -->
<h2 id="fees-h">Hostel fees, 2026</h2>
<table aria-labelledby="fees-h">...</table>

<!-- aria-label: a name when there is no visible text -->
<button type="button" aria-label="Close">✕</button>

<!-- aria-describedby: extra help read after the name -->
<label for="pin">PIN code</label>
<input id="pin" inputmode="numeric" aria-describedby="pin-hint">
<p id="pin-hint">Six digits, for example 124001 for Rohtak.</p>

Three things to remember:

  • Prefer aria-labelledby when the text is visible, because it stays in sync and gets translated by browser tools.
  • aria-label is not supported on a plain <div> or <span> with no role.
  • A control's accessible name should start with its visible text, because voice control users say what they see.

Step 1: a disclosure button with aria-expanded

A disclosure is a button that shows and hides a section, such as a filter panel. Native <details> and <summary> do this with no ARIA, as in Details & Summary Elements. When your design needs a separate button, use ARIA states:

HTMLOpen in editor
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Filter panel</title>
  <style>
    body { font-family: system-ui, sans-serif; padding: 16px; }
    #filters { border: 1px solid #ccc; padding: 8px 12px; margin-top: 8px; }
    button[aria-expanded="true"]::after { content: " ▲"; }
    button[aria-expanded="false"]::after { content: " ▼"; }
  </style>
</head>
<body>
  <h1>Kurtas for men</h1>
  <button type="button" id="toggle" aria-expanded="false" aria-controls="filters">Filters</button>
  <div id="filters" hidden>
    <label><input type="checkbox"> Cotton</label>
    <label><input type="checkbox"> Under ₹999</label>
    <label><input type="checkbox"> Ships from Jaipur</label>
  </div>

  <script>
    const button = document.querySelector("#toggle");
    const panel = document.querySelector("#filters");
    button.addEventListener("click", () => {
      const open = button.getAttribute("aria-expanded") === "true";
      button.setAttribute("aria-expanded", String(!open));
      panel.hidden = open;
    });
  </script>
</body>
</html>

What you should see and hear:

  1. The panel is hidden. A screen reader says "Filters, button, collapsed".
  2. Press Tab, then Enter. The panel opens, the arrow flips and the reader says "expanded".
  3. Inspect the button in the DevTools Accessibility pane: the expanded state changes with every press.

The CSS arrow reads the ARIA state, so the look and the announcement can never disagree. aria-controls points to the panel's id; some screen readers use it and others ignore it.

Step 2: live regions for status messages

When script changes text that the user is not focused on, a screen reader says nothing unless the text is in a live region.

  • aria-live="polite" waits until the reader finishes speaking. Use it for most updates.
  • aria-live="assertive" interrupts at once. Keep it for urgent errors.
  • role="status" is a polite live region and role="alert" an assertive one. Both read the whole region.
HTMLOpen in editor
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Cart status</title>
</head>
<body>
  <h1>Rohan's Sports Shop</h1>
  <p>Cricket bat, Kashmir willow, ₹2,450</p>
  <button type="button" id="add">Add to cart</button>
  <p role="status" id="cart-status"></p>

  <script>
    let count = 0;
    document.querySelector("#add").addEventListener("click", () => {
      count++;
      document.querySelector("#cart-status").textContent =
        `Bat added. Your cart has ${count} item${count === 1 ? "" : "s"}.`;
    });
  </script>
</body>
</html>

The status paragraph is in the page from the start, empty. Many screen readers only watch regions that existed before the change, so adding a new element with text in one step is often silent.

aria-hidden and its pitfalls

aria-hidden="true" removes an element and everything inside it from the accessibility tree. It suits decorative icons next to text. Elsewhere it is dangerous, because it hides content from screen readers but not from the keyboard.

HTML
<!-- Wrong: the link can still get focus, but the reader announces nothing useful -->
<div aria-hidden="true"><a href="/offers">Diwali offers</a></div>

<!-- Right for a closed off-screen menu: hidden from everyone -->
<nav id="menu" aria-label="Main" hidden>...</nav>

<!-- Right for content behind an open panel: no focus, no clicks, no reading -->
<main inert>...</main>

Common mistakes

  • ARIA instead of HTML. role="button" on a <div> needs keyboard code; a <button> does not.
  • Forgetting to update states. aria-expanded must change every time the panel opens or closes.
  • Hidden name differs from visible text. Voice control users cannot click it by name.
  • Creating the live region at the moment of the message. Put it in the page first, empty.

Interview questions

1. What is the first rule of ARIA?

If a native HTML element or attribute does the job, use it instead of ARIA. Native elements bring focus, keyboard handling and roles; ARIA only changes what is announced.

2. What is the difference between an ARIA state and an ARIA property?

States, such as aria-expanded or aria-checked, describe conditions that change as the user interacts. Properties, such as aria-labelledby or aria-controls, describe characteristics that usually stay the same.

3. When would you use aria-labelledby instead of aria-label?

Use aria-labelledby when the name already exists as visible text, because it reuses that text and takes priority. Use aria-label when there is no visible text, such as an icon-only button.

4. How do you build an accessible show/hide button?

Prefer <details> and <summary>. Otherwise use a real <button> with aria-expanded and aria-controls pointing at the panel, and on click toggle both the attribute and the panel's hidden property.

5. What is wrong with aria-hidden="true" on a container with links?

The links leave the accessibility tree but can still receive keyboard focus, so a screen reader user lands on something announced badly or not at all. Use hidden or inert instead.

Key points

  • ARIA edits the accessibility tree (role, name, state) and never adds behaviour or keyboard support.
  • First rule: use native HTML when it does the job; avoid redundant roles.
  • Roles say what something is, states change during use, properties rarely change.
  • HTML elements already provide landmark roles; label repeated landmarks such as two <nav> elements.
  • Name priority: aria-labelledby, then aria-label, then native sources.
  • Keep aria-expanded up to date; create live regions before they change.
  • Never use aria-hidden on focusable content; use hidden or inert.

Exercise

A coaching centre in Pune shows its FAQ question "What are the batch timings?" in a clickable <div>, and the answer "Morning 7-9 am and evening 6-8 pm" in a <div> with display:none. Rebuild it accessibly: use a real button, add the right ARIA state and link, keep the answer hidden at first, and add a status line that says "Answer shown" or "Answer hidden" after each press.

Show solution
HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>FAQ</title>
</head>
<body>
  <h2>
    <button type="button" id="q1" aria-expanded="false" aria-controls="a1">
      What are the batch timings?
    </button>
  </h2>
  <div id="a1" hidden>
    <p>Morning 7-9 am and evening 6-8 pm.</p>
  </div>
  <p role="status" id="faq-status"></p>

  <script>
    const q = document.querySelector("#q1");
    const a = document.querySelector("#a1");
    q.addEventListener("click", () => {
      const open = q.getAttribute("aria-expanded") === "true";
      q.setAttribute("aria-expanded", String(!open));
      a.hidden = open;
      document.querySelector("#faq-status").textContent = open ? "Answer hidden" : "Answer shown";
    });
  </script>
</body>
</html>

The real <button> brings focus and keyboard support, and the <h2> keeps the question in the heading outline. aria-expanded changes on every press, and the empty role="status" paragraph exists before its text changes, so the update is announced.

Quick Quiz

1. What does adding role="button" to a <div> give you?

2. Which attribute's value usually changes while the user works with the page?

3. An element has both aria-labelledby and aria-label. Which gives the accessible name?

4. Why is aria-hidden="true" on a wrapper around a link a problem?

Learn HTML with a mentor

Free lessons get you started. Our Full Stack Web Development training adds real projects and code reviews from working developers.

  • Project-based learning
  • Mentor code reviews
  • Free counselling call