
Searchable Collapsed Content with HTML hidden until-found
Collapsible sections, tabbed panels, and long documentation drawers help keep interfaces clean, yet hiding DOM content with display: none or hidden creates a major usability gap. When text is hidden from layout, the browser's built-in find-in-page search and URL text-fragment navigation skip right over it, leaving users convinced that relevant answers or settings do not exist on the page unless they manually open every panel first.
The WHATWG HTML hidden="until-found" attribute bridges this divide by hiding collapsed regions via content-visibility: hidden rather than removing them from the search index. When a user triggers find-in-page or navigates to a matching fragment inside a hidden="until-found" container, the browser automatically removes the hidden attribute, scrolls the matched text into view, and fires a beforematch event right before revealing the element so scripts can synchronize UI indicators like aria-expanded.
Example
<button type="button" aria-expanded="false" aria-controls="release-notes">
Toggle Release Notes
</button>
<div id="release-notes" hidden="until-found">
<p>Added hardware-accelerated WebGPU compute pipelines and reduced cold-start latency.</p>
</div>
<script>
const notes = document.getElementById('release-notes');
const toggle = document.querySelector('[aria-controls="release-notes"]');
notes.addEventListener('beforematch', () => {
toggle.setAttribute('aria-expanded', 'true');
});
</script>
Implementation
<section class="searchable-panels" aria-label="Documentation Sections">
<article class="panel">
<h2 class="panel-heading">
<button
type="button"
class="panel-trigger"
aria-expanded="false"
aria-controls="panel-caching"
>
Caching Strategy and Revalidation
</button>
</h2>
<div id="panel-caching" class="panel-content" hidden="until-found">
<p>
Configure <code>stale-while-revalidate</code> headers on immutable asset
endpoints so edge nodes serve cached responses immediately while
refreshing payloads in the background.
</p>
</div>
</article>
<article class="panel">
<h2 class="panel-heading">
<button
type="button"
class="panel-trigger"
aria-expanded="false"
aria-controls="panel-security"
>
Content Security Policy Nonces
</button>
</h2>
<div id="panel-security" class="panel-content" hidden="until-found">
<p>
Generate a cryptographically random per-request <code>nonce</code> and
attach it to inline script tags to block unauthorized script execution.
</p>
</div>
</article>
</section>
/* ==========================================================================
Accessible Searchable Disclosure Panels
========================================================================== */
.searchable-panels {
max-inline-size: 44rem;
display: grid;
gap: 0.75rem;
}
.panel {
border: 1px solid #cbd5e1;
border-radius: 0.625rem;
background: #ffffff;
}
.panel-heading {
margin: 0;
font-size: 1rem;
}
.panel-trigger {
inline-size: 100%;
display: flex;
justify-content: space-between;
align-items: center;
padding: 0.95rem 1.25rem;
border: 0;
background: transparent;
font: inherit;
font-weight: 600;
text-align: start;
color: #0f172a;
cursor: pointer;
}
.panel-trigger::after {
content: "+";
font-size: 1.25rem;
line-height: 1;
}
.panel-trigger[aria-expanded="true"]::after {
content: "−";
}
.panel-trigger:focus-visible {
outline: 2px solid #2563eb;
outline-offset: -2px;
border-radius: 0.625rem;
}
.panel-content {
padding: 0 1.25rem 1.15rem;
color: #334155;
line-height: 1.6;
}
/* Preserve UA content-visibility: hidden behavior for until-found */
.panel-content[hidden="until-found"] {
padding-block: 0;
}
/**
* Progressive Enhancement Controller for `hidden="until-found"` Panels
* Keeps button `aria-expanded` attributes synchronized across both manual clicks
* and automatic browser `beforematch` reveals during find-in-page or fragment jumps.
*/
const supportsUntilFound = 'onbeforematch' in document.createElement('div');
for (const trigger of document.querySelectorAll('.panel-trigger[aria-controls]')) {
const panelId = trigger.getAttribute('aria-controls');
const panel = panelId ? document.getElementById(panelId) : null;
if (!panel) continue;
const setExpanded = (expanded) => {
trigger.setAttribute('aria-expanded', String(expanded));
if (expanded) {
panel.removeAttribute('hidden');
} else {
panel.setAttribute('hidden', supportsUntilFound ? 'until-found' : '');
}
};
trigger.addEventListener('click', () => {
const isExpanded = trigger.getAttribute('aria-expanded') === 'true';
setExpanded(!isExpanded);
});
// Fired by the browser right before removing `hidden="until-found"` on search match
panel.addEventListener('beforematch', () => {
trigger.setAttribute('aria-expanded', 'true');
});
}
Main Point: Using hidden="until-found" together with the beforematch event keeps collapsed content searchable via browser find-in-page while letting disclosure controls maintain accurate accessibility state.
Credits
Photo by Yusuf Evli on Unsplash