How TO - Tab Headers

Learn how to create tab headers with CSS and JavaScript.

Tab Headers

Click on the "city" buttons to display the appropriate header:


London is the capital city of England.


Paris is the capital of France.


Tokyo is the capital of Japan.


Oslo is the capital of Norway.

Create Togglable Tab Headers

Step 1) Add HTML:


<div id="London" class="tabcontent">
  <p>London is the capital city of England.</p>

<div id="Paris" class="tabcontent">
  <p>Paris is the capital of France.</p>

<div id="Tokyo" class="tabcontent">
  <p>Tokyo is the capital of Japan.</p>

<div id="Oslo" class="tabcontent">
  <p>Oslo is the capital of Norway.</p>

<button class="tablink" onclick="openCity('London', this, 'red')" id="defaultOpen">London</button>
<button class="tablink" onclick="openCity('Paris', this, 'green')">Paris</button>
<button class="tablink" onclick="openCity('Tokyo', this, 'blue')">Tokyo</button>
<button class="tablink" onclick="openCity('Oslo', this, 'orange')">Oslo</button>

Create buttons to open specific tab content. All <div> elements with class="tabcontent" are hidden by default (with CSS & JS). When the user clicks on a button - it will open the tab content that "matches" this button.

Step 2) Add CSS:

Style the buttons and the tab content:


/* Style the tab buttons */
.tablink {
    background-color: #555;
    color: white;
    float: left;
    border: none;
    outline: none;
    cursor: pointer;
    padding: 14px 16px;
    font-size: 17px;
    width: 25%;

/* Change background color of buttons on hover */
.tablink:hover {
    background-color: #777;

/* Set default styles for tab content */
.tabcontent {
    color: white;
    display: none;
    padding: 50px;
    text-align: center;

/* Style each tab content individually */
#London {background-color:red;}
#Paris {background-color:green;}
#Tokyo {background-color:blue;}
#Oslo {background-color:orange;}

Step 3) Add JavaScript:


function openCity(cityName, elmnt, color) {
    // Hide all elements with class="tabcontent" by default */
    var i, tabcontent, tablinks;
    tabcontent = document.getElementsByClassName("tabcontent");
    for (i = 0; i < tabcontent.length; i++) {
        tabcontent[i].style.display = "none";

    // Remove the background color of all tablinks/buttons
    tablinks = document.getElementsByClassName("tablink");
    for (i = 0; i < tablinks.length; i++) {
        tablinks[i].style.backgroundColor = "";

    // Show the specific tab content
    document.getElementById(cityName).style.display = "block";

    // Add the specific color to the button used to open the tab content = color;

// Get the element with id="defaultOpen" and click on it
Try it Yourself »

Tip: Also check out How To - Tabs.