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.
Ever heard the phrase “I want to be a programmer but I’m too lazy to code”? To be honest, you’re not alone! Many people are interested in the world of information technology (IT) and its bright career potential, but feel intimidated by the complicated coding process that requires a lot of persistence. The good news […]
Who here still gets nervous every time they hear the word “presentation”? Thinking about the same old slide designs, putting together points that make you sleepy, not to mention the last-minute typo drama. Ugh! Nah, here you go, I’ll share a secret recipe to make PowerPoint no longer a nightmare. Let’s get to know this […]
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 […]
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, […]