Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages

Next.js and React · Troubleshooting walkthrough

A modal fails 'aria-hidden elements do not contain focusable elements'

Short answer

Chrome's Blocked aria-hidden warning and the axe and Lighthouse check aria-hidden elements do not contain focusable elements both mean focusable content sits under aria-hidden="true". Around a modal it is the page behind the dialog, or a closing dialog that still holds focus. Make the background inert instead, or open a native dialog with showModal(), and move focus before hiding anything.

  • Applies to Chrome and Edge (current Chromium releases), axe-core 4.10 and later (rule aria-hidden-focus), React 19.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Two different tools complain about the same underlying mistake. Chrome logs Blocked aria-hidden on an element because its descendant retained focus, and axe and Lighthouse fail the rule aria-hidden elements do not contain focusable elements. In both, content marked aria-hidden="true" is either holding the keyboard focus or can still receive it. For screen reader users that means focus has landed on something the accessibility tree says does not exist.

Modals are the usual place it happens, because opening one tempts you to hide the page behind it. The fix is to use the attribute made for that job, inert, and to order focus changes carefully. Work through the three situations below.

The text you see

  • Blocked aria-hidden on an element because its descendant retained focus. The focus must not be hidden from assistive technology users.
  • Avoid using aria-hidden on a focused element or its ancestor. Consider using the inert attribute instead, which will also prevent focus.
  • ARIA hidden element must not be focusable or contain focusable elements
  • aria-hidden elements do not contain focusable elements

Stage 1

See what is hidden and what has focus

Open the modal, then run this snippet in the console. It prints the focused element and every aria-hidden element that still contains something focusable. Which case you are in follows from where the focus sits.

Focus owner and the offending hidden containers

document.activeElement;
[...document.querySelectorAll('[aria-hidden="true"]')].filter((el) =>
  el.querySelector('a[href], button:not([disabled]), input:not([disabled]), select, textarea, [tabindex]:not([tabindex="-1"])')
);

Stage 2

The background is hidden but still tabbable

aria-hidden="true" removes an element from the accessibility tree, nothing more. Its links and buttons stay in the tab order. Deque's rule text describes exactly this gap: content leaves the reading order but remains in the focus order, so a keyboard user can tab to controls that are not announced.

Replace the attribute on the page behind the dialog with inert. MDN describes inert content as non-focusable, non-clickable and hidden from assistive technology in one step. One constraint matters: nothing inside an inert element can escape it, so the modal must be rendered outside that element, for example with a React portal into document.body.

Make the app root inert while the modal is open

// the modal lives outside #root (React: createPortal(modal, document.body))
const appRoot = document.getElementById('root');
function onModalOpen()  { appRoot.setAttribute('inert', ''); }
function onModalClose() { appRoot.removeAttribute('inert'); }

Stage 3

Focus is inside the part being hidden

Chrome's warning means aria-hidden was applied to an ancestor of the focused element, and Chrome ignores the attribute rather than hide focus from assistive technology. It happens when the background is hidden while the opener button still has focus, or when a closing dialog is hidden before focus has left it. The console prints an Element with focus line and an Ancestor with aria-hidden line to help find them.

Change the order. On open, move focus into the dialog first and hide the rest second. On close, return focus to the opener first and hide or remove the dialog second.

Focus moves first, hiding second

// open
dialog.hidden = false;
dialog.querySelector('button, [href], input').focus();
appRoot.setAttribute('inert', '');

// close
appRoot.removeAttribute('inert');
opener.focus();
dialog.hidden = true;

Stage 4

Closed drawers, menus and slides

The axe rule also fires without any modal. A drawer slid off-screen, a collapsed accordion or a carousel slide marked aria-hidden while its links still take focus is the same failure. Deque lists an off-screen link inside an aria-hidden element as failing and a display: none link or a tabindex="-1" button as passing.

For closed content, hide it for everyone with the hidden attribute or display: none, which MDN notes already removes it from the accessibility tree, so aria-hidden is not needed. For content that must stay visible but not interactive, such as a partly visible slide, use inert.

Hidden for everyone, or visible but inert

<nav id="drawer" hidden>...</nav>
<div class="slide" inert>...</div>

Stage 5

Prefer the native dialog

A <dialog> opened with showModal() makes the rest of the document inert by itself, traps focus inside, and returns focus when closed. There is no aria-hidden bookkeeping to get wrong. If a component library hides things for you, the order of focus and hiding lives in that library, so update it or report the two console lines Chrome prints.

Stage 6

Check the fix

  1. With the modal open, press Tab repeatedly: focus never leaves the dialog.
  2. Run the first snippet again: the list is empty.
  3. Close with the keyboard: the console shows no warning and document.activeElement is the opener.
  4. Add an automated accessibility test that opens and closes each modal, not just the page at rest.

If you only keep one thing

  • aria-hidden hides from screen readers but not from the keyboard.
  • Use inert for the background, and render the modal outside it.
  • Move focus before hiding anything.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Next step

Want a second pair of eyes on it?

Send me the error and what changed before it started. I reply within 24 hours.