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
inertchanges, and what it leaves alone - How it differs from
disabled,hidden, andaria-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.
- Focus Lab
- Off-Canvas Menu Lab
- Which Tool?
- The Code
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.
The menu is closed and off screen.
With the menu closed and the box unticked, the links are invisible but still accept focus, so a keyboard user would tab into links they cannot see. Tick the box and test again.
Pick the right tool for each job.
<!-- One word switches the whole area off -->
<section id="shop" inert>
<button>Buy now</button>
<a href="/help">Help</a>
<input type="text" aria-label="Name">
</section>
<!-- An off-canvas menu: unreachable while closed -->
<button id="toggle" aria-expanded="false" aria-controls="menu">Menu</button>
<nav id="menu" aria-label="Main" inert>
<a href="/">Home</a>
<a href="/recipes">Recipes</a>
</nav>
const toggle = document.getElementById("toggle");
const menu = document.getElementById("menu");
toggle.addEventListener("click", () => {
const opening = menu.inert; // inert now means "closed"
menu.inert = !opening; // reflects the attribute
toggle.setAttribute("aria-expanded", String(opening));
});
/* inert changes behaviour, not looks, so show it yourself */
[inert] {
opacity: 0.5;
}
What inert Doesโ
For an inert element and all of its descendants:
| Part of the experience | What happens |
|---|---|
| Pointer | Clicks and taps do nothing, because the element is skipped when the browser works out what was pressed |
| Keyboard | Nothing inside can take focus, so Tab skips it |
| Screen readers | The content is removed from the accessibility tree, as if it were hidden |
| Text selection | Text cannot be selected |
| Find in page | Text inside is generally not found |
| Looks | Nothing 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:
| Tool | Where it works | Looks | Focus and clicks | Screen reader | Form data |
|---|---|---|---|---|---|
inert | Any element, with its whole subtree | Unchanged | Blocked | Hidden | Not what it is for |
disabled | Form controls (and fieldset) | Usually greyed out | Blocked | Announced as unavailable | The control is left out of the submission |
hidden | Any element | Not displayed, no space used | Not reachable | Hidden | Not rendered |
aria-hidden="true" | Any element | Unchanged | Not blocked | Hidden | Unchanged |
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.
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
inertto hide content from screen readers when it should be hidden from everyone. Usehidden. - 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โ
- Press Tab repeatedly. Does focus ever land on something you cannot see?
- Click inside the area you switched off. Does anything respond?
- In DevTools, open the Accessibility panel and check that the inert area is not listed.
- Switch the state on and off, and watch where focus goes.
- Test with a screen reader if you can, and listen for your status message.
- Look for
aria-hiddenon containers that hold links or buttons. They are the likely bugs. - Test in a second browser, and think about what happens without support.
Best Practicesโ
- Use
inertfor areas that are temporarily unavailable. - Prefer it to
aria-hiddenon anything that can take focus. - Use
disabledfor individual form controls, andhiddento remove things entirely. - Show the state with CSS and text.
- Put announcements outside the inert area.
- Move focus before inerting the region that has it.
- Use a native
dialogfor modal pop-ups. - Plan for browsers without support.
- Keep the area small and temporary.
- 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-hiddenareas - An inert area has a visible explanation and a status message outside it
- Focus is moved before a region is made inert
- I used
disabledfor single controls, andhiddento remove content - Modal pop-ups use a native
dialog - The page still works where
inertis not supported - I tested with Tab, the mouse, and a screen reader
Security and Privacy Notesโ
inertis 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โ
- Hiding a menu with CSS only, so its links still take focus.
- Using
aria-hiddenon areas with links or buttons. - Making important content inert with no explanation.
- Putting the status message inside the inert area.
- Forgetting to move focus before switching the area off.
- Expecting
inertto change how the area looks. - Using
inertinstead ofdisabledto keep a field out of a submission. - Trying to un-inert a child of an inert parent.
- Building a custom pop-up when a native
dialogwould do. - Treating
inertas protection.
Practice Timeโ
- In the Focus Lab, run the focus test in all three modes. Which controls accept focus each time?
- In
inertmode, click the Buy button several times. What is the counter? - Switch to
aria-hiddenonly, and press the Buy button again. What is the difference? - In the Menu Lab, with the menu closed and the box unticked, run the test. What does it say about invisible links?
- Tick the box and test again.
- Complete the Which Tool? quiz.
- Add
inertto an off-screen menu on a test page, and check it with the Tab key. - 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โ
inertswitches 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-hiddenwhenever the content can take focus. - Use
disabledfor single controls andhiddento 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.