How to create an Accordion with CSS and JavaScript?

CSSWeb DevelopmentFront End Technology

To create an Accordion with CSS and JavaScript, the code is as follows −


 Live Demo

<!DOCTYPE html>
<meta name="viewport" content="width=device-width, initial-scale=1">
.demo {
   background-color: #eee;
   cursor: pointer;
   padding: 25px;
   width: 100%;
.active, .demo:hover {
   background-color: #ccc;
   border: 2px;
   font-size: 14px;
.panel {
   padding: 0 18px;
   display: none;
   background-color: #F0F8FF;
      overflow: hidden;
<p>Following is the information on exams:</p>
<button class="demo">Admit Card</button>
<div class="panel">
   <p>Admit Card will release on 25th March.</p>
<button class="demo">Exam Date</button>
<div class="panel">
   <p>Exam will held on 30th March.</p>
var val = document.getElementsByClassName("demo");
var i;
for (j = 0; j < val.length; j++) {
   val[j].addEventListener("click", function() {
      var panel = this.nextElementSibling;
      if ( === "block") { = "none";
      } else { = "block";


This will produce the following output −

On clicking any of the Button, the hidden info will be visible as shown below −

Updated on 03-Apr-2020 07:34:29