Full answer
The hidden attribute says that an element is not relevant right now. The browser's built-in stylesheet turns it into display: none, so the element is not drawn, not focusable and not read by screen readers. It is a good, simple way to hide a panel, an error message or a dialog until it is needed. A script can change it with el.hidden = true or false.
The gotcha: the built-in rule is a very weak rule, [hidden] { display: none } in the browser's own style sheet. Any author rule that sets display on the same element has a higher priority. If your CSS says .panel { display: flex; }, then a .panel with hidden is still visible. The attribute is present, but nothing hides the element. This is a classic bug with flex and grid components.
Fixes:
- Add a rule
[hidden] { display: none !important; }to your base styles. This is one of the few good uses of!important. - Or write component rules so that they do not set
displayon the hidden state, for example.panel:not([hidden]) { display: flex; }.
Compared with a class that sets display: none, hidden is shorter, has a clear meaning and is respected by assistive technology in the same way. Use CSS classes when the hiding depends on screen size, because hidden is not responsive.
There is also hidden="until-found". The content stays hidden, but find-in-page and links to a fragment can reveal it, and the browser fires a beforematch event. It is useful for collapsed text that should still be searchable. It is a newer feature, so check the support table and expect plain hidden behaviour in browsers that do not know it.
The first panel is shown even though it has hidden. The second is hidden because an author rule with the same priority brings back display: none. The third is hidden by the browser's default rule.
Add one global hidden rule so you never see this bug.
<style>
.panel { display: flex; padding: 4px; background: #cde; }
.fixed[hidden] { display: none; }
</style>
<div class="panel" id="a" hidden>Still visible because of display: flex</div>
<div class="panel fixed" id="b" hidden>Really hidden</div>
<div id="c" hidden>Plain hidden</div>Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- hidden maps to display: none in the browser's style sheet.
- Author display rules override it.
- Add a global [hidden] rule.
- Use classes for responsive hiding.
- until-found keeps content searchable; check support.
Common mistakes
- Using hidden on an element that also has an author display rule, and expecting it to disappear.
- Using hidden for something that should hide only on small screens.
- Hiding important content with hidden and expecting find-in-page to reach it.
Follow-ups
- Why is hidden content not read by screen readers?
- What does hidden="until-found" change?
Tips
- Explain that hidden is a weak default rule, so author display rules beat it.
- Give the global [hidden] rule as the fix.