The basic HTML structure for an accordion consists of a container element that holds multiple accordion items. Each item typically has a header (which the user clicks to expand or collapse) and a content section.
<div class="accordion">
<div class="accordion-item">
<div class="accordion-header">
<h2>Section 1</h2>
</div>
<div class="accordion-content">
<p>This is the content for section 1.</p>
</div>
</div>
<div class="accordion-item">
<div class="accordion-header">
<h2>Section 2</h2>
</div>
<div class="accordion-content">
<p>This is the content for section 2.</p>
</div>
</div>
</div>
To create the accordion effect, we use CSS to control the visibility of the content sections. By default, the content sections are hidden, and when the header is clicked, we can use JavaScript or CSS pseudo - classes to show the content.
.accordion {
width: 300px;
}
.accordion-item {
border: 1px solid #ccc;
margin-bottom: 5px;
}
.accordion-header {
background-color: #f4f4f4;
padding: 10px;
cursor: pointer;
}
.accordion-content {
padding: 10px;
display: none;
}
.accordion-item.active .accordion-content {
display: block;
}
We can create a simple accordion using only CSS by leveraging the :checked
pseudo - class along with the input
and label
elements.
<div class="css-accordion">
<input type="checkbox" id="section1">
<label for="section1">Section 1</label>
<div class="accordion-content">
<p>Content for section 1.</p>
</div>
<input type="checkbox" id="section2">
<label for="section2">Section 2</label>
<div class="accordion-content">
<p>Content for section 2.</p>
</div>
</div>
.css-accordion input[type="checkbox"] {
display: none;
}
.css-accordion label {
display: block;
background-color: #f4f4f4;
padding: 10px;
cursor: pointer;
}
.css-accordion .accordion-content {
padding: 10px;
display: none;
}
.css-accordion input[type="checkbox"]:checked ~ .accordion-content {
display: block;
}
For more advanced functionality, such as animations and controlling which sections can be open at the same time, we can use JavaScript.
<div class="js-accordion">
<div class="accordion-item">
<div class="accordion-header" onclick="toggleAccordion(this)">
<h2>Section 1</h2>
</div>
<div class="accordion-content">
<p>This is the content for section 1.</p>
</div>
</div>
<div class="accordion-item">
<div class="accordion-header" onclick="toggleAccordion(this)">
<h2>Section 2</h2>
</div>
<div class="accordion-content">
<p>This is the content for section 2.</p>
</div>
</div>
</div>
<script>
function toggleAccordion(header) {
const item = header.parentNode;
const content = item.querySelector('.accordion-content');
item.classList.toggle('active');
if (item.classList.contains('active')) {
content.style.display = 'block';
} else {
content.style.display = 'none';
}
}
</script>
button
for accordion headers instead of div
or span
to ensure keyboard navigation and screen reader compatibility.aria - expanded
and aria - controls
to provide more information to assistive technologies.<div class="accordion">
<div class="accordion-item">
<button class="accordion-header" aria-expanded="false" aria-controls="section1-content">
<h2>Section 1</h2>
</button>
<div class="accordion-content" id="section1-content">
<p>Content for section 1.</p>
</div>
</div>
</div>
@media (max-width: 600px) {
.accordion {
width: 100%;
}
}
Accordions are a versatile and useful UI component that can enhance the user experience of a website. By understanding the fundamental concepts of HTML and CSS, along with different usage methods, common practices, and best practices, you can create efficient and accessible accordions. Whether you choose a pure CSS solution or a JavaScript - enhanced one, always keep performance and user experience in mind.