Tabs - Commerce Ventures
Tabs
Tabs are a common user interface element that allow users to switch between different views or sections within a single page or application. They help to organize content in a way that is visually appealing and easy to navigate.
Creating Tabs with HTML and CSS
To create tabs using HTML and CSS, you can follow the structure below:
<div class="tabs">
<ul class="tab-list">
<li class="tab active">Tab 1</li>
<li class="tab">Tab 2</li>
<li class="tab">Tab 3</li>
</ul>
<div class="tab-content">
<div class="tab-item active">Content for Tab 1</div>
<div class="tab-item">Content for Tab 2</div>
<div class="tab-item">Content for Tab 3</div>
</div>
</div>
Styling Tabs with CSS
Here’s an example of how to style tabs with CSS:
.tabs {
display: flex;
flex-direction: column;
}
.tab-list {
display: flex;
list-style: none;
padding: 0;
}
.tab {
padding: 10px 20px;
cursor: pointer;
background-color: #f1f1f1;
border: 1px solid #ccc;
margin-right: 5px;
}
.tab.active {
background-color: #fff;
border-bottom: none;
}
.tab-content {
border: 1px solid #ccc;
padding: 10px;
}
.tab-item {
display: none;
}
.tab-item.active {
display: block;
}
JavaScript for Tab Functionality
To make the tabs functional, we can add a simple JavaScript snippet:
document.querySelectorAll('.tab').forEach(tab => {
tab.addEventListener('click', () => {
document.querySelector('.tab.active').classList.remove('active');
tab.classList.add('active');
const index = Array.from(tab.parentNode.children).indexOf(tab);
document.querySelector('.tab-item.active').classList.remove('active');
document.querySelectorAll('.tab-item')[index].classList.add('active');
});
});
Conclusion
Tabs are an effective way to manage and present content in web applications, improving user experience by allowing easy access to different sections without loading multiple pages.