How to create tabs with CSS and JavaScript?

CSSWeb DevelopmentFront End ScriptsJavascript

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


 Live Demo

<!DOCTYPE html>
<meta name="viewport" content="width=device-width, initial-scale=1">
.tab {
   overflow: hidden;
   border: 2px solid gray;
   background-color: #FFE4C4;
.tab button {
   background-color: inherit;
   float: left;
   cursor: pointer;
   padding: 14px 16px;
   font-size: 15px;
.tab button:hover {
   background-color: gray;
.tab {
   background-color: #ccc;
.demo2 {
   display: none;
   padding: 3px 12px;
<p>Following is the exam information:</p>
<div class="tab">
<button class="demo" onclick="infofunc(event, 'dates')">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 −

Now, click any of the tab to get the following −

Updated on 03-Apr-2020 07:38:08