How to create accordion only using HTML no JavaScript required?

In this tutorial we will use HTML tags that are details and summary tags with the help of these two tags we can build accordion without using CSS or Javascript. The details and summary tags in HTML are used to create an interactive and simple accordion.

The details tag behaves like a container for content that can be shown or hidden, and the summary tag provides a visible heading or label for the content within the details element. When we click on the summary tag or activate it, toggles the display of the content inside the details tag, showing or hiding it based on its current state. This behavior resembles that of an accordion, allowing users to expand or collapse content sections as needed.

Example 01:

Try It
        
        <details>
  <summary>Accordion title 01</summary>
  <p>Here is the content! for Accordion title 01</p>
</details>
<details>
  <summary>Accordion title 02</summary>
  <p>Here is the content! for Accordion title 02</p>
</details>
        
    

Load the accordion open by default

The details element's default behavior is to load in a closed state, which might result in hidden content when the page initially loads. I prefer to display content without hiding it from readers upon page load.

We can load the accordion open by default by adding the open attribute in the details tag.

Try It
        
        <details open>
  <summary>Accordion title 01</summary>
  <p>Here is the content! for Accordion title 01</p>
</details>
<details open>
  <summary>Accordion title 02</summary>
  <p>Here is the content! for Accordion title 02</p>
</details>
        
    

Related Post

Styling HTML Accordion using CSS

How to create accordion only using HTML no JavaScript required?

Leave a comment