Step by Step

The aria-expanded attribute: which element gets it, and why NVDA still says “collapsed”

8 min read
Mikhail ShaymardanovWeb Quality and Accessibility Expert

The aria-expanded attribute tells a screen reader whether the content a control opens is shown: "true" if so, "false" if hidden, no attribute if the control opens nothing. On a hamburger menu, a real button carries it: on a clickable div, with no role, Chrome ignores it and the screen reader gets no state. The script flips the value, and the CSS can read it to show the menu.

Setting, toggling and checking a menu's state


#Which element gets aria-expanded, and with what value?

The aria-expanded attribute goes on the control that shows and hides content: "true" means shown, "false" hidden; no attribute means the control opens nothing. On a hamburger menu, that's the button: a blind person doesn't see the menu appear, so this ARIA state tells them. NVDA, a free screen reader for Windows, adds it to the button's name and role: “collapsed” or “expanded”.

An accessible menu button follows four rules, and each version of our example is judged by them:

  • it's a real <button>: Tab reaches it, and Enter and Space activate it with no script;
  • it carries aria-expanded, "false" on load and "true" with the menu open, since a missing attribute isn't “closed”;
  • its name stays “Menu”, open or closed, as our aria-label guide explains;
  • the menu itself carries no aria-expanded.

At Larkmere Cheese Cellar, our demo cheesemonger, a round button with three lines opens the navigation in the mobile header. We coded it four ways, all identical on screen.

Screenshot: Larkmere Cheese Cellar's red header on a phone, with the menu open. A yellow panel lists Cheeses, Cheese boards, Tasting workshops, The aging cellar and Find us over the page; the round yellow button shows a cross.Marker 1. The round menu button, now showing a cross. Marker 2. The yellow navigation panel, open below the header.
Menu open in Larkmere's header: the open or closed state belongs on the round buttonmarker 1, now a cross, and never on the yellow panelmarker 2 that it opens.

#Why doesn't aria-expanded work on a clickable div?

In our first version, the icon is a div that responds to clicks. With no role attribute, it gets the role generic, which carries no meaning. ARIA 1.2 allows aria-expanded only on a closed list of roles, such as button, link or tab, and generic isn't on it. The phrase developers search for, “the aria expanded attribute is not allowed on the generic role”, says just that.

Chrome hands screen readers its accessibility tree. In it, we found the div reduced to a generic node with no name or state, which Tab never reaches. The axe-core 4.11.0 testing engine does flag it, under its aria-allowed-attr rule. Its message names no role, though: “ARIA attribute is not allowed: aria-expanded="false"”. The phrase developers search for therefore doesn't come from axe.

Screenshot: Larkmere Cheese Cellar's red header, with the Larkmere name in brush script, the Cart (2) link and a round yellow button with three black lines.Marker 1. The round three-line button, menu closed.
The same three linesmarker 1: as a div, they give Chrome a generic node with no state that Tab never reaches; as a button, NVDA announces “Menu, button, collapsed”.

Avoid

<div class="burger" aria-expanded="false" onclick="toggleMenu(this)">
  <svg width="24" height="24" viewBox="0 0 24 24">
    <path class="line top" d="M3 6h18" />
    <path class="line middle" d="M3 12h18" />
    <path class="line bottom" d="M3 18h18" />
  </svg>
</div>

Correct

<button type="button" class="burger" aria-label="Menu" aria-expanded="false" aria-controls="menu">
  <svg aria-hidden="true" focusable="false" width="24" height="24" viewBox="0 0 24 24">
    <path class="line top" d="M3 6h18" />
    <path class="line middle" d="M3 12h18" />
    <path class="line bottom" d="M3 18h18" />
  </svg>
</button>

Screen reader: « Menu, button, collapsed »

Styled like the old div, the <button> looks the same. A div with role="button" and tabindex="0" is still tolerated, since Chrome then passes on a button and its state. You have to code Enter and Space yourself, though, and the <button> handles both on its own. An aria-label on a div with no role runs into the same generic role.

The Includdy scan flags this div too, since it runs axe's default rules. Its AI writes a fixed button next to the faulty code, to review against the four menu-button rules.

The “tabIndex should only be declared on interactive elements” error: what to use instead

#Should the menu itself get aria-expanded too?

No: on the panel, the attribute tells screen readers nothing. The navigation role of the nav element used to accept it, until ARIA 1.2 took it away in 2019. An older tutorial may still put it on the menu.

In our second version, the script toggles the attribute on the nav, and the button doesn't have it. Chrome ignores it: its accessibility tree shows a navigation node and a button, neither with a state. Axe-core, which flagged the div, reports nothing here. The Chrome and NVDA test at the end of this page catches it.

Screenshot: under Larkmere Cheese Cellar's red header, an open yellow panel lists Cheeses, Cheese boards, Tasting workshops, The aging cellar and Find us.Marker 1. The yellow navigation panel, open.
On the yellow panelmarker 1, aria-expanded never shows up in Chrome's tree, and axe-core reports nothing; on the button, the state gets through.

Avoid

<button type="button" class="burger" aria-label="Menu">…</button>
<nav id="menu" class="menu" aria-expanded="true">
  <ul>…</ul>
</nav>

Correct

<button type="button" class="burger" aria-label="Menu" aria-expanded="true" aria-controls="menu">…</button>
<nav id="menu" class="menu">
  <ul>…</ul>
</nav>

Screen reader: « Menu, button, expanded »


#Why is the menu open on screen while NVDA says “collapsed”?

When a script opens the menu with a CSS class and leaves aria-expanded alone, the attribute stays at false. The browser takes the state from that attribute alone, and NVDA announces it: “collapsed”, in front of a menu that's plainly open.

In our third version, the button does carry aria-expanded="false", but the script toggles an .is-open class on the nav. With the menu open, Chrome's Accessibility tab showed us the Expanded row still at false. Axe-core reports nothing: it can't know that this button opens a panel.

Screenshot: Chrome on Larkmere Cheese Cellar's header, with the yellow menu open on the left. On the right, the developer tools: the Elements panel with the menu button selected, and the Accessibility tab, whose computed properties show Expanded: false.Marker 1. The yellow menu, open in the page. Marker 2. The Expanded row of the computed properties, set to false.
The menu is open on screenmarker 1, yet Chrome still passes on “Expanded: false”marker 2: that's the state NVDA announces, “collapsed”.

In an audit, a state stuck at “collapsed” fails the WCAG success criterion Name, Role, Value, at level A. It requires that assistive technologies get notified when a scripted component changes state. In Europe, EN 301 549 requires every web page to meet that criterion. This standard was drafted as one voluntary way to comply with Directive (EU) 2019/882, the European Accessibility Act.

#How do you open the menu in CSS with aria-expanded="true"?

The fix drops the .is-open class. The script flips the attribute, writing the string "true" or "false", and the CSS reads its value. The selector [aria-expanded="true"] matches only that exact value, never a missing or empty attribute. Three rules draw the cross, and one more shows the menu, which the + selector finds only right after the button, inside the same parent.

Screenshot: Larkmere Cheese Cellar's red header with the menu open: the round yellow button shows a cross, and the top of the yellow panel shows Cheeses and Cheese boards.Marker 1. The round menu button, now showing a cross.
Same screen, menu open and cross showingmarker 1: with the class, the state passed on stays “collapsed”; once the CSS reads aria-expanded, it becomes “expanded”.

Avoid

<button type="button" class="burger" aria-label="Menu" aria-expanded="false" aria-controls="menu">…</button>
<nav id="menu" class="menu">…</nav>
 
<style>
  .menu { display: none; }
  .menu.is-open { display: flex; }
  .burger.is-open .top { transform: translateY(6px) rotate(45deg); }
</style>
<script>
  const button = document.querySelector(".burger");
  const menu = document.querySelector(".menu");
  button.addEventListener("click", () => {
    button.classList.toggle("is-open");
    menu.classList.toggle("is-open");
  });
</script>

Screen reader: « Menu, button, collapsed »

Correct

<button type="button" class="burger" aria-label="Menu" aria-expanded="false" aria-controls="menu">
  <svg aria-hidden="true" focusable="false" width="24" height="24" viewBox="0 0 24 24">
    <path class="line top" d="M3 6h18" />
    <path class="line middle" d="M3 12h18" />
    <path class="line bottom" d="M3 18h18" />
  </svg>
</button>
<nav id="menu" class="menu">
  <ul>
    <li><a href="/cheeses">Cheeses</a></li>
    <li><a href="/boards">Cheese boards</a></li>
  </ul>
</nav>
 
<style>
  .menu { display: none; }
  .burger[aria-expanded="true"] + .menu { display: flex; }
  .burger .line { stroke: currentColor; stroke-width: 2; transform-box: fill-box; transform-origin: center; }
  .burger[aria-expanded="true"] .top { transform: translateY(6px) rotate(45deg); }
  .burger[aria-expanded="true"] .middle { opacity: 0; }
  .burger[aria-expanded="true"] .bottom { transform: translateY(-6px) rotate(-45deg); }
</style>
<script>
  const button = document.querySelector(".burger");
  button.addEventListener("click", () => {
    const isOpen = button.getAttribute("aria-expanded") === "true";
    button.setAttribute("aria-expanded", String(!isOpen));
  });
</script>

Screen reader: « Menu, button, expanded »

Opening the menu through the attribute goes further than the navigation example of the W3C, which publishes ARIA. That example reads the attribute in CSS only to style the button, and opens the links with JavaScript.

Leaving the hidden attribute on the menu as well sets a trap: a display rule overrides it, so a menu set to display: flex stays visible even closed. On the Larkmere demo, only the CSS hides the menu, with display: none by default. Closed, the menu drops out of the accessibility tree, and in our Chrome test, Tab skipped its five links.

Reading the attribute in CSS also changes who notices a bug. With the class version, only someone using a screen reader heard “collapsed”. On the fixed version, we commented out the setAttribute line, and a click left the menu closed, three lines still showing. The whole team now sees the mistake on the first click.


#How do you check what the button tells a screen reader?

The test takes two steps: Chrome's Accessibility tab shows the state the button passes on, then NVDA with Chrome lets you hear it. Together, they catch the attribute on the panel and the state frozen by a class, two mistakes axe-core lets through.

  1. In Chrome's developer tools, open the Elements panel and select the menu button.
  2. In the Accessibility tab, Computed Properties shows its name, role and Expanded row.
  3. Click the button in the page: the value should switch from false to true.

The person-shaped button at the top right of the DOM tree replaces that tree with the page's full accessibility tree. On the version with the attribute on the nav, we found a navigation row there with no state.

Screenshot: Chrome on Larkmere Cheese Cellar's fixed header, with the yellow menu open on the left. On the right, the Elements panel with the menu button selected and aria-expanded set to true, and the Accessibility tab, whose computed properties show Expanded: true.Marker 1. The Expanded row of the computed properties, set to true.
On Larkmere's fixed menu, the Expanded row switches to “true”marker 1 as soon as the button is clicked.

#What should NVDA say when the menu opens?

When Tab lands on the button, NVDA says its name, role and state, “Menu, button, collapsed”, sometimes after naming the area it enters. Press Enter and it repeats neither the name nor the role: it only says “expanded”, then “collapsed” on the next press. We took these words from the W3C's screen reader tests, run with Chrome and not yet approved. The audio clips come from the eSpeak NG speech synthesizer. On a Mac, NVDA runs in a virtual machine.

Click the demo, then press the key shown, or move on with “Next step”. Escape leaves the demo.
3 steps

Three keys on Larkmere's fixed menu: when it opens, NVDA says only one word, “expanded”.

Keep focus on the button when the menu opens, because NVDA doesn't report a state change on an element that has lost focus. If the script moves focus to the first link, “expanded” can get lost. NVDA's developer, NV Access, confirmed this in focus mode, where the screen reader passes almost every key to the component. Issue 12738 has been open since 2021. The W3C pattern leaves focus on the button.

Larkmere's fixed version looks the same on screen. A real button carries the state in a single attribute, which the script switches from false to true and the CSS reads to show the menu. Chrome's tree shows true, and NVDA turns it into “expanded”. The .is-open class is gone, along with the line that looked up the menu: the script now only knows about the button.

NVDA screen reader: how to install it, set it up and test a web page

To track down other elements like this div, with no role and an ARIA attribute they don't accept, the Includdy scan analyzes a page or a component. Every issue it flags comes with a test plan, steps and expected result included, that any teammate can run again.


Share this article

Frequently asked questions

For a hamburger menu, it's recommended. The W3C's disclosure pattern calls it optional, while the ARIA 1.2 specification says authors should use it when the content that expands isn't a child of the button. A menu that sits next to its button is exactly that case.

The attribute points to the menu's id, as in aria-controls="menu". NVDA doesn't announce this link on the button, and the request to make it do so, filed in 2018, is still open. The open or closed state itself comes from aria-expanded.

For the state, yes. The first summary inside a details element is exposed as a button, expanded or collapsed depending on the open attribute, without a line of script, and Chrome's accessibility tree does give it that state. The HTML specification reserves details for disclosure widgets, and a list of links that you show and hide is one.

The W3C's screen reader test of this pattern uses a button with aria-expanded: before you switch to details, listen to that markup with NVDA.

No. The W3C keeps the menu role for widgets that offer the complex behavior assistive technologies expect from that role. Its navigation example codes a site's links as a disclosure pattern, even when people call them a menu.

The button carries aria-expanded, and the links stay a plain list inside the nav element.