Many interactive components start with the same pile of JavaScript: trap focus in a modal, close a layer on outside click, wire up a search input, then manually restore focus later. Modern HTML can take over some of that work. The inert attribute, the popover attribute, and the search input type do not replace every script, but they remove a surprising amount of state management from common UI components.

This guide walks through all three from a practical starter point: what each one does, where it helps, and how to combine them in a modal, popover, and search box.
1. Use inert to quiet the rest of the page
The inert attribute makes a section of the DOM unable to receive interactive input. Links, buttons, form controls, and other focusable elements inside an inert subtree are removed from the tab order and are not exposed as interactive controls to assistive technology. The browser will also ignore clicks in that area.
This is especially useful when a modal is open. Without inert, keyboard users can tab out of the dialog and reach page content behind the overlay. A common fix is a focus trap, but focus traps add code and edge cases. A simpler pattern is to mark the background page as inert while the modal is active.
Basic inert markup
Here is the shape of an app shell:
<body>
<div id="app" class="app-shell">
<header>...</header>
<main>...</main>
</div>
<dialog id="settings-dialog">
<h2>Settings</h2>
<button id="close-settings">Close</button>
</dialog>
</body>
When the dialog opens, add inert to the app shell:
const app = document.querySelector('#app');
const dialog = document.querySelector('#settings-dialog');
document.querySelector('#open-settings').addEventListener('click', () => {
dialog.showModal();
app.inert = true;
});
dialog.addEventListener('close', () => {
app.inert = false;
});
Because the dialog is rendered in its own top layer, it stays interactive even though the main app shell is inert. When the dialog closes, remove the state so the page becomes usable again.
Why this is cleaner than a hand-rolled focus trap
- You do not need to collect every focusable selector in the page.
- You do not need to wrap focus from the last item back to the first.
- Background controls are consistently unavailable to keyboard, mouse, and assistive tech.
- The state lives directly on the DOM instead of in parallel JavaScript flags.
Use inert carefully. It is a strong attribute. If you accidentally apply it too high in the DOM, you can disable UI that should remain available. Put it on the background container, not on the whole body, when the modal itself lives in that body.

2. Use popover for lightweight layers
The popover attribute gives you a browser-managed floating layer. A popover can be shown and hidden without manually toggling classes, stopping outside-click events, or managing z-index battles. The browser places active popovers in the top layer, above ordinary content.
There are two display values:
- Automatic popover: light-dismiss behavior; only one can be shown at a time; opening another closes it.
- Manual popover: stays open until your code or a close button hides it; multiple manual popovers can be open.
Minimal popover example
<button popovertarget="user-menu" popovertargetaction="toggle">
Account
</button>
<div id="user-menu" popover>
<button>Profile</button>
<button>Billing</button>
<button popovertarget="user-menu" popovertargetaction="hide">Close</button>
</div>
That is enough for a working menu. Clicking the button toggles the layer. Pressing Esc closes an automatic popover, and clicking outside also dismisses it. There is no required click handler for basic open and close behavior.
Style it without fighting positioning
By default, a popover appears in the center of the viewport. For a small menu, you may want it anchored near its trigger:
#user-menu {
position: anchor(--account-button);
top: anchor(--account-button bottom);
left: anchor(--account-button start);
margin-top: 0.5rem;
min-width: 12rem;
border: 1px solid #d4d4d8;
border-radius: 0.75rem;
padding: 0.5rem;
background: white;
box-shadow: 0 12px 30px rgb(0 0 0 / 12%);
}
Anchor positioning is still worth checking in your target browsers. If you need broader support, keep a small positioning fallback with JavaScript or a wrapper, while still using popover for show, hide, top-layer rendering, and dismissal.
Choose dialog, popover, or both
Use <dialog> for a modal task that demands attention and has a clear semantic role. Use popover for transient UI such as menus, toasts, pickers, command palettes, and inline help.
They can also work together. A command palette may use popover behavior for quick dismissal while your script marks the rest of the page inert if you want modal-like blocking. For ordinary dropdown menus, that is usually unnecessary; the light-dismiss behavior is part of why popover feels clean.
3. Use type="search" for semantic search fields
For years, many search boxes were written as plain type="text" inputs. The native search type communicates the purpose of the field and gives the browser room to provide useful built-in behavior.
<form role="search" id="site-search">
<label for="query">Search articles</label>
<input
id="query"
type="search"
name="q"
placeholder="HTML attributes, CSS layout..."
autocomplete="off"
spellcheck="false"
>
<button type="submit">Search</button>
</form>
Browsers may render a clear control, adjust spelling or autocorrection behavior, and present a suitable input context. More importantly, the markup says what the control is for. Pair it with role="search" on the form when the form represents a major search region, and always include a visible or accessible label.
Do less JavaScript with native form behavior
If your search has a real results page, let the form submit normally:
<form action="/search" method="get" role="search">
<input type="search" name="q" required>
<button>Search</button>
</form>
The query appears in the URL as ?q=term, the browser handles validation when the field is required, and you avoid a custom submit handler entirely. This also gives users reloadable, shareable search URLs.
Enhance progressively for suggestions
For an autocomplete UI, listen to the input event and render results into a popover:
<form role="search">
<input id="query" type="search" name="q" aria-controls="search-results">
<div id="search-results" popover="manual"></div>
</form>
<script>
const input = document.querySelector('#query');
const results = document.querySelector('#search-results');
input.addEventListener('input', async () => {
const q = input.value.trim();
if (!q) {
results.hidePopover();
results.replaceChildren();
return;
}
const items = await fetchSuggestions(q);
results.replaceChildren(renderSuggestions(items));
results.showPopover();
});
</script>
JavaScript still handles fetching and rendering data, but it no longer has to create the overlay, manage escape-key behavior, or fight stacking contexts. Keep arrow-key navigation and aria-activedescendant if you are building a full combobox; popover alone does not turn a suggestion panel into an accessible autocomplete widget.
A practical component pattern: search trigger plus command popover
Here is how these features can meet in one component. A search button opens a command palette. The palette is a popover, the input is a native search field, and the page behind can be inert while the palette is open.
<div id="page">
<button popovertarget="command-palette">Search</button>
<main>page content</main>
</div>
<div id="command-palette" popover="manual" class="palette">
<form role="search">
<label for="command-input">Search components and pages</label>
<input id="command-input" type="search" name="q">
</form>
<ul id="suggestions"></ul>
</div>
A small script can coordinate the extra behavior:
const page = document.querySelector('#page');
const palette = document.querySelector('#command-palette');
const input = palette.querySelector('input');
palette.addEventListener('beforetoggle', (event) => {
page.inert = event.newState === 'open';
});
palette.addEventListener('toggle', (event) => {
if (event.newState === 'open') input.focus();
});
The result is a clear split. HTML owns the semantic control and the layer. CSS owns appearance. JavaScript only handles application-specific behavior such as loading results, choosing keyboard behavior, and toggling inert state.
Browser support and fallback habits
Before shipping, check support against your audience. inert and type="search" are straightforward to use broadly, while popover and CSS anchor positioning have different support timelines across browsers.
A sensible fallback strategy is:
- Build the core interaction with semantic HTML and a normal form or button.
- Test whether the advanced API exists, for example by checking
HTMLElement.prototype.showPopover. - Initialize the enhanced behavior only in supporting browsers.
- Provide a simpler page, dialog, or custom-layer experience where needed.
Do not make the entire component depend on the newest feature if a meaningful number of users cannot use it. Progressive enhancement keeps the UI useful before the fancy layer behavior kicks in.
Start with these replacements
Next time you build a common component, look for JavaScript that is mostly compensating for missing browser behavior:
- Modal: try
<dialog>plusinerton the background. - Dropdown or tooltip-like layer: try
popoverbefore adding a z-index stack. - Search: use
type="search", a real form, and add scripting only for live suggestions.
These native features will not cover every design requirement, but they are excellent first tools. Reach for them, inspect the rendered behavior with your keyboard and screen reader, and add custom code only where the platform stops short. That is how component code gets smaller without giving up control.