Ask any question about CSS here... and get an instant response.
Post this Question & Answer:
How can I create a CSS-only accordion effect without using JavaScript?
Asked on Apr 30, 2026
Answer
Creating a CSS-only accordion effect involves using the `:checked` pseudo-class with input elements to toggle the visibility of content. This method relies on HTML checkboxes or radio buttons to manage the open and close states of the accordion sections.
<!-- BEGIN COPY / PASTE -->
<style>
.accordion {
margin: 1em 0;
}
.accordion input {
display: none;
}
.accordion label {
display: block;
padding: 0.5em;
background: #007bff;
color: white;
cursor: pointer;
}
.accordion .content {
max-height: 0;
overflow: hidden;
transition: max-height 0.3s ease-out;
background: #f1f1f1;
padding: 0 0.5em;
}
.accordion input:checked ~ .content {
max-height: 100px; /* Adjust as needed */
}
</style>
<div class="accordion">
<input type="checkbox" id="section1" />
<label for="section1">Section 1</label>
<div class="content">
<p>This is the content for section 1.</p>
</div>
</div>
<div class="accordion">
<input type="checkbox" id="section2" />
<label for="section2">Section 2</label>
<div class="content">
<p>This is the content for section 2.</p>
</div>
</div>
<!-- END COPY / PASTE -->Additional Comment:
- The accordion uses hidden checkboxes to control the display of content sections.
- The `:checked` pseudo-class is used to apply styles when the checkbox is checked, such as expanding the content area.
- Adjust `max-height` in the `.accordion input:checked ~ .content` rule to fit your content size.
- This approach is purely CSS-based and does not require JavaScript, making it simple and lightweight.
Recommended Links:
