Sub topic 9
Submenus as Disclosure Buttons
This chapter assumes you can write basic HTML, CSS, and JavaScript event handlers. It also assumes you know how to mark up a primary navigation with nav, lists, and an accessible name. Here you add submenus to that navigation. The pattern is a disclosure: a button that shows and hides a group of links.
Why submenus are disclosures, not menus
A submenu in site navigation is a group of links that stays hidden until the user asks for it. The ARIA authoring guidance describes this as a disclosure pattern. A disclosure is a button that controls the visibility of one other element.
Developers often reach for role="menu" and role="menuitem" because the word "menu" appears in the feature name. Those roles are meant for application menus, like the File menu in a desktop editor. They tell a screen reader to switch into a mode where arrow keys are the main way to move. Site navigation is a list of links, so that behavior gets in the way. Users expect Tab to move between links, and the menu roles break that expectation.
With the disclosure pattern you keep the native semantics. The trigger is a real button. The links stay real links inside a real list. The only ARIA you need is aria-expanded, and usually aria-controls.
| Aspect | Disclosure navigation | ARIA menu (`role="menu"`) |
|---|---|---|
| Trigger | `button` with `aria-expanded` | `menuitem` with `aria-haspopup` |
| Items | Links in a list | `menuitem` elements |
| Moving between items | Tab and Shift+Tab | Arrow keys, Tab leaves the menu |
| Screen reader mode | Normal browsing | Application mode |
| Best for | Website navigation | Application command menus |
The markup
Start from the primary navigation you already have. Replace a plain link with a button wherever a top-level item opens a submenu.
<nav aria-label="Primary">
<ul>
<li><a href="/" aria-current="page">Home</a></li>
<li>
<button type="button" aria-expanded="false" aria-controls="support-menu">
Support
</button>
<ul id="support-menu" hidden>
<li><a href="/support/help-center">Help center</a></li>
<li><a href="/support/contact">Contact us</a></li>
<li><a href="/support/status">System status</a></li>
</ul>
</li>
<li><a href="/pricing">Pricing</a></li>
</ul>
</nav>Each part has a job:
type="button"stops the button from submitting a form if the navigation is ever placed inside one.aria-expandedis"false"while the submenu is hidden and"true"while it is shown. Screen readers announce this as "collapsed" or "expanded".aria-controlspoints to theidof the controlled element. Support for it in screen readers is uneven, so treat it as a helpful extra. Do not rely on it alone to communicate state.hiddenremoves the submenu from the visual layout and from the accessibility tree. Its links are also removed from the tab order, so keyboard users do not tab through invisible links.
The button's text is its accessible name. Keep it the same as the label you would use for a top-level link, such as "Support". Do not add words like "menu" or "dropdown", because the state announcement already conveys that.
Should the top-level label also be a link?
Sometimes "Support" should lead to a Support landing page and also open a submenu. A single element cannot do both well. If a link opens a menu on click, the user cannot reach the landing page. There are two common solutions:
- Use only a button, and put the landing page as the first link inside the submenu, for example "Support overview".
- Use a link plus a separate small button next to it that toggles the submenu. This needs a visible or screen-reader-only label such as "Show Support submenu".
The first option is simpler and works well on touch screens. Choose the second only when the landing page matters enough to justify two controls per item.
The script
The script has one job: keep aria-expanded and hidden in sync. Keep that in one function so the two cannot drift apart.
const nav = document.querySelector('nav[aria-label="Primary"]');
const buttons = nav.querySelectorAll('button[aria-expanded]');
function setOpen(button, open) {
const panel = document.getElementById(button.getAttribute('aria-controls'));
button.setAttribute('aria-expanded', String(open));
panel.hidden = !open;
}
function closeAll(except) {
buttons.forEach((button) => {
if (button !== except) setOpen(button, false);
});
}
buttons.forEach((button) => {
button.addEventListener('click', () => {
const willOpen = button.getAttribute('aria-expanded') !== 'true';
closeAll(button);
setOpen(button, willOpen);
});
});
nav.addEventListener('keydown', (event) => {
if (event.key !== 'Escape') return;
const openButton = nav.querySelector('button[aria-expanded="true"]');
if (openButton) {
setOpen(openButton, false);
openButton.focus();
}
});
document.addEventListener('click', (event) => {
if (!nav.contains(event.target)) closeAll(null);
});This script does the following:
- It toggles one submenu per click and closes any other open submenu, so only one is open at a time.
- It closes the open submenu on Escape and returns focus to the button. Returning focus matters because the focused link just disappeared. Without this step, focus would drop to the top of the page.
- It closes everything when the user clicks outside the navigation.
Notice what it does not do. It does not move focus into the submenu when it opens. The user presses Tab to reach the first link, which is the next element in the document order. This keeps the behavior predictable.
Styling the states
CSS should make the open state visible and keep the submenu out of the way of the layout of the other items.
nav li {
position: relative;
}
nav button[aria-expanded] {
font: inherit;
padding: 0.75rem 1rem;
background: none;
border: 0;
cursor: pointer;
}
nav button[aria-expanded]::after {
content: "";
display: inline-block;
margin-left: 0.5em;
border: 0.3em solid transparent;
border-top-color: currentColor;
vertical-align: -0.15em;
}
nav button[aria-expanded="true"]::after {
transform: rotate(180deg);
}
nav button:focus-visible,
nav a:focus-visible {
outline: 3px solid #1a5fd0;
outline-offset: 2px;
}
nav li > ul {
position: absolute;
top: 100%;
left: 0;
min-width: 14rem;
padding: 0.5rem 0;
background: #fff;
border: 1px solid #ccc;
list-style: none;
}
nav li > ul[hidden] {
display: none;
}Two details deserve attention. First, the arrow is drawn with a pseudo-element, so it is not part of the button's accessible name. Second, the [hidden] rule is a safeguard. Any rule that sets display on the list would otherwise override the browser's default hidden styling and show the submenu even though the script says it is closed.
Style the arrow from aria-expanded rather than from a separate class. That way the visual state cannot disagree with the state that assistive technology reports.
Hover: an optional enhancement
Many sites also open submenus on mouse hover. If you add it, treat it as an addition to click, never a replacement. Keyboard and touch users have no hover.
Hover brings its own failure modes:
- The pointer crosses a small gap between the button and the submenu, and the submenu closes before it gets there. Remove the gap or add a short close delay.
- Content that appears on hover must stay visible until the user moves away or dismisses it, and the pointer must be able to move onto it. WCAG 1.4.13 (Content on Hover or Focus) requires this.
- Hover and click can conflict. On a touch screen, a tap may fire both. If hover opens the menu and the click toggles it, the tap closes what the hover just opened. Make the click handler tolerate this, for example by opening on hover only for devices that report
(hover: hover).
A simple way to limit hover behavior to devices that support it is a media query in CSS, or a matchMedia('(hover: hover)') check in the script.
Common errors
- Using
role="menu"for site navigation. This changes how screen readers handle keys and promises arrow-key behavior you did not build. - Updating only
hidden. Ifaria-expandedstays"false", screen reader users hear that the menu is closed while the links are showing. - Hiding with
opacity: 0orleft: -9999px. The links stay in the tab order, so keyboard focus disappears into invisible items. Usehiddenordisplay: none. - Using a
divorawith nohrefas the trigger. It cannot be reached or activated with the keyboard unless you rebuild whatbuttonalready gives you. - Not returning focus after Escape. Focus is lost when the focused element is hidden.
- Opening on focus. A submenu that opens whenever the button receives focus forces keyboard users to cross through every submenu's links while tabbing through the bar.
Check your understanding
Exercise
A developer builds a submenu with a button and a hidden list. The script removes the hidden attribute on click but never changes aria-expanded. Describe what a screen reader user experiences, and give the one-line fix inside the click handler.
Show answer
The screen reader keeps announcing the button as "collapsed" even after the links appear, so the user is told the submenu is closed while it is open. The fix is to update the attribute at the same time as hidden, for example button.setAttribute('aria-expanded', String(open)); inside the same function that sets panel.hidden = !open;. Putting both in one setOpen function prevents the two from drifting apart.
Exercise
A submenu is hidden with opacity: 0; pointer-events: none;. Why is this a keyboard accessibility bug, and which two alternatives fix it?
Show answer
The links are still in the tab order, so a keyboard user presses Tab and focus moves onto links they cannot see. Screen readers may also read them. Using the hidden attribute or display: none removes the links from both the tab order and the accessibility tree. (visibility: hidden also works.)
Exercise
Your top-level item "Products" must link to a landing page and also reveal a submenu. Name two ways to build this, and say which is simpler for touch users.
Show answer
Option one: make "Products" a button only, and put a "Products overview" link as the first item in the submenu. Option two: keep "Products" as a link and add a separate toggle button beside it with an accessible name such as "Show Products submenu". Option one is simpler for touch users because there is only one control per item and no risk of tapping the wrong target.