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.
Bored with the browser’s default scrollbar? Want your website to look more unique and attract visitors? Well, you’re in the right place! In this article, we’ll explore how to modify your website’s scrollbar with just a touch of CSS. It’s really easy, let’s get straight to it! Scrollbars are essential for navigation, especially on long […]
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 […]
Choosing your favorite programming language can be tough with so many options out there. If you’re unsure about which programming language to learn for your future, I can help you out on this blog. So, I’ll share the steps and considerations I used to figure out which programming language fits best based on my experience. […]