How to create tab headers with CSS and JavaScript?

CSSWeb DevelopmentFront End TechnologyJavascript

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


 Live Demo

<!DOCTYPE html>
<meta name="viewport" content="width=device-width, initial-scale=1">
.tablink {
   background-color: black;
   color: white;
   float: left;
   cursor: pointer;
   padding: 20px 12px;
   font-size: 15px;
   width: 33.3%;
.tablink:hover {
   background-color: black;
.tabcontent {
   color: white;
   display: none;
   padding: 50px;
   text-align: center;
#date {
   border: 1px solid black;
#time {
   color: black;
   border: 1px solid black;
#venue {
   border: 1px solid black;
<h2>Match Schedule</h2>
<div id="date" class="tabcontent">
   <p>Match will held on 2nd April.</p>
<div id="time" class="tabcontent">
   <p>Begins: 9AM</p>
<div id="venue" class="tabcontent">
   <p>MCG, Australia</p>
<button class="tablink" onclick="demo('date', this, 'red')" id="defaultOpen">Date</button>
<button class="tablink" onclick="demo('time', this, 'green')">Time</button>
<button class="tablink" onclick="demo('venue', this, 'blue')">Venue</button>
function demo(match,e,color) {
   var i, content, links;
   content = document.getElementsByClassName("tabcontent");
   var len1 = content.length;
   for (i = 0; i < len1; i++) {
      content[i].style.display = "none";
   links = document.getElementsByClassName("tablink");
   var len2 = links.length;
   for (i = 0; i < len2; i++) {
      links[i].style.backgroundColor = "";
   document.getElementById(match).style.display = "block"; = color;


This will produce the following output −

Now, click on any of the tabs −

Updated on 03-Apr-2020 07:47:24