<div class="accordion">
<!-- Accordion Item 1 -->
<div class="accordion-item">
<h3 class="accordion-header">
<button class="accordion-button"
aria-expanded="false"
aria-controls="panel1">
Section 1
</button>
</h3>
<div id="panel1"
class="accordion-panel"
role="region">
<p>Content for section 1.</p>
</div>
</div>
<!-- Additional items -->
</div>
.accordion-item {
border: 1px solid #ddd;
margin-bottom: 8px;
border-radius: 4px;
}
.accordion-button {
width: 100%;
padding: 16px;
text-align: left;
background: #f5f5f5;
border: none;
cursor: pointer;
transition: background 0.3s;
}
.accordion-button:hover {
background: #e0e0e0;
}
.accordion-button[aria-expanded="true"] {
background: #007bff;
color: white;
}
.accordion-panel {
max-height: 0;
overflow: hidden;
transition: max-height 0.3s ease-out;
padding: 0 16px;
}
.accordion-panel.active {
max-height: 1000px; /* Adjust based on content */
padding: 16px;
}
document.querySelectorAll('.accordion-button').forEach(button => {
button.addEventListener('click', () => {
const panel = document.getElementById(button.getAttribute('aria-controls'));
const isExpanded = button.getAttribute('aria-expanded') === 'true';
// Toggle current panel
button.setAttribute('aria-expanded', !isExpanded);
panel.classList.toggle('active');
// Close other panels (for single-open accordions)
if (!isExpanded) {
document.querySelectorAll('.accordion-button').forEach(otherButton => {
if (otherButton !== button) {
otherButton.setAttribute('aria-expanded', 'false');
document.getElementById(otherButton.getAttribute('aria-controls'))
.classList.remove('active');
}
});
}
});
});
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.