- 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="container">
<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>
</div>
CSS
.container {
max-width: 800px;
width: 100%;
}
.tab-container {
position: relative;
}
.tab-container ul {
margin: 0;
padding: 0;
list-style: none;
display: flex;
border-bottom: none;
position: relative;
z-index: 2;
}
.tab-container ul li {
flex: 1;
padding: 18px 24px;
text-align: center;
cursor: pointer;
transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
position: relative;
color: #6c757d;
font-weight: 500;
letter-spacing: 0.5px;
border: 3px solid transparent;
border-radius: 12px 12px 0 0;
margin: 0 2px;
background: rgba(255, 255, 255, 0.6);
backdrop-filter: blur(10px);
-webkit-backdrop-filter: blur(10px);
overflow: hidden;
}
.tab-container ul li::before {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: linear-gradient(135deg,
rgba(102, 126, 234, 0.1) 0%,
rgba(118, 75, 162, 0.1) 100%);
opacity: 0;
transition: opacity 0.4s ease;
border-radius: 12px 12px 0 0;
}
.tab-container ul li:hover::before {
opacity: 1;
}
.tab-container ul li.selected {
color: #fff;
font-weight: 600;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
border: 3px solid #667eea;
border-bottom: none;
box-shadow:
0 -4px 16px rgba(102, 126, 234, 0.3),
0 0 0 1px rgba(255, 255, 255, 0.2);
transform: translateY(-2px);
z-index: 3;
}
.tab-container ul li.selected::after {
content: '';
position: absolute;
bottom: -3px;
left: 0;
right: 0;
height: 3px;
background: linear-gradient(90deg, #667eea, #764ba2);
border-radius: 0 0 3px 3px;
box-shadow: 0 2px 8px rgba(102, 126, 234, 0.4);
}
.tab-container ul li:not(.selected):hover {
color: #667eea;
background: rgba(255, 255, 255, 0.9);
border-color: rgba(102, 126, 234, 0.3);
transform: translateY(-1px);
box-shadow: 0 4px 12px rgba(102, 126, 234, 0.15);
}
.tab-content {
display: none;
padding: 32px;
min-height: 180px;
color: #495057;
line-height: 1.7;
border: 3px solid #667eea;
border-top: none;
border-radius: 0 0 16px 16px;
background: #fff;
position: relative;
z-index: 2;
margin-top: -3px;
margin-left: 2px;
margin-right: 2px;
}
.tab-content::before {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
height: 4px;
background: linear-gradient(90deg, #667eea, #764ba2, #667eea);
border-radius: 0 0 4px 4px;
animation: gradientFlow 3s ease-in-out infinite;
}
@keyframes gradientFlow {
0%, 100% {
background: linear-gradient(90deg, #667eea, #764ba2, #667eea);
}
50% {
background: linear-gradient(90deg, #764ba2, #667eea, #764ba2);
}
}
.tab-content.selected {
display: block;
animation: outlineFadeIn 0.5s cubic-bezier(0.4, 0, 0.2, 1);
}
@keyframes outlineFadeIn {
from {
opacity: 0;
transform: translateY(-15px) scale(0.98);
filter: blur(2px);
}
to {
opacity: 1;
transform: translateY(0) scale(1);
filter: blur(0);
}
}
/* レスポンシブデザイン */
@media (max-width: 768px) {
.tab-container ul li {
padding: 14px 16px;
font-size: 14px;
}
.tab-content {
padding: 24px;
min-height: 150px;
}
}
JavaScript
document.addEventListener('DOMContentLoaded', function() {
const tabs = document.querySelectorAll('.tab-container ul li');
const contents = document.querySelectorAll('.tab-content');
tabs.forEach(tab => {
tab.addEventListener('click', function() {
const targetId = this.getAttribute('data-id');
// すべてのタブからselectedクラスを削除
tabs.forEach(t => t.classList.remove('selected'));
contents.forEach(c => c.classList.remove('selected'));
// クリックされたタブと対応するコンテンツにselectedクラスを追加
this.classList.add('selected');
document.getElementById(targetId).classList.add('selected');
});
});
});