Sub topic 13
content-visibility: hidden for tabs, panels, and cached views
This chapter assumes you know content-visibility: auto and contain-intrinsic-size from the earlier chapters, and that you can toggle classes with JavaScript.
What hidden does
content-visibility: hidden skips rendering the element's contents all the time, not only when they are off-screen. The element still takes part in layout as a box, but its children are not laid out or painted.
It looks like display: none, with one difference. With display: none the browser throws away the rendering state of the subtree. With content-visibility: hidden the browser may keep that state, such as style and layout results. Showing the panel again can then be faster than rebuilding it.
.panel {
content-visibility: hidden;
}
.panel.is-open {
content-visibility: visible;
}A working tab example
<button id="toggle">Toggle details</button>
<section id="details" class="panel">
<p>Expensive content goes here.</p>
</section>
<style>
.panel {
content-visibility: hidden;
contain-intrinsic-size: auto 300px;
}
.panel.is-open {
content-visibility: visible;
}
</style>
<script>
const panel = document.getElementById('details');
document.getElementById('toggle').addEventListener('click', () => {
panel.classList.toggle('is-open');
});
</script>The hidden value applies size containment, so the panel's size no longer depends on its children. Without a size, the box collapses in that dimension. Use contain-intrinsic-size to reserve space, or accept the collapse if you want the panel to disappear from the flow.
Choosing between auto and hidden
| Question | `auto` | `hidden` |
|---|---|---|
| Who decides when contents render? | The browser, based on viewport proximity | You, through the property value |
| Contents in the accessibility tree? | Yes | No |
| Found by find-in-page? | Yes | No |
| Typical use | Long scrolling pages | Tabs, collapsed panels, cached views |
Use hidden when the user has deliberately closed something, and auto when the content is simply far away.
Caveats
Hidden contents are not focusable, not exposed to assistive technology, and not searchable. That matches what a user expects from a closed tab, but not from content they might want to find. Do not use hidden for text that people should be able to search for.
Kept rendering state costs memory. If you keep dozens of large views hidden, you trade speed for memory. Measure before you decide to cache.
Changes made to a hidden subtree, such as inserting DOM nodes, are cheap. The browser defers the rendering work until the panel is shown. That makes hidden a good place to prepare a view before the user opens it.
Exercise
A settings dialog has five tabs. Only one is visible at a time, and users switch between them often. Which content-visibility value should the inactive tabs use, and what should you add so the tab area does not collapse?
Show answer
Use content-visibility: hidden on the inactive tabs, because the user has closed them and the browser can keep their rendering state for fast switching. Because hidden applies size containment, add contain-intrinsic-size (for example auto 300px) or an explicit size to reserve space. Check memory use if the tabs are large.