The basic HTML structure for an accordion consists of a container element, usually a <div>
, and a series of sections. Each section has a header (e.g., a <button>
or <h2>
) that the user can click on, and a content area (e.g., a <div>
) that will show or hide when the header is clicked.
<div class="accordion">
<div class="accordion-item">
<button class="accordion-header">Section 1</button>
<div class="accordion-content">
<p>This is the content for section 1.</p>
</div>
</div>
<div class="accordion-item">
<button class="accordion-header">Section 2</button>
<div class="accordion-content">
<p>This is the content for section 2.</p>
</div>
</div>
</div>
To make the accordion functional, we’ll use CSS to hide and show the content based on the state of the header. We can use the :checked
pseudo - class in combination with the input
element or the :active
or :focus
pseudo - classes on the button. Here’s a simple example using the :checked
pseudo - class with a hidden checkbox:
<div class="accordion">
<input type="checkbox" id="accordion-1">
<label for="accordion-1" class="accordion-header">Section 1</label>
<div class="accordion-content">
<p>This is the content for section 1.</p>
</div>
</div>
.accordion-content {
display: none;
}
input[type="checkbox"]:checked ~ .accordion-content {
display: block;
}
If you want only one item to be expanded at a time, you can use JavaScript to manage the state. However, with pure HTML and CSS, we can use radio buttons instead of checkboxes. Radio buttons ensure that only one option can be selected at a time.
<div class="accordion">
<input type="radio" id="accordion-1" name="accordion-group">
<label for="accordion-1" class="accordion-header">Section 1</label>
<div class="accordion-content">
<p>This is the content for section 1.</p>
</div>
<input type="radio" id="accordion-2" name="accordion-group">
<label for="accordion-2" class="accordion-header">Section 2</label>
<div class="accordion-content">
<p>This is the content for section 2.</p>
</div>
</div>
.accordion-content {
display: none;
}
input[type="radio"]:checked ~ .accordion-content {
display: block;
}
If you want multiple items to be expandable simultaneously, use checkboxes as shown in the previous example. Each checkbox can be toggled independently.
To make the expansion and collapse of the accordion look smooth, we can add CSS transitions. For example, we can add a transition to the max-height
property instead of display
because display
doesn’t support transitions.
.accordion-content {
max-height: 0;
overflow: hidden;
transition: max-height 0.3s ease-out;
}
input[type="checkbox"]:checked ~ .accordion-content {
max-height: 500px; /* Adjust this value based on your content */
}
To make the accordion more user - friendly, we can style the headers. For example, we can change the background color when the header is active.
.accordion-header {
background-color: #f1f1f1;
padding: 10px;
cursor: pointer;
}
.accordion-header:hover {
background-color: #ddd;
}
input[type="checkbox"]:checked + .accordion-header {
background-color: #ccc;
}
<button>
or <input>
for the accordion headers. This helps screen readers understand the functionality.aria - expanded
can be used to indicate whether the content is expanded or collapsed.em
for sizing.Creating an accordion with HTML and CSS is a great way to add interactive elements to your web pages without relying on JavaScript. By understanding the fundamental concepts, usage methods, common practices, and best practices, you can build functional and accessible accordions. Remember to focus on accessibility and responsive design to ensure a great user experience across all devices.