how to create tab in html

"Create Tabs in HTML: A Step-by-Step Guide"

By Sarah Jones
AD

Creating tabs in HTML is a fundamental aspect of web development, providing a clean, organized way to divide and manage content. It enhances user experience by simplifying navigation, and it's relatively straightforward to implement. Here's a comprehensive guide on how to create tabs in HTML.

Tabs are typically implemented using a combination of HTML, CSS, and sometimes JavaScript. We'll start with the basics of creating tabs using only HTML and CSS, then explore adding functionality with JavaScript. Let's dive in.

tabs with HTML and CSS

To create basic tabs using HTML and CSS, you'll need to structure your HTML and style it to display the tabs and their content.

AD
HTML Tag
HTML Tag
HTML Cheat Sheet 📄✨ Quick Guide for Beginners
HTML Cheat Sheet 📄✨ Quick Guide for Beginners
Very Simple Tabs With Pure HTML CSS JS (Free Download)
Very Simple Tabs With Pure HTML CSS JS (Free Download)

First, let's structure our HTML. We'll use a simple unordered list for our tab navigation and another unordered list for the tab content.

HTML Structure

For our HTML structure, we'll use a container div, then an unordered list for our tabs, and another unordered list for our tab content. Each tab and its content will be in a list item.

the ultimate guide to creating and formating tags
the ultimate guide to creating and formating tags
HTML Full Notes for Beginners 💻
HTML Full Notes for Beginners 💻

basic HTML structure

<div id="tabs"> <ul> <li>Tab 1</li> <li>Tab 2</li> <li>Tab 3</li> </ul> <ul> <li>Content 1</li> <li>Content 2</li> <li>Content 3</li> </ul> </div>

Next, we'll style our tabs with CSS.

AD

CSS Styling

We'll use CSS to make our tabs look and behave like tabs. We'll style the unordered lists to display as horizontal lists, add some padding and background color to the list items, and hide the content we don't want to initially display.

#tabs ul { list-style-type: none; margin: 0; padding: 0; } #tabs li { display: inline; padding: 10px 20px; background-color: #ddd; } #tabs li:not(:first-child) { border-left: 1px solid #999; } #tabs ul:nth-child(2) li { display: none; } /* Add your own styling here */

Adding functionality with JavaScript

Our tabs look great, but they're not functional yet. Let's add some JavaScript to make the tab content switch when we click on a tab.

AD

We'll use JavaScript to add an event listener to our tab navigation, then show the corresponding content when a tab is clicked.

Basic JavaScript Functionality

Let's add some simple JavaScript to our HTML. We'll select our tab navigation and content, then add a click event listener to our tab navigation. When a tab is clicked, we'll loop through the content, showing the one that corresponds to the clicked tab and hiding the rest.

document.querySelector('#tabs ul:first-child').addEventListener('click', function (e) { if (e.target.tagName === 'LI') { const content = document.querySelector('#tabs ul:nth-child(2)'), tabs = document.querySelectorAll('#tabs ul:first-child li'), target = e.target; content.querySelectorAll('li').forEach(function (li, index) { li.style.display = (index === Array.from(tabs).indexOf(target)) ? 'block' : 'none'; }); } });

AD

Improving JavaScript Functionality

Our tabs are now functional, but we can make them even better. Let's add a class to our active tab and content, remove it from the previous active tab and content, and prevent the whole page from jumping when a tab is clicked.

document.querySelector('#tabs ul:first-child').addEventListener('click', function (e) { if (e.target.tagName === 'LI') { const content = document.querySelector('#tabs ul:nth-child(2)'), tabs = document.querySelectorAll('#tabs ul:first-child li'), target = e.target, previous = document.querySelector('#tabs li.active') || tabs[0]; // Remove the active class from the previous tab and content previous.classList.remove('active'); content.querySelector('[data-target="' + previous.getAttribute('data-target') + '"]').classList.remove('active'); // Add the active class to the clicked tab and content target.classList.add('active'); content.querySelector('[data-target="' + target.getAttribute('data-target') + '"]').classList.add('active'); // Prevent the whole page from jumping when a tab is clicked e.preventDefault(); } });

And there you have it! You now know how to create tabs in HTML, style them with CSS, and add functionality with JavaScript. Explore and experiment to create your own unique tabs. Happy coding!

Why stop at HTML tabs? Next, you could try creating accordions, menus, or even a whole web application. The sky's the limit with web development, so keep practicing and expanding your skills.

AD