HTML inert Attribute Explained: How to Make a DOM Subtree Completely Non-Interactive
The HTML inert attribute lets you make an entire DOM subtree non-interactive, unfocusable, and invisible to assistive technologies with a single declarative property. This article explains what inert does, how it compares to disabled, readonly, and aria-hidden, how to use it in markup and JavaScript, and its most practical use cases such as modal dialog background locking, drawers and overlays, and page caching. It also covers accessibility behavior, browser support detection, and common pitfalls to avoid.
1. What Is the inert Attribute?
inert is a standard HTML global attribute whose meaning is essentially “non-responsive.” When an element is marked inert, the browser does three things to that element and its entire subtree:
- Blocks all user interaction events: clicks, hover, drag, and input events simply don’t fire;
- Removes the subtree from the focus order: Tab navigation,
focus()calls, andautofocusall skip over it; - Hides it from assistive technologies: screen readers no longer announce the subtree’s content, equivalent to pruning it from the accessibility tree.
Its positioning differs from disabled, readonly, and aria-hidden: disabled only applies to form controls and cannot be applied to arbitrary parents with child elements; readonly only affects input values; aria-hidden only affects the accessibility tree but doesn’t block interaction. inert is a combined “interaction + focus + accessibility” solution that covers all three concerns at once.
2. Basic Usage
<div id="sidebar" inert><!-- The entire subtree is non-interactive, unfocusable, and hidden from screen readers --><nav>...</nav></div><div id="modal"><!-- Modal content responds normally --></div>
You can also control it dynamically with JavaScript:
const sidebar = document.querySelector('#sidebar');// Make the background inert when opening the modalfunction openModal() {sidebar.inert = true;modal.showModal();}function closeModal() {sidebar.inert = false;modal.close();}
inert is a boolean attribute, exposed on the JS side as the DOM property element.inert. Setting it to true or false takes effect immediately—no need to manually traverse child elements and add tabindex="-1".
3. Typical Use Cases
1. Locking the Background Behind a Modal Dialog
The two classic problems with modal dialogs are “the background is still clickable” and “focus can escape to the background.” The traditional approach is to listen for keydown, intercept Tab, and manually cycle focus—verbose code that’s easy to get wrong. With inert, you can solve it in one step:
function trapBackground(root, modal) {Array.from(document.body.children).forEach(el => {if (el !== modal && el !== overlay) el.inert = true;});}
Combined with the native dialog element, you get standards-compliant focus trapping at almost zero cost.
2. Drawers, Sidebars, and Overlays
When a mobile slide-in drawer is closed, hiding it with CSS alone may still leave it reachable by screen readers; using visibility: hidden would forfeit transition animations. inert is decoupled from CSS transitions, so you can apply it after the closing animation finishes—getting both smooth motion and proper accessibility.
3. Route-Level Page Caching
When front-end frameworks implement keep-alive style page caching, cached but inactive pages can retain focus and event listeners. Adding inert to the root node of inactive pages prevents “ghost input” and duplicate focus issues.
4. Interaction Lockdown and Read-Only States
During feature rollouts or failure drills, you may sometimes need to temporarily block user interaction in a specific area. inert provides a switch that doesn’t intrude into business code.
4. Accessibility and Interaction Details
- If an element inside an inert subtree currently has focus, it is automatically blurred—the browser guarantees focus never “dangles” inside an inert subtree.
- inert is not the same as
aria-hidden:aria-hiddenonly hides content at the announcement level. If such an element is still focusable, you get an invalid state where focus lands on an invisible element. inert removes focus reachability as well, making its semantics more complete. - You can detect support with
'inert' in HTMLElement.prototype. For older browsers that lack support, fall back to a degraded approach of iterating and settingdisabled/aria-hiddenplus keyboard event interception, or use a mature polyfill (typically based onpointer-events: noneand tabindex rewriting).
5. Things to Watch Out For
- inert applies to the entire subtree, so place it carefully—usually on the root container behind an overlay;
- Don’t treat inert as a security measure—it only blocks user interaction; scripts can still access and manipulate the subtree via APIs;
- There is no default visual change in CSS, so it’s a good idea to style inert regions to signal unavailability to users, for example:
[inert] {pointer-events: none;opacity: 0.6;user-select: none;}
- In form submission scenarios, whether the values of inert controls are included in the submission depends on implementation details. Don’t rely on this implicit behavior in critical flows—use explicit
disabledinstead.
6. Summary
inert consolidates three requirements that previously required multiple combined techniques—“block interaction, remove from the focus chain, hide from assistive technology”—into a single declarative attribute, greatly reducing the implementation complexity of modals, drawers, page caching, and similar scenarios. Now that modern browsers fully support it, new projects should prefer this native capability and save their effort for the actual business logic.
