Accordion Using HTML 5

Introduction
 
Recently, I used a useful widget in my web application which is called Accordion. In the older version of HTML, to implement Accordion, we needed to implement a lot of stuff like HTML, CSS and JavaScript code too for allowing the toggle effect.
 
But HTML 5 has this tag which can work similarly but in an effective way. In this tutorial, we are going to see the difference between both the old and the new accordion widget.
 
HTML 5 Accordion
 
To use HTML 5 Accordion, there is a new HTML tag, <details>, and it is used to add the accordion into our HTML page.
  1. <details>  
  2.      ...  
  3. </details>  
And to display the label along with the accordion, we are going to use the <summary> tag which is used to display a caption or a description.
  1. <summary>  
  2.       This is simple summary  
  3. </summary>  
Accordian using an older approach
 
If you have used an older approach to develop the accordion, then you must be aware of the excessive time it takes. Also, you must know that we need to apply an additional JavaScript code so that we can get the toggle effect. You can find the example below.
  1. <html>  
  2.   
  3. <head>  
  4.     <title>Manav - HTML Old Accordian Demo</title>  
  5.     <style>  
  6.         .accordion {  
  7.             background-color: #eee;  
  8.             color: #444;  
  9.             cursor: pointer;  
  10.             padding: 18px;  
  11.             width: 100%;  
  12.             border: none;  
  13.             text-align: left;  
  14.             outline: none;  
  15.             font-size: 15px;  
  16.             transition: 0.4s;  
  17.         }  
  18.   
  19.         .active,  
  20.         .accordion:hover {  
  21.             background-color: #ccc;  
  22.         }  
  23.   
  24.         .panel {  
  25.             padding: 0 18px;  
  26.             display: none;  
  27.             background-color: white;  
  28.             overflow: hidden;  
  29.         }  
  30.     </style>  
  31. </head>  
  32.   
  33. <body>  
  34.     <h2>HTML Old Accordian</h2>  
  35.     <button class="accordion">Section 1</button>  
  36.     <div class="panel">  
  37.         <p>This is section 1</p>  
  38.     </div>  
  39.     <button class="accordion">Section 2</button>  
  40.     <div class="panel">  
  41.         <p>This is section 2</p>  
  42.     </div>  
  43.     <button class="accordion">Section 3</button>  
  44.     <div class="panel">  
  45.         <p>This is section 3</p>  
  46.     </div>  
  47.     <script>  
  48.         var acc = document.getElementsByClassName("accordion");  
  49.         var i;  
  50.         for (i = 0; i < acc.length; i++) {  
  51.             acc[i].addEventListener("click", function () {  
  52.                 this.classList.toggle("active");  
  53.                 var panel = this.nextElementSibling;  
  54.                 if (panel.style.display === "block") {  
  55.                     panel.style.display = "none";  
  56.                 } else {  
  57.                     panel.style.display = "block";  
  58.                 }  
  59.             });  
  60.         }  
  61.     </script>  
  62.   
  63. </body>  
  64.   
  65. </html>  
Here, in this example, I have used JavaScript code which allows us to toggle the Div section but using HTML 5 <details> widget, we don't need to implement such things. You can see the output like this.
 
 
 
New approach
 
We are going to learn the newly added HTML 5 tag <details> with a simple example where we are not going to use JavaScript to enable the toggle effect.
  1. <html>  
  2.   
  3. <head>  
  4.     <title>Manav - HTML 5 Accordian Demo</title>  
  5.     <style>  
  6.         details {  
  7.             background: #eee;  
  8.             color: #444;  
  9.             padding: 18px;  
  10.             border: none;  
  11.             text-align: left;  
  12.             outline: none;  
  13.             font-size: 15px;  
  14.         }  
  15.     </style>  
  16. </head>  
  17.   
  18. <body>  
  19.     <h1>HTML 5 Acordian Tag</h1>  
  20.     <details>  
  21.         <summary>Section 1</summary>  
  22.         <h3>This is section 1</h3>  
  23.     </details>  
  24.   
  25.     <details>  
  26.         <summary>Section 2</summary>  
  27.         <h3>This is section 2</h3>  
  28.     </details>  
  29.   
  30.     <details>  
  31.         <summary>Section 3</summary>  
  32.         <h3>This is section 3</h3>  
  33.     </details>  
  34. </body>  
  35.   
  36. </html>  
As you can see in the above example, I have just used different HTML 5 tags. We get the same result as compared to the older approach.
 
 
Conclusion
 
This is how we can take advantage of HTML 5 tags to simplify the different functionalities. Keep in mind that it's just an approach to use the accordion type of effect. Still, you can implement customized accordion as per your requirements.
 
Thanks!