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 previous chapters. It also assumes you can write basic JavaScript to toggle a class. The goal is to know when content-visibility: hidden is better than display: none, and how to use it without breaking accessibility or layout.
What hidden does
content-visibility: hidden gives the element the same containment as auto and always skips its contents. The browser applies layout, style, paint, and size containment, and it never renders the descendants.
The difference from auto is control. With auto, the browser decides when to skip. With hidden, you decide. The element stays skipped until you change the value.
The difference from display: none is what the browser keeps. With display: none, the element and its subtree generate no boxes. When you show it again, the browser rebuilds the boxes, then runs layout and paint. With content-visibility: hidden, the element still generates its own box. The browser may keep the rendering state of the subtree, such as style and layout results, so showing it again can be much cheaper.
The three common ways to hide content compare like this:
| Property | Element's own box | Space reserved | Rendering state of the subtree | Reachable by find-in-page and focus |
|---|---|---|---|---|
| `display: none` | Removed | No | Discarded | No |
| `visibility: hidden` | Kept | Yes, full content size | Kept, and still computed | No |
| `content-visibility: hidden` | Kept | Only what you reserve | Kept, but not updated while skipped | No |
visibility: hidden still pays for layout and paint preparation of the full subtree on every change. content-visibility: hidden skips that work while keeping the cached result.
A tabbed interface
The usual use case is a set of panels where only one is visible. You can use display: none for the inactive panels. That works, but each tab switch rebuilds the panel. For heavy panels such as large tables, charts, or long lists, that rebuild is noticeable.
This page runs as written. Save it as an HTML file and open it in a browser.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Tabs with content-visibility</title>
<style>
.panel {
content-visibility: hidden;
contain-intrinsic-size: auto 400px;
}
.panel.is-open {
content-visibility: visible;
}
</style>
</head>
<body>
<div role="tablist">
<button role="tab" id="tab-a" aria-controls="panel-a" aria-selected="true">Orders</button>
<button role="tab" id="tab-b" aria-controls="panel-b" aria-selected="false">Reports</button>
</div>
<section class="panel is-open" role="tabpanel" id="panel-a" aria-labelledby="tab-a">
<p>Orders table goes here.</p>
</section>
<section class="panel" role="tabpanel" id="panel-b" aria-labelledby="tab-b">
<p>Reports charts go here.</p>
</section>
<script>
const tabs = document.querySelectorAll('[role="tab"]');
tabs.forEach((tab) => {
tab.addEventListener('click', () => {
tabs.forEach((t) => {
const open = t === tab;
t.setAttribute('aria-selected', String(open));
document.getElementById(t.getAttribute('aria-controls'))
.classList.toggle('is-open', open);
});
});
});
</script>
</body>
</html>The CSS has two rules. The base rule hides every panel, and .is-open overrides it. You could write content-visibility: visible as the default and skip the override, but the explicit value makes the toggle easy to read.
Why the size reservation matters
hidden applies size containment. A skipped element is laid out as if it were empty. Without a reserved size, the hidden panel collapses to zero height, plus its padding and border.
For tabs this is usually fine, because the hidden panel should not take space. In the example, contain-intrinsic-size: auto 400px only matters if you want the hidden panel to occupy room, for example in a stacked layout of collapsed sections. In that case, the auto keyword remembers the last rendered size, so the placeholder matches reality after the first display.
In a tabbed layout, you normally want the hidden panels out of the flow. Combine hidden with position: absolute or a grid that stacks panels in one cell, or accept the zero height. Choose one approach on purpose, because the hidden panel's size otherwise depends on its padding and border only.
Cached views in single-page apps
The same idea fits a single-page application (SPA) that keeps recent views alive. Instead of destroying a route's DOM when the user navigates away, you keep it and mark it hidden. Going back then reuses the cached rendering state.
- The scroll position inside the view may not be preserved, so test it. Store and restore
scrollTopyourself when it matters. - DOM updates made while the view is skipped are cheap, because the browser defers style and layout work until the view is shown.
- Memory use grows. The browser keeps state for every cached view, so cap the number of cached views.
- Form values, JavaScript state, and event listeners stay alive, since the DOM is still there. Timers and network polling in a hidden view keep running unless you stop them.
Accessibility and find-in-page
Skipped contents are not available to user-agent features. For hidden, that means:
- Find-in-page does not match text inside the panel.
- Keyboard focus cannot enter it, and tab order skips its controls.
- Text selection cannot cross into it.
- It is not exposed to assistive technology, so screen readers do not read it.
This is the behavior you want for inactive tabs, because users should not reach content that is not shown. It is a problem if you use hidden for content that users expect to find. The hidden="until-found" HTML attribute supports that case in browsers that implement it. It lets find-in-page reveal the content and fires a beforematch event, which you can use to open the right panel.
Check the result in the browser's accessibility tree view rather than assuming. Support and exposure details differ between browsers.
Measuring the benefit
Do not assume hidden beats display: none for your page. Measure it.
- Open the Performance panel in DevTools and start recording.
- Switch tabs several times, then stop recording.
- Compare the "Recalculate Style", "Layout", and "Paint" entries after each switch, using a version with
display: noneand a version withcontent-visibility: hidden. - Repeat with CPU throttling at 4x or 6x to approximate a slower device.
The gain is largest for panels with thousands of nodes. For a small panel, the difference is lost in noise, and the extra memory is not worth it.
Pitfalls
- Collapsed size. A hidden element with no reserved size has zero content height. Layout that depended on that space shifts when you show the panel. Reserve space with
contain-intrinsic-sizeif needed. - Clipped overflow. Containment includes paint containment, so descendants such as dropdowns and tooltips inside a panel are clipped to its box when it is visible. This happens with
visibletoo if you keep the containing styles, so test popups. - Unreachable content. Anything inside a hidden panel cannot take focus. Move focus to the newly opened panel or its tab on switch.
- Browser support. Unsupported browsers ignore the property and show every panel. Use
@supports (content-visibility: hidden)to apply the hiding rule only where it works, and keep a fallback such as thehiddenattribute ordisplay: nonefor the rest.
.panel {
display: none;
}
.panel.is-open {
display: block;
}
@supports (content-visibility: hidden) {
.panel {
display: block;
content-visibility: hidden;
}
.panel.is-open {
content-visibility: visible;
}
}Exercise
A dashboard has five heavy tab panels, and users report a visible delay each time they switch tabs. You change the inactive panels from display: none to content-visibility: hidden. Name two benefits you expect and two things you must check afterward.
Show answer
Expected benefits: the browser can reuse cached rendering state, so showing a panel needs less layout and style work, and DOM updates made while a panel is hidden are deferred. Things to check: the hidden panels' size (they collapse unless you reserve it with contain-intrinsic-size) and accessibility (hidden content is not focusable, findable, or exposed to screen readers, so ARIA tab attributes and focus management must still be correct). Also measure with the Performance panel, because the gain may be small and memory use may rise.