You should know that an Accordion is a UI element used to show a list of items concisely. This component consists of several widgets that can be expanded or collapsed. It’s a handy kind of component to display hidden content, usually when a title is clicked. You’ll often find this component on Frequently Asked Questions (FAQ) pages in apps and websites.
In this tutorial, I’m gonna share how I make an accordion without JavaScript. Why do I mention JavaScript? Because developers usually create accordions with some logic, like showing and hiding in each section. For now, I’ll show you how to make a simple accordion using the <details> and <summary> elements in HTML.
You just need to understand how to use those two tags first, if you don’t get it, I’ll explain it below:
The <details> HTML element creates a disclosure widget in which information is visible only when the widget is toggled into an open state. A summary or label must be provided using the <summary> element.
A disclosure widget is typically presented onscreen using a small triangle that rotates (or twists) to indicate open/closed state, with a label next to the triangle. The contents of the <summary> element are used as the label for the disclosure widget. The contents of the <details> provide the accessible description for the <summary>.
<details> element is used to create interactive widgets that can be opened and closed.
<summary> element defines a summary or title for the <details> element.
Alright, without needing to explain much, I’ll share the steps to create an accordion using HTML:
Use the <details> element to wrap each part of the accordion.
Use the <summary> element to define the title of each section.
Add detailed content within the <details> element.
<details>
<summary>Summary 1</summary>
<p>This is the detailed content for section 1.</p>
</details>
<details>
<summary>Summary 2</summary>
<p>This is the detailed content for section 2.</p>
</details>
<details>
<summary>Summary 3</summary>
<p>This is the detailed content for section 3.</p>
</details>
The explanation of the code above:
When a user clicks the <summary> element, the content inside the <details> element will be shown or hidden.
You can add CSS styles to make the accordion look nice.
The <details> and <summary> elements are a simple way to make an accordion without JavaScript. Even though they have some limitations, this method works great for simple use cases where you don’t need advanced functionality.
Rizky Ramadhan
Content Writer
I am Rizky Ramadhan, a content writer and programmer who is passionate about sharing knowledge and experiences through writing and coding.
Do you often find it difficult to explain complex workflows? Or maybe you want to present your business processes in a more engaging and easy-to-understand way? If so, then flowchart is the answer! A flowchart, or flowchart, is a visual representation of the steps in a process. With a clear flowchart, communication becomes more effective, […]
All novice programmers must know the Hamburger menu, right? It turns out that there are many types of menus used for websites, bro, not just hamburger menus. We already know that the Hamburger is known as a burger icon with a design that is often used to hide navigation menus in user interfaces. When the […]
Finding the perfect font can be the key to success for any design project. Serif fonts give a classic and timeless vibe, while sans-serif fonts are the go-to for modern designs. Clean, simple, and easy to read, these fonts are everywhere—from websites and mobile apps to branding and print materials. But have you ever thought […]
As a developer, I’m always looking for ways to create something unique and completely under my control. When I decided to start a personal blog, I knew I didn’t want to be tied down to any pre-made themes or templates. I wanted the freedom to design the front-end exactly how I wanted it, without sacrificing […]