How To Make JavaScript Tabs With CSS And HTML

One popular feature that web developers often incorporate is JavaScript tabs.

These tabs allow users to navigate through different sections of a webpage seamlessly.

If you’re interested in learning how to make JavaScript tabs, you’ve come to the right place!

In this comprehensive guide, we’ll walk you through the process step by step, ensuring you have all the knowledge and tools necessary to implement this feature successfully.

What is JavaScript Tabs?

JavaScript tabs are a user interface element commonly used on websites to organize content into distinct sections.

They allow users to switch between different sections of content without having to load separate webpages.

Additionally, each tab represents a specific section, and when clicked, the corresponding content is displayed while hiding the content of other tabs.

JavaScript is utilized to add interactivity and functionality to the tabs, making them an excellent tool for enhancing user experience.

Types of Tabs you can create

Here are some common tab styles/layouts used in user interfaces:

  • Standard Tabs: Horizontal row of tabs at the top.
  • Pill Tabs: Rounded rectangular tabs.
  • Vertical Tabs: Tabs positioned vertically on the side.
  • Accordion Tabs: Collapsible tabs, one open at a time.
  • Scrollable Tabs: Horizontal tabs with scroll option.
  • Icon Tabs: Tabs with icons instead of or alongside text labels.
  • Dropdown Tabs: Tabs displayed in a dropdown menu

Why Should You Use JavaScript Tabs?

JavaScript tabs offer several advantages that make them a popular choice for web developers.

Here are some compelling reasons to consider using JavaScript tabs on your website:

Improved User Experience

JavaScript tabs provide a seamless and intuitive way for users to navigate through different sections of content on a webpage.

This enhances the overall user experience and makes it easier for visitors to find the information they need.

Space Efficiency

By using tabs, you can effectively organize and display a large amount of content within a limited space.

This is particularly useful when dealing with information-heavy websites or pages where scrolling can become cumbersome.

Faster Loading Times

JavaScript tabs eliminate the need to load multiple webpages when accessing different sections of content.

This can significantly improve loading times, resulting in a snappier and more responsive website.

Easy Customization

JavaScript tabs are highly customizable, allowing you to tailor their appearance and behavior to match your website’s design and requirements.

You can modify colors, styles, and animations to create a unique and visually appealing user interface.

How to make Javascript Tabs?

Creating JavaScript tabs may seem like a daunting task, but with the right guidance, it can be a straightforward process.

In this section, we’ll explore the step-by-step approach to making JavaScript tabs.

Step 1: Setting Up the HTML Structure

The HTML structure provides the foundation for your JavaScript tabs. It involves creating a container element and defining the tabs and their corresponding content.

Here’s an example of the HTML structure:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="src/style.css">
</head>

<body>
  <h1 id="header"></h1>
  <div class="tabs-container">
    <ul class="tabs">
      <li><a href="#tab1">Tab 1</a></li>
      <li><a href="#tab2">Tab 2</a></li>
      <li><a href="#tab3">Tab 3</a></li>
    </ul>
    <div id="tab1" class="tab-content">Content for Tab 1</div>
    <div id="tab2" class="tab-content">Content for Tab 2</div>
    <div id="tab3" class="tab-content">Content for Tab 3</div>
  </div>


  <script src="src/script.js"></script>
</body>

</html>

In this example, we have a container element with a class of “tabs-container.” Inside the container, we have an unordered list (<ul>) with a class of “tabs.”

Each list item (<li>) represents a tab and contains an anchor (<a>) element with an href attribute pointing to the corresponding content div.

The content divs have unique IDs and a class of “tab-content.”

Step 2: Styling the Tabs with CSS

After setting up the HTML structure, it’s time to style the tabs using CSS.

This step allows you to customize the appearance of the tabs to match your website’s design.

Here’s an example CSS code to get you started:

.tabs-container .tabs li {
  display: inline-block;
  margin-right: 10px;
}

.tabs-container .tabs li a {
  text-decoration: none;
  color: #e41111;
  padding: 5px 10px;
  border: 1px solid #bb0a0a;
  border-bottom: none;
  border-radius: 5px 5px 0 0;
}

.tabs-container .tabs li a:hover {
  background-color: #15f169;
}

.tabs-container .tab-content {
  display: none;
  padding: 10px;
  border: 1px solid #1511e6;
  border-top: none;
}

This CSS code styles the tabs and their corresponding content. It sets the tabs to display inline-block, adds padding, borders, and border-radius for styling.

The content divs are initially hidden with display: none and revealed when their corresponding tab is clicked.

Step 3: Adding JavaScript Functionality

The final step involves adding JavaScript functionality to make the tabs interactive.

This step ensures that when a tab is clicked, the corresponding content is displayed while hiding the content of other tabs.

Here’s an example JavaScript code to achieve this behavior:

const tabs = document.querySelectorAll('.tabs-container .tabs li');
const tabContents = document.querySelectorAll('.tabs-container .tab-content');

tabs.forEach((tab, index) => {
  tab.addEventListener('click', () => {
    // Hide all tab contents
    tabContents.forEach(content => {
      content.style.display = 'none';
    });

    // Show the clicked tab content
    tabContents[index].style.display = 'block';
  });
});

In this JavaScript code, we select all the tabs and their corresponding content elements using querySelectorAll.

Then, we add a click event listener to each tab. When a tab is clicked, we hide all the tabcontent elements by setting their display property to “none“.

We then show the content of the clicked tab by setting its display property to “block“.

This way, only the content of the active tab is visible, while the rest remain hidden.

Here is the putout by following the steps above:

Javascript tabs output

Note: You can now customize the code depending on what is suitable for your liking.

To learn more about JavaScript functions here are other resources you can check out:

Conclusion

In conclusion, JavaScript tabs are a powerful tool for organizing and displaying content on your website.

By following the steps outlined in this guide, you can create interactive and visually appealing tabs that enhance user experience.

Remember to customize the styles to match your website’s design and test your tabs across different browsers.

With JavaScript tabs, you can provide a seamless navigation experience for your users and make your website more engaging and user-friendly.

Frequently Asked Questions

Is JavaScript still worth learning in 2026?
Yes. JavaScript runs on 98% of websites for the front-end, dominates the back-end via Node.js, powers mobile apps through React Native, builds desktop tools through Electron, and is the scripting layer for most AI tooling (LangChain.js, OpenAI SDK, Vercel AI). Whether you target web, mobile, AI, or full-stack capstones, JavaScript is the broadest single language you can learn.
What is the difference between var, let, and const?
var is function-scoped, hoisted to the top of its scope, and can be redeclared, which leads to bugs in modern code. let is block-scoped (only visible inside the nearest {}) and can be reassigned. const is block-scoped and cannot be reassigned, although object contents can still mutate. Default to const for everything, switch to let only when you actually need to reassign, and avoid var in any code written after 2017.
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds useful features like top-level await, private class fields with the # prefix, and the .at() array method. If you are writing for older browsers (IE11 or older Android WebViews), transpile down with Babel or use a build tool like Vite, esbuild, or webpack.
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard, free, with built-in IntelliSense, debugger, terminal, Git, and a huge extension marketplace (ESLint, Prettier, GitHub Copilot, Tailwind). Install the JavaScript and TypeScript Nightly extension for the latest language features. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, the Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools with F12 (or right-click then Inspect), go to the Console tab, type or paste your code, press Enter. For HTML pages, add a script tag pointing to your .js file. Locally with Node.js: download Node from nodejs.org (LTS version), then run node script.js in your terminal from the file folder. Use the same Node setup for backend capstones, API integrations, and scripts that do not need a browser.
What can I build with JavaScript for my BSIT capstone?
Common BSIT capstones in JavaScript: full-stack web apps using React or Vue on the front-end with Node.js and Express on the back-end (MongoDB or MySQL for the database), real-time chat or notification systems using Socket.io, single-page dashboards with Chart.js or D3.js, cross-platform mobile apps with React Native, AI-powered chatbots using OpenAI SDK and LangChain.js, and Chrome extensions for productivity tools. Add Tailwind CSS for the UI and Vercel or Netlify for free deployment.
Glay Eliver

Programmer & Technical Writer at PIES IT Solution

Glay Eliver is a programmer and writer at PIES IT Solution, author of over 600 tutorials at itsourcecode.com. Specializes in JavaScript tutorials, Microsoft Office how-tos (Excel, Word, PowerPoint), and Python error debugging covering ImportError, TypeError, AttributeError, ModuleNotFoundError, and JavaScript ReferenceError. Authored several of the site’s highest-traffic Excel and MS Office reference articles.

Expertise: JavaScript · MS Excel · MS Word · MS PowerPoint · Python · Python ImportError · Python TypeError · Python AttributeError · ModuleNotFoundError · JavaScript ReferenceError · Pygame  · View all posts by Glay Eliver →

Leave a Comment