html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Smooth Accordion Menu</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
line-height: 1.6;
color: #333;
max-width: 800px;
margin: 0 auto;
padding: 2rem 1rem;
background: #f8f9fa;
}
.accordion {
width: 100%;
}
.accordion-item {
border-bottom: 1px solid #e0e0e0;
overflow: hidden;
}
.accordion-item:last-child {
border-bottom: none;
}
.accordion-header {
width: 100%;
padding: 1.25rem 1rem;
background: none;
border: none;
text-align: left;
cursor: pointer;
display: flex;
justify-content: space-between;
align-items: center;
font-size: 1.1rem;
font-weight: 600;
color: #333;
transition: background-color 0.2s ease;
position: relative;
}
.accordion-header:hover,
.accordion-header:focus {
background-color: #f0f4f8;
outline: none;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}
.accordion-header:focus-visible {
outline: 2px solid #007bff;
outline-offset: -2px;
}
.accordion-title {
flex-grow: 1;
}
.accordion-icon {
font-size: 0.8rem;
color: #666;
transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
margin-left: 1rem;
flex-shrink: 0;
}
.accordion-header[aria-expanded="true"] .accordion-icon {
transform: rotate(180deg);
}
.accordion-content {
background-color: #fff;
padding: 0 1rem;
max-height: 0;
overflow: hidden;
transition: max-height 0.4s cubic-bezier(0.4, 0, 0.2, 1),
padding 0.4s cubic-bezier(0.4, 0, 0.2, 1);
border-left: 3px solid #007bff;
}
.accordion-content.active {
padding: 1.25rem 1rem;
max-height: 500px; /* Large enough for most content; dynamically adjusted in JS */
}
.accordion-content p {
margin: 0 0 1rem 0;
}
.accordion-content ul {
margin: 0 0 1rem 1.5rem;
padding: 0;
}
.accordion-content li {
margin-bottom: 0.5rem;
}
@media (max-width: 600px) {
.accordion-header {
padding: 1rem 0.75rem;
font-size: 1rem;
}
.accordion-content {
padding-left: 0.75rem;
padding-right: 0.75rem;
}
.accordion-content.active {
padding: 1rem 0.75rem;
}
}
</style>
</head>
<body>
<h1>Smooth Accordion Menu Example</h1>
<p>Click on the headers below to expand/collapse sections. Only one section can be open at a time.</p>
<div class="accordion" role="region" aria-label="Accordion sections">
<div class="accordion-item">
<button
id="header1"
class="accordion-header"
aria-expanded="false"
aria-controls="panel1"
>
<span class="accordion-title">Introduction</span>
<span class="accordion-icon" aria-hidden="true">▼</span>
</button>
<div id="panel1" class="accordion-content" role="region" aria-labelledby="header1">
<p>This is a smooth accordion menu built with vanilla HTML, CSS, and JavaScript. It supports accessibility features like ARIA attributes and keyboard navigation.</p>
<ul>
<li>Fully responsive design</li>
<li>Smooth CSS transitions using <code>cubic-bezier</code> for natural easing</li>
<li>Dynamic <code>max-height</code> adjustment for precise animations</li>
<li>Single-open behavior (closes others when one is opened)</li>
</ul>
</div>
</div>
<div class="accordion-item">
<button
id="header2"
class="accordion-header"
aria-expanded="false"
aria-controls="panel2"
>
<span class="accordion-title">Features</span>
<span class="accordion-icon" aria-hidden="true">▼</span>
</button>
<div id="panel2" class="accordion-content" role="region" aria-labelledby="header2">
<p>Key features include:</p>
<ul>
<li><strong>Smooth Expansion/Collapse:</strong> Uses <code>max-height</code> transition with <code>scrollHeight</code> for exact sizing.</li>
<li><strong>Accessibility:</strong> ARIA roles, states, labels, and focus management.</li>
<li><strong>Performance:</strong> No dependencies, lightweight, GPU-accelerated transitions.</li>
<li><strong>Customization:</strong> Easily themeable with CSS variables (extendable).</li>
<li>Hover and focus states for better UX.</li>
</ul>
</div>
</div>
<div class="accordion-item">
<button
id="header3"
class="accordion-header"
aria-expanded="false"
aria-controls="panel3"
>
<span class="accordion-title">How It Works</span>
<span class="accordion-icon" aria-hidden="true">▼</span>
</button>
<div id="panel3" class="accordion-content" role="region" aria-labelledby="header3">
<p>The animation works by:</p>
<ol>
<li>Setting <code>max-height: 0</code> initially with <code>overflow: hidden</code>.</li>
<li>On click, JavaScript sets <code>max-height</code> to <code>scrollHeight + 'px'</code> and adds an <code>active</code> class.</li>
<li>CSS handles the smooth transition and icon rotation.</li>
<li>Closes all other panels for accordion behavior.</li>
</ol>
<p>Copy-paste ready! No external libraries needed.</p>
</div>
</div>
<div class="accordion-item">
<button
id="header4"
class="accordion-header"
aria-expanded="false"
aria-controls="panel4"
>
<span class="accordion-title">Customization Tips</span>
<span class="accordion-icon" aria-hidden="true">▼</span>
</button>
<div id="panel4" class="accordion-content" role="region" aria-labelledby="header4">
<p>To customize:</p>
<ul>
<li>Change colors: Edit <code>background-color</code>, <code>border-left</code>.</li>
<li>Allow multiple open: Remove the "close others" loop in JS.</li>
<li>Adjust timing: Modify <code>transition</code> duration/easing.</li>
<li>Add icons: Use SVG or Font Awesome in <code>.accordion-icon</code>.</li>
<li>Dynamic content: Call <code>updateMaxHeight()</code> after content changes.</li>
</ul>
</div>
</div>
</div>
<script>
document.querySelectorAll('.accordion-header').forEach(header => {
header.addEventListener('click', () => {
const isExpanded = header.getAttribute('aria-expanded') === 'true';
const contentId = header.getAttribute('aria-controls');
const content = document.getElementById(contentId);
// Close all panels
document.querySelectorAll('.accordion-header').forEach(h => {
h.setAttribute('aria-expanded', 'false');
});
document.querySelectorAll('.accordion-content').forEach(c => {
c.classList.remove('active');
c.style.maxHeight = '0px';
});
// Toggle current panel
if (!isExpanded) {
content.classList.add('active');
header.setAttribute('aria-expanded', 'true');
content.style.maxHeight = content.scrollHeight + 'px';
}
});
});
// Optional: Function to update height if content changes dynamically
function updateAccordionHeight(content) {
if (content.classList.contains('active')) {
content.style.maxHeight = content.scrollHeight + 'px';
}
}
</script>
</body>
</html>
This is a complete, self-contained accordion menu. Copy-paste into an HTML file to test. It features:
To allow multiple panels open simultaneously, remove the "Close all panels" loop in the JS.