How to create a vertical tab menu with CSS and JavaScript?

CSSWeb DevelopmentFront End TechnologyJavascript

To create a vertical tab menu with CSS and JavaScript, the code is as follows −


 Live Demo

<!DOCTYPE html>
<meta name="viewport" content="width=device-width, initial-scale=1">
* {
   box-sizing: border-box
.tab {
   float: left;
   border: 1px solid blue;
   background-color: black;
   width: 20%;
   height: 200px;
.tab button {
   display: block;
   background-color: inherit;
   color: white;
   padding: 22px 16px;
   width: 100%;
   border: none;
   outline: none;
   text-align: left;
   cursor: pointer;
   transition: 0.3s;
   font-size: 17px;
.tab button:hover {
   background-color: gray;
.tab {
   background-color: #006400;
.demo2 {
   float: left;
   padding: 0px 10px;
   border: 1px solid;
   width: 70%;
   border-left: none;
   height: 200px;
<p>Following is the exam information:</p>
<div class="tab">
   <button class="demo" onclick="infofunc(event, 'dates')" id="current">Exam Dates</button>
   <button class="demo" onclick="infofunc(event, 'centre')">Exam Centre</button>
<div id="dates" class="demo2">
   <p>Exam date will be announced on 25th March.</p>
<div id="centre" class="demo2">
   <p>Exam Centre will be announced on 30th March.</p>
function infofunc(e, info) {
   var i, content, links;
   content = document.getElementsByClassName("demo2");
   for (i = 0; i < content.length; i++) {
      content[i].style.display = "none";
   links = document.getElementsByClassName("demo");
   for (i = 0; i < links.length; i++) {
      links[i].className = links[i].className.replace(" active", "");
   document.getElementById(info).style.display = "block";
   e.currentTarget.className += " active";


This will produce the following output −

Click on any of the tabs, let’s say “Exam Centre” −

Updated on 03-Apr-2020 07:43:07