Start by defining what “ready” means
The phrase “hidden until ready” causes trouble when ready is vague. A component is not ready just because a script tag has started downloading. It is ready when the markup it depends on exists, required data is available, dimensions are measured if needed, and the interactive UI is in a usable state.

Before hiding anything, decide which state you are controlling:
- Loading: the enhanced component is being prepared.
- Ready: initialization succeeded and the enhanced UI can be revealed.
- Fallback: JavaScript failed, timed out, or could not enhance the element.
That distinction matters because hiding content is easy; recovering it after a failed script is the part teams forget.
The correct use of the HTML hidden attribute
The native hidden attribute is a strong signal that an element is not currently relevant to the user. It is not merely a visual class. Apply it to enhanced UI that should not exist until JavaScript has finished setting it up.
<article
id="profile-card"
class="profile-card"
hidden
aria-busy="true"
aria-describedby="profile-card-status"
>
<h2>Profile preview</h2>
<div data-profile-content></div>
</article>
<p id="profile-card-status" class="sr-only" aria-live="polite">
Loading profile preview.
</p>
Do not put hidden on the only available content unless you have a reliable no-JavaScript fallback. A safer pattern keeps basic content available and hides only the JS-enhanced version.
<div id="profile-fallback">
<a href="/profile/42">View profile</a>
</div>
<article id="profile-card" class="profile-card" hidden aria-busy="true">
<!-- enhanced widget -->
</article>
The CSS display trap
A common surprise is that display: flex, display: grid, or even a utility class can override the browser’s default [hidden] { display: none; } rule. Always include this reset early in your component CSS:
[hidden] {
display: none !important;
}
The !important is deliberate here. It preserves HTML semantics even when component classes set another display value. If you need a state that is visually hidden but remains accessible, do not use hidden. Use a visually-hidden class instead.
.sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border: 0;
}
Where aria-busy helps—and where it does not
aria-busy="true" tells assistive technology that a region is being updated and that waiting may avoid announcing transient intermediate content. Clean it up when work finishes, whether it succeeds or fails.
It does not hide content visually, prevent clicks, or guarantee a loading announcement. Do not leave it on forever. A stale busy state can make screen readers wait for updates that never arrive.
card.setAttribute('aria-busy', 'true');
After initialization:
card.setAttribute('aria-busy', 'false');
For a failure, remove the busy state and reveal or replace the fallback. If you use a live region, write a concise completion or error message.
Reserve space and control CLS
Hiding a widget prevents a flash of unstyled content, but it does not automatically prevent layout shift. If a zero-height hidden element becomes a 320px card after loading, everything below it still moves.
Reserve the component’s footprint with a placeholder. Use fixed dimensions when they are known, or an aspect-ratio box when the width is fluid.
.profile-slot {
min-height: 18rem;
aspect-ratio: 4 / 3;
border: 1px solid #d7dce3;
border-radius: 14px;
background: #f5f7fa;
}
.profile-card {
min-height: 18rem;
border-radius: 14px;
background: #fff;
}
If the final height varies, measure before revealing. The catch: a hidden element cannot be measured because it has no box. You have three practical options:
- Reserve known space with CSS and reveal directly.
- Measure offscreen by temporarily moving or cloning the element without making it visible in the page flow.
- Measure in a hidden-but-rendered state using containment styles, then apply the measured height before making it visible.
The third option needs care. The element must not be display: none while measuring.
.is-measuring {
position: absolute !important;
width: var(--expected-width, 100%);
height: 0;
overflow: hidden;
visibility: hidden;
pointer-events: none;
}
Then measure offsetHeight or getBoundingClientRect(), set the placeholder height, remove the measuring state, and reveal.
What content-visibility is actually for
content-visibility: auto and contain-intrinsic-size are rendering performance tools. They let the browser skip rendering work for off-screen content while remembering an estimated size. They are useful for long pages with many heavy sections.
.article-section {
content-visibility: auto;
contain-intrinsic-size: auto 600px;
}
They are not a universal anti-flicker switch. They do not wait for your JavaScript, do not coordinate ready states, and should not replace hidden, loading placeholders, or initialization logic. Use them for off-screen rendering savings, not component readiness.
A safe initialization order
A reliable order looks like this:
- Render accessible HTML and a non-JS fallback first.
- Reserve layout space with CSS.
- Hide only the enhanced widget using
hidden. - Mark the widget as
aria-busy="true"while updating it. - Load or initialize the script.
- If measurement is required, temporarily create a renderable but invisible state.
- Measure the final size and apply it to the slot.
- Populate the widget.
- Remove
hidden, setaria-busy="false", and update the live region. - Hide the fallback only after the enhanced UI is usable.
If your script runs after DOMContentLoaded, the initial HTML is already parsed. For external dependencies, use the script’s load event, but also guard with a timeout or error handler. window.onload waits for all page resources, so it is usually too broad for a single component.
End-to-end example
Initial HTML
<div class="profile-slot">
<div id="profile-fallback" class="profile-fallback">
<p>Profile preview is unavailable.</p>
<a href="/profile/42">Open the profile page</a>
</div>
<article
id="profile-card"
class="profile-card"
hidden
aria-busy="true"
aria-describedby="profile-status"
>
<h2 id="profile-title">Profile preview</h2>
<div id="profile-content"></div>
</article>
</div>
<p id="profile-status" class="sr-only" aria-live="polite">Loading profile preview.</p>
CSS fallback and layout reservation
[hidden] {
display: none !important;
}
.profile-slot {
min-height: 18rem;
}
.profile-fallback,
.profile-card {
min-height: 18rem;
padding: 1rem;
border: 1px solid #d7dce3;
border-radius: 14px;
}
.sr-only {
position: absolute;
width: 1px;
height: 1px;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
}
Successful initialization, measurement, and reveal
const slot = document.querySelector('.profile-slot');
const card = document.querySelector('#profile-card');
const fallback = document.querySelector('#profile-fallback');
const status = document.querySelector('#profile-status');
const content = document.querySelector('#profile-content');
async function initProfileCard() {
const data = await fetch('/api/profile/42').then((r) => {
if (!r.ok) throw new Error('Profile request failed');
return r.json();
});
content.innerHTML = `
<p><strong>${data.name}</strong></p>
<p>${data.role}</p>
`;
// Optional measurement when final height is unknown.
card.hidden = false;
card.classList.add('is-measuring');
const height = card.offsetHeight;
slot.style.minHeight = `${height}px`;
card.classList.remove('is-measuring');
// Reveal the usable enhanced UI.
fallback.hidden = true;
card.setAttribute('aria-busy', 'false');
status.textContent = 'Profile preview loaded.';
}
initProfileCard().catch(() => {
card.hidden = true;
fallback.hidden = false;
card.setAttribute('aria-busy', 'false');
status.textContent = 'Profile preview could not be loaded.';
});
Because the card is briefly made renderable for measurement, keep it visually and interactively hidden until sizing is complete. If your height is fixed, skip that step entirely.
Failure and no-JavaScript plans
Never make critical content depend solely on a successful reveal. Links, forms, product details, and legal or checkout information need a fallback. With no JavaScript, the enhanced widget can remain hidden, so the basic link or server-rendered content must stay visible.
For script load failures, add an error handler or a timeout that removes blocking states. Also clean up ARIA: set aria-busy="false", remove stale live-region loading text, and move focus only when the failure requires the user to act immediately.
The goal is not to hide every pixel until JavaScript runs. The goal is to hide unstable enhancement, reserve stable space, and always leave users with a working path when the enhancement cannot finish.