Tabbed navigation is a great way to organize content into separate sections, improving the user experience by reducing clutter on your page. In this beginner-friendly tutorial, you’ll learn how to build interactive tabs using HTML, CSS, and JavaScript, following clean code practices.
🧠 What Are Tabs?
Tabs are a UI component that allow users to switch between different sections of content without leaving the page. Each tab corresponds to a content area, and only one section is visible at a time.
This makes it perfect for:
- FAQs
- Settings pages
- Product details
- And more!
👁️ Preview
🛠️ Step 1: Set Up the HTML
Let’s start by creating the basic structure with HTML.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Tabbed Interface</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="tabs">
<div class="tab-buttons">
<button class="tab-btn active" data-tab="tab1">Tab 1</button>
<button class="tab-btn" data-tab="tab2">Tab 2</button>
<button class="tab-btn" data-tab="tab3">Tab 3</button>
</div>
<div class="tab-content active" id="tab1">
<p>This is the content for Tab 1.</p>
</div>
<div class="tab-content" id="tab2">
<p>This is the content for Tab 2.</p>
</div>
<div class="tab-content" id="tab3">
<p>This is the content for Tab 3.</p>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
🎨 Step 2: Style the Tabs with CSS
Now let’s make everything look nice and clean.
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: Arial, sans-serif;
padding: 2rem;
}
.tabs {
max-width: 600px;
margin: 0 auto;
}
.tab-buttons {
display: flex;
border-bottom: 2px solid #ddd;
margin-bottom: 1rem;
}
.tab-btn {
flex: 1;
padding: 1rem;
background-color: #f9f9f9;
border: none;
cursor: pointer;
transition: background-color 0.3s ease;
}
.tab-btn:hover {
background-color: #e0e0e0;
}
.tab-btn.active {
background-color: #fff;
border-bottom: 2px solid #007bff;
font-weight: bold;
}
.tab-content {
display: none;
}
.tab-content.active {
display: block;
}
⚙️ Step 3: Add Interactivity with JavaScript
Now let’s make the tabs actually switch content when clicked.
const tabButtons = document.querySelectorAll(".tab-btn");
const tabContents = document.querySelectorAll(".tab-content");
tabButtons.forEach((button) => {
button.addEventListener("click", () => {
const target = button.getAttribute("data-tab");
tabButtons.forEach((btn) => btn.classList.remove("active"));
tabContents.forEach((tab) => tab.classList.remove("active"));
button.classList.add("active");
document.getElementById(target).classList.add("active");
});
});
This small script:
- Listens for a click on any tab button
- Hides all content sections
- Removes the “active” class from all buttons
- Adds the “active” class to the clicked button and its corresponding content section
✅ Final Result
You now have a fully functional tab component using just HTML, CSS, and JavaScript! This is a very useful building block in modern front-end development.
🧩 Extra Tips
- You can use icons or animations to improve UI
- For accessibility, consider adding
role="tablist"
,role="tab"
, and ARIA attributes - For dynamic tabs, explore how to create tabs from data with JavaScript