brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: the inert attribute switches off a whole part of a page

Web
~/tips/tip-html-inert-attribute

...the page behind the drawer...

Makes part of a page unclickable, unfocusable and hidden from screen readers with one attribute.

When a drawer or side panel is open, the page behind it should stop reacting. For years that meant tabindex="-1" on every link, aria-hidden and a pile of JavaScript. Now it’s one attribute.

<main inert>
  ...the page behind the drawer...
</main>
<aside class="drawer">...</aside>

According to MDN, an element with inert and everything inside it can’t be focused or clicked, isn’t found by the browser’s find-in-page, doesn’t allow text selection and is removed from the accessibility tree. Tab skips straight over it. MDN lists it as Baseline, available in all major browsers since April 2023.

In JavaScript it’s a boolean property, so opening and closing the drawer is two lines:

document.querySelector("main").inert = true;   // drawer opens
document.querySelector("main").inert = false;  // drawer closes

For a real modal you don’t even need this: a <dialog> opened with showModal() already handles the rest of the page. inert is for the cases in between, like drawers, a page that’s loading, or a step in a form that isn’t active yet.

Caveat: inert content looks exactly the same as before. MDN says it’s your job to show visually what is active and what isn’t, for example by dimming it. And for one single button, the disabled attribute is the better choice.

Further reading: MDN: inert

More Web tips async function sha256(text) { url = URL.createObjectURL(file); function setFilter(name, value) { and open <span translate="no">Home Assistant</span> in your browser. inputmode="numeric" autocomplete="one-time-code"> All Web tips (6) All topics

Link