Skip to main content

The inert Attribute

Sometimes part of a page should be out of reach: a menu that has slid off screen, a section that is saving, the page behind a pop-up. Before inert, you switched these off by hand, with tabindex="-1" on every link, aria-hidden on containers, pointer-events: none in CSS, and a lot of luck. The inert attribute does the whole job with one word.

What You Will Learnโ€‹

  • What inert changes, and what it leaves alone
  • How it differs from disabled, hidden, and aria-hidden
  • Patterns that use it: off-canvas menus, saving states, carousels, and overlays
  • How to set it from JavaScript, and what to do without support
  • Accessibility and security notes

The Big Ideaโ€‹

Putting inert on an element says: "this part of the page is not available right now." The element and everything inside it stop taking part in the page, for every kind of visitor: mouse, touch, keyboard, and screen reader.

http://127.0.0.1:5500/index.html
Switch the area

Shop area

Help

Clicks received by the Buy button: 0 (try clicking it in each mode)

Nothing is switched off. Every control accepts focus.

The test calls focus() on each control and checks whether the browser accepted it, which is the same thing the Tab key does. Nothing is stored or sent.

What inert Doesโ€‹

For an inert element and all of its descendants:

Part of the experienceWhat happens
PointerClicks and taps do nothing, because the element is skipped when the browser works out what was pressed
KeyboardNothing inside can take focus, so Tab skips it
Screen readersThe content is removed from the accessibility tree, as if it were hidden
Text selectionText cannot be selected
Find in pageText inside is generally not found
LooksNothing changes. It still shows, exactly as before

That last row matters: inert is about behaviour, not appearance. If a section is switched off, show that with your own CSS, such as lower opacity, and say so in text.

Open the Focus Lab and try the buttons in each mode. In the plain mode, every control accepts focus. In the inert mode, every control refuses it, and clicking the Buy button does nothing.

inert, disabled, hidden, and aria-hiddenโ€‹

These four are easy to mix up:

ToolWhere it worksLooksFocus and clicksScreen readerForm data
inertAny element, with its whole subtreeUnchangedBlockedHiddenNot what it is for
disabledForm controls (and fieldset)Usually greyed outBlockedAnnounced as unavailableThe control is left out of the submission
hiddenAny elementNot displayed, no space usedNot reachableHiddenNot rendered
aria-hidden="true"Any elementUnchangedNot blockedHiddenUnchanged

The aria-hidden trapโ€‹

aria-hidden="true" only tells screen readers to ignore something. A keyboard user can still Tab into it. So a link inside an aria-hidden area can take focus, while a screen reader says nothing about it. That is a real accessibility failure.

Switch the Focus Lab to aria-hidden="true" only and run the focus test: every control still accepts focus. inert fixes both problems at once. Keep aria-hidden for things that cannot be focused, such as a purely decorative icon.

info

inert is about interaction, not form data. If you need a control left out of a submission, use disabled on that control. Do not depend on inert for that.

Pattern 1: Off-Canvas Menusโ€‹

A menu that slides off screen with CSS is invisible, but not gone. Its links are still in the tab order, so a keyboard user tabs into links they cannot see, and a screen reader reads a menu that looks closed.

The fix: set inert while the menu is closed, and remove it when the menu opens, as in the code above. Open the Off-Canvas Menu Lab, leave the box unticked, and run the focus test on the closed menu. Then tick the box and run it again.

Pattern 2: A Section That Is Savingโ€‹

When a large part of a form is saving, you want it frozen so no one edits it half-way. Instead of disabling every field, make the whole section inert and tell people what is going on:

<p id="status" role="status"></p>
<fieldset id="details">...many fields...</fieldset>
details.inert = true;
status.textContent = "Saving your changes...";
// ...when done...
details.inert = false;
status.textContent = "Saved.";

Notice that the status message sits outside the inert area. An inert area is hidden from screen readers, so anything you want announced must live somewhere else.

Pattern 3: Slides and Hidden Panelsโ€‹

In a carousel, the slides that are off screen are still in the page. Make every slide except the current one inert, so Tab does not wander into slides nobody can see. The same idea fits stacked panels, such as the earlier steps of a wizard that you keep in the page but not in view.

If you do not need to keep an area in the layout at all, hidden is simpler.

Pattern 4: Behind a Custom Overlayโ€‹

A native dialog opened with showModal() already makes the rest of the page behave as if it were inert, as the Dialog lesson explains. If you cannot use a dialog and build your own overlay, set inert on the page regions behind it (not on the overlay itself), and remove it when the overlay closes. Use the native dialog whenever you can, because it also handles focus and the Escape key.

Setting inert From JavaScriptโ€‹

inert is a boolean attribute, and it has a matching JavaScript property that stays in sync:

panel.inert = true;     // adds the attribute
panel.inert = false; // removes it
console.log(panel.inert);

A few rules to remember:

  • It is inherited and cannot be undone below. If a parent is inert, a child cannot opt out, even with inert="false". Remove it from the parent.
  • Move focus first. If the element that currently has focus becomes inert, focus is lost and falls back to the page. Move focus somewhere sensible before you switch a region off, for example to the control that opened it.
  • Explain the state. Use text, a live region outside the inert area, and your own styling.

Support and Fallbacksโ€‹

inert works in current versions of the major browsers, but older versions ignore it. Check its Baseline label on MDN. In a browser without support:

  • the area simply stays interactive,
  • and nothing breaks, but you lose the protection.

For a low-stakes case, such as an off-screen menu, that is acceptable, and you can detect support if you want a backup:

if (!("inert" in HTMLElement.prototype)) {
// optional: add tabindex="-1" to focusable children while the area is off
}

For high-stakes cases, such as blocking the page behind a pop-up, prefer the native dialog, or a maintained polyfill.

Accessibility Notesโ€‹

  • Do not make visible, important content inert without a clear reason and a visible explanation. People who use screen readers cannot reach it, and nobody can select or copy its text.
  • Tell visitors why. A greyed-out area with no explanation is confusing. Add a visible message such as "Saving...", in a live region outside the inert area.
  • Manage focus on purpose when you add or remove inert, as above.
  • Do not use inert to hide content from screen readers when it should be hidden from everyone. Use hidden.
  • Test it: press Tab through the page, click the area, and look at the browser's accessibility panel, if it has one, to confirm the area is gone from the tree.

Checking Your Setupโ€‹

  1. Press Tab repeatedly. Does focus ever land on something you cannot see?
  2. Click inside the area you switched off. Does anything respond?
  3. In DevTools, open the Accessibility panel and check that the inert area is not listed.
  4. Switch the state on and off, and watch where focus goes.
  5. Test with a screen reader if you can, and listen for your status message.
  6. Look for aria-hidden on containers that hold links or buttons. They are the likely bugs.
  7. Test in a second browser, and think about what happens without support.

Best Practicesโ€‹

  1. Use inert for areas that are temporarily unavailable.
  2. Prefer it to aria-hidden on anything that can take focus.
  3. Use disabled for individual form controls, and hidden to remove things entirely.
  4. Show the state with CSS and text.
  5. Put announcements outside the inert area.
  6. Move focus before inerting the region that has it.
  7. Use a native dialog for modal pop-ups.
  8. Plan for browsers without support.
  9. Keep the area small and temporary.
  10. Test with the keyboard and a screen reader.

Quick Checklistโ€‹

  • Off-screen menus, slides, and panels are inert while out of view
  • No focusable controls sit inside aria-hidden areas
  • An inert area has a visible explanation and a status message outside it
  • Focus is moved before a region is made inert
  • I used disabled for single controls, and hidden to remove content
  • Modal pop-ups use a native dialog
  • The page still works where inert is not supported
  • I tested with Tab, the mouse, and a screen reader

Security and Privacy Notesโ€‹

  • inert is not security. It is a client-side setting that any visitor can remove in DevTools. Never use it to protect information or actions. The server must decide who may do what.
  • Inert content is still in the page. It is in the source, and it may still be sent in a form. Do not hide private data in an inert area.
  • Do not rely on it to prevent double submissions or double payments. Disable buttons, and make the server ignore repeats. See Secure Forms.
  • Inert does not stop scripts. A script can still read or change an inert area.

Global Audience Notesโ€‹

  • Keyboard and screen reader use is common among many visitors, including people who cannot use a mouse. Hidden-but-focusable content hurts them the most.
  • Explain states in plain, translatable text. "Saving..." and "Unavailable" are easier to translate than icons alone.
  • Slow connections make saving states long. A clear message and a way to tell when it ends are especially helpful.
  • Older and low-cost devices may lack support. Keep the page usable without it.

Common Mistakes to Avoidโ€‹

  1. Hiding a menu with CSS only, so its links still take focus.
  2. Using aria-hidden on areas with links or buttons.
  3. Making important content inert with no explanation.
  4. Putting the status message inside the inert area.
  5. Forgetting to move focus before switching the area off.
  6. Expecting inert to change how the area looks.
  7. Using inert instead of disabled to keep a field out of a submission.
  8. Trying to un-inert a child of an inert parent.
  9. Building a custom pop-up when a native dialog would do.
  10. Treating inert as protection.

Practice Timeโ€‹

  1. In the Focus Lab, run the focus test in all three modes. Which controls accept focus each time?
  2. In inert mode, click the Buy button several times. What is the counter?
  3. Switch to aria-hidden only, and press the Buy button again. What is the difference?
  4. In the Menu Lab, with the menu closed and the box unticked, run the test. What does it say about invisible links?
  5. Tick the box and test again.
  6. Complete the Which Tool? quiz.
  7. Add inert to an off-screen menu on a test page, and check it with the Tab key.
  8. Bonus: write the three lines of script that freeze a form section while it saves, and announce the change.

Quick Recapโ€‹

Click a question to check your answer.

1. What does inert do?

It makes an element and all its descendants unavailable: no clicks, no keyboard focus, no screen reader access, and no text selection. It does not change how the element looks.

2. How is inert different from aria-hidden?

aria-hidden only hides content from screen readers, so keyboard users can still focus it. inert blocks pointer, keyboard, and screen reader access together.

3. How is inert different from disabled?

disabled applies to form controls, greys them out, and leaves them out of form submission. inert applies to any element and its whole subtree, and changes behaviour only.

4. Why set inert on a closed off-canvas menu?

A menu hidden only by CSS still takes keyboard focus and is read by screen readers. inert removes it from both.

5. What should you do before making a region inert?

Move focus out of it, if it holds the focused element, and prepare a visible explanation and a status message outside the region.

6. Is inert a security feature?

No. It is a client-side setting that visitors can remove, so the server must always enforce permissions.

Key Takeawaysโ€‹

  • inert switches off an area for pointer, keyboard, and screen reader, with one attribute.
  • It changes behaviour, not looks. Show the state yourself.
  • Prefer it to aria-hidden whenever the content can take focus.
  • Use disabled for single controls and hidden to remove content.
  • Great for off-canvas menus, saving states, and carousels.
  • Move focus first, and announce outside the area.
  • Never treat it as security.

Next up: template, where you will learn how to keep reusable chunks of HTML in your page without showing them, and copy them when you need them.