How to Build Zero-Layout-Shift FAQ Accordions with CSS Grid (0fr to 1fr) & Live Search
Modern CSS animates unknown accordion content heights cleanly by transitioning CSS grid rows: set the wrapper to `display: grid; grid-template-rows: 0fr; transition: grid-template-rows 0.35s ease;` and animate to `grid-template-rows: 1fr;` when open, wrapping the child inside `overflow: hidden;`.
Step 1: Structure the Semantic FAQ Items with Details/Summary
Use HTML5 details and summary tags containing a CSS grid wrapper around the answer content.
<div class="faq-accordion-widget" id="faqWidget">
<div class="faq-search-box">
<input type="search" id="faqSearchInput" placeholder="Filter questions (e.g. refund, billing, api)..." aria-label="Search FAQ">
</div>
<div class="faq-list">
<details class="faq-item">
<summary class="faq-question">
<span>Can I use these widgets in commercial SaaS applications?</span>
<span class="faq-icon" aria-hidden="true">+</span>
</summary>
<div class="faq-content-grid">
<div class="faq-content-inner">
<p>Yes. All generated code snippets and Web Components are 100% MIT licensed for commercial and personal usage with zero royalty obligations.</p>
</div>
</div>
</details>
</div>
</div>
Step 2: Apply CSS Grid 0fr to 1fr Transition Rules
Animate grid-template-rows to smoothly expand and collapse dynamic content of arbitrary height.
.faq-item {
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
padding: 16px 0;
}
.faq-question {
display: flex;
justify-content: space-between;
align-items: center;
font-weight: 600;
font-size: 1.05rem;
color: #fff;
cursor: pointer;
list-style: none;
}
.faq-question::-webkit-details-marker {
display: none;
}
.faq-icon {
font-size: 1.4rem;
transition: transform 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}
.faq-item[open] .faq-icon {
transform: rotate(45deg);
}
.faq-content-grid {
display: grid;
grid-template-rows: 0fr;
transition: grid-template-rows 0.35s cubic-bezier(0.16, 1, 0.3, 1);
}
.faq-item[open] .faq-content-grid {
grid-template-rows: 1fr;
}
.faq-content-inner {
overflow: hidden;
color: #94a3b8;
line-height: 1.6;
padding-top: 12px;
}
Step 3: Add Instant In-Card Keyword Filtering
Filter visible questions as the user types into the search input with zero latency.
const searchInput = document.getElementById('faqSearchInput');
const items = document.querySelectorAll('.faq-item');
searchInput.addEventListener('input', (e) => {
const q = e.target.value.toLowerCase().trim();
items.forEach(item => {
const text = item.textContent.toLowerCase();
const matches = text.includes(q);
item.style.display = matches ? 'block' : 'none';
if (q.length > 1 && matches) {
item.setAttribute('open', '');
}
});
});
Next-Level Addons: Elevating Your Implementation
Take this technique further by combining it with complementary design token studios and animation tools:
A trick I use in almost every project: don't fight specificity battles with !important. Leverage modern CSS cascade layers (@layer) and CSS variables as design tokens—it makes dark/light theme switching and responsive tuning effortless.
Frequently Asked Questions
Practical answers to common implementation questions, browser quirks, and performance tips.
Why is grid-template-rows: 0fr to 1fr better than max-height?
The max-height hack requires guessing a large pixel ceiling (e.g. 500px). When content is short, the animation finishes prematurely and feels sluggish. CSS grid transitions interpolate exact calculated content heights.
Does CSS grid 0fr to 1fr work across all browsers?
Yes, animatable grid tracks are supported in Chrome 107+, Safari 16+, Firefox 113+, and Edge, representing over 96% global browser compatibility.
How does details and summary aid accessibility?
Native details/summary handles Enter/Space toggling, links accessible name to description, and notifies screen readers of expanded/collapsed states automatically.
How do you keep only one accordion item open at a time?
In modern HTML, add the same `name` attribute to all details elements: `
What Schema.org structure should be added for SEO?
Include an FAQPage schema graph containing Question and acceptedAnswer entities to qualify for Google Search rich accordion snippets.