How to build an Accordion without JavaScript using only HTML and CSS

Creating an Accordion with Only HTML and CSS

Accordions are nifty tools for displaying content neatly. They help save space and add an interactive element to web pages. Typically, accordions are JavaScript’s territory. But what if I told you that we can build them using just HTML and CSS? Sounds interesting, right? Let’s dive into how this can be done, step-by-step.

Setting Up the HTML Structure

Firstly, we need a basic structure. We’ll use <div> elements for the accordion container and its sections. Each section comprises a header that you click to reveal the content.

<div class="accordion">
<div class="accordion-section">
<input type="checkbox" id="section1" hidden/>
<label for="section1">Section 1</label>
<div class="content">
Content of Section 1.
</div>
</div>
<!-- Repeat for more sections -->
</div>

We use the <input type="checkbox"> and <label> trick. When the label is clicked, it toggles the checkbox, which we’ll use to show or hide the content.

Basic Styling with CSS

Next, let’s add some basic styles. We’ll make the headers stand out and hide the content initially.

.accordion-section {
display: block;
margin-bottom: 10px;
}
.accordion-section label {
display: block;
background-color: #eee;
padding: 10px;
cursor: pointer;
}
.accordion-section .content {
display: none;
padding: 10px;
border: 1px solid #ddd;
}

This code makes the headers clickable and hides the content. Notice how we use `display: none` for the content initially.

Adding Interactivity with CSS

To show the content when the section’s header is clicked, we’ll use the :checked pseudo-class.

input:checked + label + .content {
display: block;
}

This CSS rule says: when the checkbox is checked, display the content that follows the label. Magic, isn’t it?

Enhancing with Transitions and Hover Effects

Adding smooth open/close animations and hover effects makes the accordion more user-friendly. Here’s how:

.content {
transition: max-height 0.4s ease-out;
overflow: hidden;
max-height: 0; /* Initial state */
}
input:checked + label + .content {
max-height: 500px; /* Adjust as needed */
}
.accordion-section label:hover {
background-color: #ccc;
}

We use max-height to animate the accordion’s expansion and collapse. The hover effect darkens the header on mouseover.

Using the <details> and <summary> HTML Elements

Another way to create an accordion with just HTML and CSS involves the <details> and <summary> elements. This method is simpler and does not require checkboxes or labels for interaction.

<details>
<summary>Section 1</summary>
Content of Section 1.
</details>
<details>
<summary>Section 2</summary>
Content of Section 2.
</details>
<!-- Repeat for more sections -->

Here, the <details> element acts as the accordion section, and <summary> serves as the header. The content is revealed when the summary element is clicked. CSS can be added to style these elements.

Accessibility and Cross-Browser Compatibility

Ensuring your accordion is accessible and works across browsers is crucial. Use ARIA attributes for screen readers and test your accordion in different browsers.

I know, we love to make our web projects unique. So, always feel free to tweak the styles, add animations, or even use flexbox for responsiveness. The web is your canvas!

Conclusion and Further Resources

We’ve seen how to create a functional and stylish accordion without JavaScript. It’s amazing what you can achieve with just HTML and CSS, right? Experiment with these techniques to add more interactivity to your websites.

For more ideas, check out CSS-Tricks or MDN Web Docs. They’re full of resources that can help you become a web design wiz!

Happy coding!

en_USEnglish