Web Development

The HTML inert Attribute - Make a Subtree Unavailable Without Removing It

The HTML inert Attribute - Make a Subtree Unavailable Without Removing It

A panel can look unavailable and still be reachable with the Tab key. A dimmed form can still expose its controls to assistive technology. An off-screen drawer can remain in the keyboard focus order even though a mouse user cannot see it. These mismatches are easy to create when an interface changes only its CSS.

The HTML inert attribute addresses that specific problem: it makes an element's subtree unavailable for user interaction without requiring the subtree to be removed from the document. That sounds simple, but the useful question is not merely how to spell the attribute. It is what the browser does for us, what it does not do, and when another mechanism is the better choice.

What an inert subtree means

inert is a Boolean global HTML attribute. When it is present, the element and its applicable flat-tree descendants become inert. The flat-tree detail matters for composed content such as slots, although most ordinary page structures can be understood as "this element and the content inside it."

Under the WHATWG definition of an inert subtree, the effect is broader than blocking a mouse click. Hit-testing behaves as though pointer events were disabled; text selection behaves as though it were disabled; editable content becomes non-editable; and user agents should ignore the content for find-in-page. Inert nodes generally cannot receive focus and are not exposed through accessibility APIs.

That bundle of behavior is the main value. A developer does not have to find every link, button, input, and contenteditable descendant and modify each one separately. The state belongs to the region.

There is an important qualification: inert has no persistent visual presentation of its own. The browser does not necessarily dim, hide, or label the region. The HTML Standard therefore advises authors to make inactive content visually obscured or otherwise clearly distinguishable. Interaction state and visual communication have to agree.

Why a transparent overlay or CSS rule is not equivalent

A common implementation places an overlay above a panel or applies pointer-events: none. That may stop pointer hit-testing, but it does not describe the complete state. Keyboard focus, accessibility exposure, text editing, and search are separate concerns. A region can become unclickable while its links remain in the sequential focus order.

This does not make overlays or CSS wrong. They can provide the visual half of the design. The mistake is treating a visual layer as if it also changed the document's interaction semantics. In many interfaces, the robust state is a pair: inert communicates unavailability to the browser, while CSS and nearby status text communicate it to people.

A small example: an unavailable settings panel

Suppose account settings should remain visible while a prerequisite is loading, but the controls must not be usable yet. The initial HTML can express that state directly:

<section aria-labelledby="settings-title">
  <h2 id="settings-title">Account settings</h2>
  <p id="settings-status" role="status">Loading current settings...</p>

  <form id="settings-panel" inert>
    <label>
      Display name
      <input name="display_name" autocomplete="name">
    </label>
    <button type="submit">Save changes</button>
  </form>
</section>

When the prerequisite is ready, script can update the DOM property and the visible status:

const panel = document.querySelector('#settings-panel');
const status = document.querySelector('#settings-status');

panel.inert = false;
status.textContent = 'Settings are ready.';

The HTMLElement.inert property reflects the content attribute. Setting it to false removes the inert state; setting it to true applies it. The status message is not decoration: it explains why controls that remain visible cannot be used. A real application still needs failure and retry states rather than assuming loading always succeeds.

This example deliberately does not call inert a submission lock. Client-side state can reduce accidental repeated interaction, but it cannot authorize an action, validate trusted data, or guarantee that another request will not reach the server. Those checks still belong on the server.

Native modal dialogs already use inertness

The most familiar inert region is the page behind a modal dialog. When a native <dialog> is opened with showModal(), the browser puts it in the top layer and makes the rest of the connected document inert. This is different from dialog.show(), which opens a non-modal dialog and leaves surrounding content interactive.

<button id="open-delete" type="button">Delete draft</button>

<dialog id="delete-dialog" aria-labelledby="delete-title">
  <h2 id="delete-title">Delete this draft?</h2>
  <p>This action cannot be undone.</p>
  <form method="dialog">
    <button value="cancel" autofocus>Cancel</button>
    <button value="confirm">Delete</button>
  </form>
</dialog>

<script>
  const dialog = document.querySelector('#delete-dialog');
  document.querySelector('#open-delete').addEventListener('click', () => {
    dialog.showModal();
  });
</script>

Using the native mechanism avoids manually marking every sibling inert. It also does not remove the need to design the dialog. The HTML dialog specification asks authors to think carefully about initial focus, and the W3C modal dialog pattern covers an accessible name, contained keyboard focus, a clear close path, and sensible focus placement after closing. In the destructive example, focusing Cancel is a design choice, not a universal rule.

In short, browser-managed inertness handles the background. It does not decide the wording, label, safest initial action, or logical next focus for every workflow.

Choose the mechanism that matches the state

Several HTML, ARIA, and CSS features can make something appear unavailable, but they are not interchangeable.

hidden: the region is not currently relevant

Use hidden when content should not be rendered because it is not currently relevant. Use inert when keeping a region visible or rendered is useful, but interacting with it is temporarily inappropriate. Hiding an inactive panel may be simpler; inertness is not automatically the better choice.

disabled: an individual supported control is unavailable

For an individual form control, disabled normally communicates intent more precisely and provides the :disabled styling hook. The HTML Standard specifically warns that applying inert to individual controls is usually less appropriate. inert becomes useful when the state covers a mixed subtree containing controls, links, editable content, and ordinary text.

aria-hidden="true": accessibility exposure changes

aria-hidden="true" is not a general interaction lock. It can remove content from the accessibility tree while leaving descendants clickable or keyboard-focusable, creating contradictory behavior. The web.dev explanation of inert highlights this difference: inertness coordinates discoverability and interaction rather than changing only accessibility exposure.

pointer-events: none: pointer hit-testing changes

This CSS declaration can be useful for presentation and pointer behavior, but it is not a keyboard or accessibility model. Use it when pointer hit-testing is genuinely the only concern, not as a substitute for an unavailable interface subtree.

Focus still needs a plan

Making a region inert answers where focus cannot go. It does not always answer where focus should go next. Before changing a region's state, consider whether focus is currently inside it, what triggered the change, and which remaining element represents the next logical step.

For a modal, that plan includes moving focus into the dialog and normally returning it to the invoking control when the dialog closes. For a loading panel, it may mean leaving focus on the control that initiated loading and announcing the state through an appropriate status message. The correct destination depends on the workflow; blindly focusing the first available element can be as confusing as allowing focus into the inactive region.

Test behavior, not just markup

An inert attribute visible in developer tools is only the beginning of verification. A useful test pass asks concrete questions:

  • Can Tab or Shift+Tab still enter the inactive region?
  • Can pointer input activate one of its controls?
  • Is the active region and the reason for inactivity understandable visually?
  • Does a screen reader encounter controls that are meant to be unavailable?
  • When the state changes, does focus remain somewhere logical and visible?
  • Can every modal be dismissed through an obvious control and expected keyboard behavior?

Find-in-page deserves cautious expectations. The standard says user agents should ignore inert nodes for this feature and may permit users to override restrictions. It is better to treat this as part of the intended browser behavior, not as a way to conceal sensitive words. Sensitive content should not be sent to an unauthorized client in the first place.

A boundary rather than a complete component

The clearest mental model is that inert draws an interaction boundary around a subtree. It is stronger and more coherent than a pointer-only CSS workaround, yet narrower than a complete interface component. It does not provide a loading message, visual styling, dialog label, close button, focus destination, authorization check, or server-side validation.

That narrowness is useful. Use disabled for one supported control, hidden for content that should not be presented, native showModal() when the interface is truly modal, and inert when a rendered region needs to become unavailable as a whole. The result is not automatically accessible, but the browser and the interface are at least describing the same state.

References