Smooth Reveal Accordion
A meticulously crafted accordion featuring seamless height transitions and reactive icon animations.
HTMLCSSJavaScript
Preview
洗練されたデザインと、スムーズに展開するアニメーションが特徴のコンポーネントです。モダンなCSS技術を使用し、固定の高さやJavaScriptによる複雑な計算に頼ることなく、コンテンツの自然な高さに合わせてシームレスに開閉します。
CSS Gridを利用しています。grid-template-rowsの値を0frから1frへとトランジションさせることで、モダンブラウザ全体で滑らかで信頼性の高い高さのアニメーションを実現しています。
はい、対応しています。ボタンには適切なaria-expanded属性が使用されており、スクリーンリーダーなどの支援技術を使用するユーザーにも、各アコーディオンパネルの開閉状態が正しく伝わります。
Use Cases & Features
- FAQ Sections: The definitive pattern for neatly organizing dense questions and answers.
- Settings & Configurations: Excellent for grouping complex settings, allowing users to progressively disclose only the options they care about.
- Mobile Navigation: A staple for rendering multi-tiered navigational menus while conserving critical mobile screen real estate.
- Core Mechanics: Delivers flawlessly smooth expand/collapse animations—regardless of content volume—by leveraging native
CSS Gridtransitions. Designed with an exclusive “accordion” behavior where opening one panel automatically closes the others.
Design & UX
- Fluid Transitions: By transitioning
grid-template-rowsalong a refinedcubic-beziercurve, the component achieves a highly satisfying, natural feeling open/close motion. - Reactive State Cues: Dynamic shifts in border color, typography weight, and a rotating chevron icon clearly communicate the active state to the user.
- Hover Micro-Interactions: Gentle shadow elevation and subtle border adjustments upon hover invite interaction.
- Accessible by Default: Implements standard
aria-expandedattributes, ensuring total reliability for screen readers and assistive technologies.
Customization Guide
- Toggle Multiple Panels: To allow multiple panels to be open simultaneously, simply remove the logic that collapses siblings (the
document.querySelectorAll...block in the JavaScript). - Brand Theming: Swap the
#8b5cf6(purple) hex code in.accordion-item.activeand.accordion-titleto perfectly match your brand’s primary accent color. - Icon Swaps: The embedded SVG chevron can be effortlessly replaced with plus/minus symbols or icons from libraries like Heroicons or Lucide.
Implementation Code
<div class="accordion-container">
<div class="accordion-item">
<button class="accordion-header" aria-expanded="false">
<span class="accordion-title">このアコーディオンの特徴は?</span>
<span class="accordion-icon">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="6 9 12 15 18 9"></polyline></svg>
</span>
</button>
<div class="accordion-content-wrapper">
<div class="accordion-content">
洗練されたデザインと、スムーズに展開するアニメーションが特徴のコンポーネントです。モダンなCSS技術を使用し、固定の高さやJavaScriptによる複雑な計算に頼ることなく、コンテンツの自然な高さに合わせてシームレスに開閉します。
</div>
</div>
</div>
<!-- 他のアコーディオンアイテムをここに追加 -->
</div>
<style>
.accordion-container {
max-width: 600px;
width: 100%;
display: flex;
flex-direction: column;
gap: 0.75rem;
font-family: system-ui, -apple-system, sans-serif;
}
.accordion-item {
border: 1px solid #e5e7eb;
border-radius: 12px;
overflow: hidden;
background-color: #ffffff;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.02);
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
.accordion-item:hover {
border-color: #d1d5db;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}
.accordion-item.active {
border-color: #8b5cf6;
box-shadow: 0 4px 12px rgba(139, 92, 246, 0.1);
}
.accordion-header {
width: 100%;
padding: 1.25rem 1.5rem;
display: flex;
align-items: center;
justify-content: space-between;
background: none;
border: none;
cursor: pointer;
text-align: left;
color: #1f2937;
}
.accordion-title {
font-weight: 600;
font-size: 1.05rem;
transition: color 0.3s ease;
}
.accordion-icon {
display: flex;
align-items: center;
justify-content: center;
width: 24px;
height: 24px;
color: #6b7280;
transition: transform 0.4s cubic-bezier(0.4, 0, 0.2, 1), color 0.3s ease;
}
.accordion-item.active .accordion-icon {
transform: rotate(180deg);
color: #8b5cf6;
}
.accordion-item.active .accordion-title {
color: #8b5cf6;
}
.accordion-content-wrapper {
display: grid;
grid-template-rows: 0fr;
transition: grid-template-rows 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}
.accordion-item.active .accordion-content-wrapper {
grid-template-rows: 1fr;
}
.accordion-content {
overflow: hidden;
padding: 0 1.5rem;
color: #4b5563;
line-height: 1.6;
font-size: 0.95rem;
opacity: 0;
transform: translateY(-8px);
transition: opacity 0.4s ease, transform 0.4s ease, padding 0.4s ease;
}
.accordion-item.active .accordion-content {
padding-bottom: 1.25rem;
opacity: 1;
transform: translateY(0);
}
</style>
<script>
document.querySelectorAll('.accordion-header').forEach(button => {
button.addEventListener('click', () => {
const item = button.closest('.accordion-item');
const isExpanded = button.getAttribute('aria-expanded') === 'true';
document.querySelectorAll('.accordion-item').forEach(otherItem => {
if (otherItem !== item) {
otherItem.classList.remove('active');
otherItem.querySelector('.accordion-header').setAttribute('aria-expanded', 'false');
}
});
if (!isExpanded) {
item.classList.add('active');
button.setAttribute('aria-expanded', 'true');
} else {
item.classList.remove('active');
button.setAttribute('aria-expanded', 'false');
}
});
});
</script> Browser Support
- Chrome: Supported
- Firefox: Supported
- Safari: Supported (Full support for Grid Animation available in Safari 16+)
- Edge: Supported
Note: In legacy browsers lacking grid-template-rows transition support, the height animation will fail gracefully—snapping open and closed instantly while remaining entirely functional.