Sub topic 14
Accessibility and find-in-page
This chapter assumes you know content-visibility: auto and hidden, and contain-intrinsic-size, from the earlier chapters. You should also be comfortable with the DOM, ARIA basics, and the browser's accessibility tree. The accessibility tree is the structure of roles, names, and states that browsers give to screen readers.
Skipping rendering is a performance feature, but it changes what users can reach. The two values behave differently, so you need to know which features keep working with each.
What "skipped" means for users
When the browser skips an element's contents, it does not lay out or paint them. The DOM nodes still exist, so scripts can read and modify them. What users can do with that content depends on the value you chose.
| Feature | `content-visibility: auto` (skipped) | `content-visibility: hidden` |
|---|---|---|
| Find-in-page | Matches are found | Not found |
| Tab and focus navigation | Reachable, which renders the section | Not reachable |
| Accessibility tree | Content stays exposed | Content is not exposed |
| Text selection and "select all" | Included | Not included |
| Fragment links (`#id`) | Work, and the browser renders the target | Do not reveal the content |
The auto value is designed so that skipping is invisible to users. The hidden value is designed to work like display: none while keeping rendering state, so users cannot reach the content.
Why `auto` is mostly safe
The browser treats an auto element as "relevant to the user" in several cases, and then it stops skipping it:
- It is on-screen or close to the viewport.
- Focus is inside it.
- A text selection is inside it.
- Something inside it is in the top layer, such as a modal dialog.
Find-in-page works because the browser can search the text without rendering it. It then renders the section holding the match and scrolls it into view. Tabbing works because the browser does not remove focusable descendants from the tab order. When focus moves into a skipped section, the section becomes relevant and is rendered.
Screen readers still see the content. A user browsing by headings or landmarks finds the headings inside skipped sections, because they remain in the accessibility tree.
Where `auto` still causes problems
Several issues remain even though the content stays reachable.
- Scroll position shifts. Skipped sections use the size from
contain-intrinsic-size. When a section renders at a different real height, the content below it moves. A user jumping to a find-in-page match can land slightly off, and the scrollbar thumb can jump. Use theautokeyword incontain-intrinsic-sizeso the browser remembers each real height after the first render. - Sizing estimates are not accurate for assistive tools. A screen reader does not depend on height, so this is mainly a visual issue. It can still disorient users who use magnification.
- Landmarks and headings inside skipped content. Exposure of skipped content has varied between browser versions. Test with a screen reader instead of assuming the tree is complete.
- Hidden-by-design content. If you apply
aria-hidden="true"ordisplay: noneyourself, that still applies.content-visibilitydoes not override it.
Why `hidden` needs extra care
With content-visibility: hidden, the content is not searchable and not focusable. Treat it as a hidden panel, not a performance hint. If you use it for a tab panel, you must still communicate state through ARIA, because the CSS alone does not tell assistive technology which tab is active.
<div role="tablist" aria-label="Report sections">
<button role="tab" id="tab-summary" aria-controls="panel-summary" aria-selected="true">Summary</button>
<button role="tab" id="tab-details" aria-controls="panel-details" aria-selected="false">Details</button>
</div>
<div role="tabpanel" id="panel-summary" aria-labelledby="tab-summary" class="panel is-open">
Summary content.
</div>
<div role="tabpanel" id="panel-details" aria-labelledby="tab-details" class="panel">
Details content.
</div>
<style>
.panel { content-visibility: hidden; }
.panel.is-open { content-visibility: visible; }
</style>
<script>
const tabs = document.querySelectorAll('[role="tab"]');
tabs.forEach((tab) => {
tab.addEventListener('click', () => {
tabs.forEach((other) => {
const selected = other === tab;
other.setAttribute('aria-selected', String(selected));
document
.getElementById(other.getAttribute('aria-controls'))
.classList.toggle('is-open', selected);
});
});
});
</script>The script keeps aria-selected and the visible panel in sync. The CSS only controls rendering. A user who searches the page for text in the Details panel will find nothing until that panel is opened.
Letting find-in-page reveal hidden content
Sometimes you want hidden content, such as a collapsed section, to be searchable. The hidden="until-found" attribute does this. The browser hides the content with content-visibility: hidden, but find-in-page can match it. When a match is in the hidden content, the browser removes the hidden attribute and fires a beforematch event on the hidden element. Fragment navigation to an element inside it reveals it too.
<section id="faq-shipping" hidden="until-found">
<h3>Shipping times</h3>
<p>Orders ship within two business days.</p>
</section>
<script>
const faq = document.getElementById('faq-shipping');
faq.addEventListener('beforematch', () => {
// Keep your own UI in sync, for example a toggle button's state.
document
.querySelector('[aria-controls="faq-shipping"]')
?.setAttribute('aria-expanded', 'true');
});
</script>Use the beforematch handler to update any aria-expanded state, because the browser changes visibility without going through your toggle code. Support for hidden="until-found" is not universal, so check current browser compatibility data. In browsers without it, the content stays hidden and unsearchable, which is the same result as plain hidden.
Choosing between the values
- Use
autofor long pages where all content should stay searchable and readable. It is a performance optimization with little change in behavior. - Use
hiddenfor content the user has not asked to see, such as inactive tabs, collapsed panels, or cached views. Add ARIA state yourself. - Use
hidden="until-found"for collapsed content that users might search for.
Testing checklist
- Use find-in-page for a phrase in a section far below the fold. Confirm the browser scrolls to it and highlights it.
- Tab through the page with the keyboard. Confirm focus enters skipped sections and the focus ring is visible.
- Open a URL with a fragment that targets an element inside a skipped section.
- Inspect the accessibility tree in DevTools and confirm headings and landmarks inside skipped sections are present.
- Run a screen reader and navigate by headings across the whole page.
Exercise
A long article uses content-visibility: auto on every section. A user searches for a word that appears only in a section far below the viewport. What does the browser do, and why is this different from using content-visibility: hidden on those sections?
Show answer
With auto, the browser can search the text of skipped sections without rendering them. When it finds the match, it marks that section as relevant, renders it, and scrolls the match into view. With hidden, the contents are not available to find-in-page, so the search finds nothing. If the sections were collapsed panels and should be searchable, hidden="until-found" would let the browser reveal them and fire beforematch.
Exercise
You build a tabbed interface where inactive panels use content-visibility: hidden. Which part of the accessibility work does the CSS not do for you?
Show answer
The CSS only skips rendering. It does not tell assistive technology which tab is selected or which panel it controls. You must set role="tab", aria-selected, aria-controls, and aria-labelledby, and update aria-selected in script when the user changes tabs.