How to Make Tab using HTML, CSS, and JavaScript?

Here you will learn how to make a simple Tab using pure HTML, CSS, and JavaScript.

HTML, CSS, and JS Tab frontend UI project

Click on the button below to see a demo of the tab we’re going to create here –

HTML code for the Tab

<div class="tab-container">
    <ul class="tab-labels" role="tablist">
        <li class="tab-label" role="tab">Tab 1</li>
        <li class="tab-label" role="tab">Tab 2</li>
        <li class="tab-label" role="tab">Tab 3</li>
    <div class="tab-content">
        <div class="tab-panel" role="tabpanel">
            Tab 1 content...
        <div class="tab-panel" role="tabpanel">
            Tab 2 content...
        <div class="tab-panel" role="tabpanel">
            Tab 3 content...

CSS Code for the Tab

@import url(";700&display=swap");
*::after {
    box-sizing: border-box;
    line-height: 1.5em;

html {
    font-size: 16px;
    scroll-behavior: smooth;
    -webkit-text-size-adjust: 100%;

body {
    margin: 0;
    background-color: #f7f7f7;
    color: #222222;
    padding: 20px;

button {
    font-family: "Open Sans", sans-serif;
    font-size: 1rem;

.tab-container {
    background: #fff;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
    border-radius: 3px;
    padding: 30px;
.tab-container .tab-labels {
    padding: 0;
    margin: 0;
.tab-container .tab-labels .tab-label {
    position: relative;
    display: inline-block;
    list-style: none;
    padding: 1em 2em 0.5em;
    padding: 15px;
    border: 1px solid transparent;
    margin-bottom: -1px;
    cursor: pointer;
    outline: none;
    font-weight: 700;
    text-transform: uppercase;
.tab-container .tab-labels {
    border-color: #ccc;
    border-bottom: 1px solid #fff;
    color: #ff8a00;
.tab-container .tab-labels .tab-label:hover {
    color: #ff8a00;
.tab-container .tab-panel {
    display: none;
.tab-container {
    display: block;
    border-top: 1px solid #ccc;

JavaScript code for the Tab

const tabLabels = document.querySelectorAll(".tab-label");
const tabPanels = document.querySelectorAll(".tab-panel");

function activate_the_tab(target_i) {
    if (tabLabels.length === tabPanels.length) {
        for (let i = 0; i < tabLabels.length; i++) {
            if (i === target_i) {
            } else {

function on_tab_click(e, i) {

tabLabels.forEach((item, i) => {
    item.addEventListener("click", (e) => on_tab_click(e, i));