Favorites

Inert Global Attribute - What Does It Do?

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
Inert Global Attribute

What does inert do?

inert is a global attribute, which means you can add it to any HTML element. It switches off all interaction with that element and everything inside it, while leaving it visible on the page:

<div inert>
  <p>Nothing in here can be clicked, focused or selected.</p>
  <button>Not clickable</button>
</div>

When an element is inert:

  • It can't be clicked or tapped. Clicks pass through as if the content weren't there.
  • It can't receive focus. Links, buttons and form fields inside it are skipped when people press Tab.
  • Its text can't be selected, and it's skipped by the browser's find-in-page search.
  • It's hidden from assistive technology. Screen readers (software that reads pages aloud) and other tools act as if the content isn't there.

One thing inert doesn't do is change how the content looks. The browser adds no styling of its own, so it's up to you to show people that the content is inactive.

Try it out

Tick the box to make the section inert. Then try clicking the link, typing in the field, selecting the text, or pressing Tab to move through it:

Results

Some text you can select, with a link inside it.

The demo dims the section with a little CSS, because inert content otherwise looks exactly the same:

[inert] {
  opacity: 0.45;
}

In JavaScript, every element has an inert property you can set to true or false:

const section = document.querySelector('#settings');

section.inert = true;   // switch off
section.inert = false;  // switch back on

When to use inert

inert is designed for regions of a page that are temporarily out of action. Typical examples:

  • Content behind a custom overlay, such as a slide-out menu or a full-screen search panel. While the overlay is open, the page behind it should be inert, so keyboard and screen reader users can't wander into it by accident.
  • Off-screen content, such as a closed menu that's been moved out of view, or the hidden slides of a carousel. Without inert, keyboard users can tab into links they can't see.
  • A section that's busy, for example while a form is being submitted or new results are loading, so people can't change things halfway through.

When not to use inert

  • Not for decorative elements. The original version of this post suggested marking decorative content as inert. Decorative images should have empty alternative text (alt=""), and other purely visual extras can use aria-hidden="true". Making visible text inert hides it from screen readers while sighted people can still read it, which leaves some visitors missing information.
  • Not for single form controls. To switch off one button or field, use the disabled attribute. Disabled controls are still announced to screen reader users as "dimmed" or "unavailable", so they know the control exists, whereas inert ones vanish completely.
  • Not for content people still need. If someone needs to read it, it shouldn't be inert.
  • Usually not needed for modal dialogs. The <dialog> element handles this for you, as we'll see shortly.

Example: a slide-out menu

A slide-out (off-canvas) menu is a perfect job for inert. When the menu is closed, it's moved off-screen, but its links would still be reachable with the keyboard. When it's open, the page behind it should be out of reach. Open the menu below, then press Tab: focus moves through the menu links and skips the link and search field in the "page" behind it. Press Escape or the Close button to shut it, and focus returns to the Open menu button.

Results

This is the page behind the menu. It has a link and a field:

<button type="button" class="menu-toggle" aria-expanded="false" aria-controls="menu">Open menu</button>

<nav id="menu" class="menu" aria-label="Main" inert>
  <button type="button" class="menu-close">Close</button>
  <ul>
    <li><a href="/">Home</a></li>
    <li><a href="/blog/">Blog</a></li>
  </ul>
</nav>

<main id="page">…</main>
const toggle = document.querySelector('.menu-toggle');
const closeButton = document.querySelector('.menu-close');
const menu = document.querySelector('#menu');
const page = document.querySelector('#page');

function setMenu(open) {
  menu.classList.toggle('is-open', open);
  menu.inert = !open;      // closed menu: out of reach
  page.inert = open;       // open menu: page behind is out of reach
  toggle.setAttribute('aria-expanded', open);

  // Move focus to where the person needs it
  if (open) {
    closeButton.focus();
  } else {
    toggle.focus();
  }
}

toggle.addEventListener('click', () => setMenu(true));
closeButton.addEventListener('click', () => setMenu(false));
document.addEventListener('keydown', (event) => {
  if (event.key === 'Escape' && !menu.inert) setMenu(false);
});

Focus matters: if the element that currently has focus becomes inert, the browser drops focus back to the start of the page, and keyboard users lose their place. Always move focus somewhere sensible before or as you make a region inert, as the setMenu() function does.

In the code, the toggle button sits outside #page, so it isn't made inert when the menu opens. On a real site, you'd usually make everything except the menu inert (the header, main content and footer), so focus can't leave the open menu at all. In the small demo above, only the demo's own "page" area is inert, so after the last menu link, Tab carries on into the rest of this article.

Modal dialogs: let <dialog> do it

The original version of this post built a modal by hand, setting inert on the page content when the modal opened. Today there's a simpler way: open a <dialog> element with showModal(), and the browser automatically makes everything else on the page inert until it closes. It also moves focus into the dialog, closes it with Escape, and returns focus to the button that opened it.

Results

Everything else is inert

While this dialog is open, the rest of the page can't be clicked or reached with the keyboard. Try pressing Tab, or clicking the page behind.

<button type="button" id="open-dialog">Open dialog</button>

<dialog id="my-dialog" aria-labelledby="dialog-title">
  <h3 id="dialog-title">Everything else is inert</h3>
  <p>Try pressing Tab, or clicking the page behind.</p>
  <form method="dialog">
    <button>Close</button>
  </form>
</dialog>
document.querySelector('#open-dialog').addEventListener('click', () => {
  document.querySelector('#my-dialog').showModal();
});

A button inside a <form method="dialog"> closes the dialog without any JavaScript. Save manual inert for layouts like the slide-out menu, where you aren't using a dialog.

inert, hidden, disabled and aria-hidden

These four attributes are easy to confuse. Here's how they differ:

AttributeVisible?Clickable or focusable?Read by screen readers?
inertYesNoNo
hiddenNoNoNo
disabled (form controls only)YesNoYes, announced as unavailable
aria-hidden="true"YesYes, still clickable and focusableNo

Browser support and best practices

The inert attribute has worked in all major browsers since April 2023, when Firefox added support (Chrome and Edge added it in 2022, and Safari in 15.5). It's safe to use on everyday websites.

  • Show it visually. Dim or blur inert content, or cover it with an overlay, so sighted people understand why it isn't responding.
  • Manage focus. Move focus into the newly active area when you make a region inert, and put it back when you're done.
  • Keep the way out reachable. Never make the control that undoes the inert state (such as a Close button) inert itself.
  • Use the right tool: <dialog> for modals, disabled for single controls, alt="" or aria-hidden for decoration, and inert for whole regions that are temporarily out of action.
  • Test with a keyboard. Press Tab through your page with the region active and inactive to check that focus goes where you expect.

For browser compatibility details and another example, see our reference page for the inert attribute, or browse our complete list of HTML attributes.

Wrapping up

The inert attribute switches off a whole region of a page in one go: no clicking, no focusing, no selecting, and nothing for screen readers to announce, while the content stays visible. Use it for content behind overlays, off-screen panels and busy sections, style it so people can see it's inactive, and manage focus carefully. For modal dialogs, let <dialog> and showModal() handle it for you.

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee