ADVERTISEMENT

How To Create Tabs using HTML, CSS & JavaScript

Telegram Group Join Now

ADVERTISEMENT

How To Create Tabs using HTML, CSS & JavaScript

Hello everyone. Welcome to today’s tutorial on Codewithrandom. We’ll learn how to make Tabs which are basically available on each browser but in this project we’ll just create a basic tab with naming it and adding some content in it. In Today’s session, We will use HTML, CSS, and JavaScript to complete this Tabs Project.

ADVERTISEMENT

Project Description

ADVERTISEMENT

Step 1
The HTML (Hypertext Markup Language) will help us to create the structure for the list with some necessary attributes and elements to make Tabs Project.

Step 2
Then we will use CSS (Cascading Stylesheet) which will help us to style or design the project with suitable padding and alignment in the Tabs Project.

ADVERTISEMENT

Step 3
At last we will use JS (JavaScript) which will add a logic to make the Tabs Project responsive from the user end.

ADVERTISEMENT

I hope you have got an idea about the project.

HTML Code for Tabs

First we’ll start with creating the structure of the Tabs project for that as you can see the above code we have used all the necessary elements & attributes to setup the structure. Let us know code the CSS part to add styling and aligned the tags.

<html lang="en">
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Tabs</title>
    <!-- Google Fonts -->
    <link
      href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap"
      rel="stylesheet"
    />
    <!-- Stylesheet -->
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div class="container">
      <div class="tabs">
        <h3 class="active">Tab 1</h3>
        <h3>Tab 2</h3>
        <h3>Tab 3</h3>
      </div>
      <div class="tab-content">
        <div class="active">
          <h4>First Title</h4>
          <p>
            Lorem ipsum dolor sit amet consectetur adipisicing elit. Dolore
            explicabo cum dolores hic possimus aut corrupti quisquam aperiam
            quia veniam inventore officiis nam error sunt libero, commodi
            architecto reiciendis qui fuga, itaque delectus quidem sequi.
            Impedit natus culpa nihil aperiam adipisci aliquam error, suscipit
            odio? Error sed esse perspiciatis quasi velit, ratione odit
            architecto? Explicabo pariatur.
          </p>
        </div>

        <div>
          <h4>Second Title</h4>
          <p>
            Lorem ipsum dolor sit amet consectetur, adipisicing elit. Deleniti,
            fugiat ab? Accusamus sed a iusto? Placeat incidunt repudiandae vero
            magnam nihil tempore quasi earum illum totam aut delectus aliquam
            pariatur, iste, qui provident quo voluptatem neque facere id
            laudantium aliquid numquam nisi accusantium. Inventore reiciendis
            nulla, iste perferendis.
          </p>
        </div>

        <div>
          <h4>Third Title</h4>
          <p>
            Lorem ipsum dolor sit amet consectetur, adipisicing elit. Quaerat
            autem accusantium voluptate debitis ipsa animi aliquid dolore?
            Suscipit consequatur architecto ullam perferendis praesentium sed
            aliquid voluptatem quibusdam laborum, doloremque aut atque debitis
            et laudantium qui veniam eligendi accusamus ipsam optio, assumenda
            aliquam ipsum dolorem similique?
          </p>
        </div>
      </div>
    </div>
    <!--Script-->
    <script src="script.js"></script>
  </body>
</html>

50+ HTML, CSS & JavaScript Projects With Source Code

ADVERTISEMENT

CSS Code for Tabs

Second comes the CSS code in which we have styled for the structure we have padded as well as aligned the Tabs project so that it is properly situated and doesn’t get messy with suitable CSS elements. Now lets code the JavaScript part to make responsive.

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
  font-family: "Poppins", sans-serif;
}
body {
  background-color: #4d5bf9;
}
.container {
  width: 80vmin;
  position: absolute;
  transform: translateX(-50%);
  left: 50%;
  top: 120px;
  background-color: #ffffff;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.2);
}
.tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
}
h3 {
  background-color: #e4e9fd;
  text-align: center;
  padding: 15px 0;
  cursor: pointer;
  font-weight: 600;
}
.tab-content {
  background-color: #ffffff;
  padding: 50px 40px;
}
.tab-content h4 {
  font-size: 28px;
  margin-bottom: 20px;
  color: #000224;
  font-weight: 600;
}
.tab-content p {
  text-align: justify;
  line-height: 1.9;
  letter-spacing: 0.4px;
  color: #202238;
}
.tab-content div {
  display: none;
}
.tab-content .active {
  display: block;
}
.tabs .active {
  background-color: #ffffff;
  color: #4d5bf9;
}

Restaurant Website Using HTML and CSS

JavaScript Code for Tabs

Last stage of the project the JavaScript in which we have added the logical and coded as per the requirement with some conditions. Let us see the Final Output of the project How To Create Tabs using HTML, CSS & JavaScript (Source Code).

ADVERTISEMENT

let tabs = document.querySelectorAll(".tabs h3");
let tabContents = document.querySelectorAll(".tab-content div");

tabs.forEach((tab, index) => {
  tab.addEventListener("click", () => {
    tabContents.forEach((content) => {
      content.classList.remove("active");
    });
    tabs.forEach((tab) => {
      tab.classList.remove("active");
    });
    tabContents[index].classList.add("active");
    tabs[index].classList.add("active");
  });
});

Final Output

ADVERTISEMENT

We have Successfully created our How To Create Tabs using HTML, CSS & JavaScript (Source Code). You can use this project for your personal needs and the respective lines of code are given with the code pen link mentioned above.

If you find out this Blog helpful, then make sure to search code with random on google for Front End Projects with Source codes and make sure to Follow the Code with Random Instagram page.

Code Idea – codingartist

Written By – Harsh Sawant

Code By – @harshh9

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Telegram Group Join Now

Share on:

Leave a Comment

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT