- Tab 1
- Tab 2
- Tab 3
Good morning. This is the content of Tab 1.
Hello. This is the content of Tab 2.
Good evening. This is the content of Tab 3.
HTML
<div class="tab-container">
<ul>
<li class="selected" data-id="tab-1">Tab 1</li>
<li data-id="tab-2">Tab 2</li>
<li data-id="tab-3">Tab 3</li>
</ul>
<div class="tab-content selected" id="tab-1">Good morning. This is the content of Tab 1.</div>
<div class="tab-content" id="tab-2">Hello. This is the content of Tab 2.</div>
<div class="tab-content" id="tab-3">Good evening. This is the content of Tab 3.</div>
</div>
CSS
.tab-container ul {
margin: 0;
padding: 0;
list-style: none;
display: flex;
border-bottom: none;
}
.tab-container ul li {
flex: 1;
padding: 16px 24px;
text-align: center;
cursor: pointer;
transition: all 0.3s ease;
position: relative;
color: #999;
font-weight: 300;
letter-spacing: 0.5px;
}
.tab-container ul li.selected {
color: #333;
font-weight: 500;
}
.tab-container ul li.selected::after {
content: '';
position: absolute;
bottom: -1px;
left: 0;
right: 0;
height: 2px;
background: #333;
animation: slideIn 0.3s ease;
}
@keyframes slideIn {
from {
transform: scaleX(0);
}
to {
transform: scaleX(1);
}
}
.tab-container ul li:not(.selected):hover {
color: #333;
}
.tab-content {
display: none;
padding: 32px 24px;
min-height: 150px;
color: #333;
line-height: 1.6;
}
.tab-content.selected {
display: block;
animation: fadeIn 0.3s ease;
}
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(10px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
JavaScript
const tabContainers = document.querySelectorAll('.tab-container');
tabContainers.forEach(container => {
const tabMenuItems = container.querySelectorAll('ul li');
const tabContents = container.querySelectorAll('.tab-content');
tabMenuItems.forEach(tabMenuItem => {
tabMenuItem.addEventListener('click', () => {
tabMenuItems.forEach(item => item.classList.remove('selected'));
tabMenuItem.classList.add('selected');
tabContents.forEach(tabContent => tabContent.classList.remove('selected'));
document.getElementById(tabMenuItem.dataset.id).classList.add('selected');
});
});
});