CSS实现tab选项卡切换

CSS实现tab选项卡切换

实现Tab选项卡切换可以通过纯CSS完成,主要利用 :target 伪类或 :checked 伪类结合 <input type="radio"> 来实现。以下是两种常见的实现方式:

方法一:使用 :target 伪类

:target 伪类可以匹配当前URL的片段标识符(即 # 后面的部分),通过点击链接切换内容。

HTML结构

html 复制代码
<div class="tab-container">
    <div class="tab-nav">
        <a href="#tab1">Tab 1</a>
        <a href="#tab2">Tab 2</a>
        <a href="#tab3">Tab 3</a>
    </div>
    <div class="tab-content">
        <div id="tab1" class="tab-item">Content 1</div>
        <div id="tab2" class="tab-item">Content 2</div>
        <div id="tab3" class="tab-item">Content 3</div>
    </div>
</div>

CSS样式

css 复制代码
.tab-container {
    width: 100%;
    max-width: 600px;
    margin: 0 auto;
}

.tab-nav {
    display: flex;
    border-bottom: 2px solid #ccc;
}

.tab-nav a {
    padding: 10px 20px;
    text-decoration: none;
    color: #333;
    background-color: #f1f1f1;
    margin-right: 5px;
    border: 1px solid #ccc;
    border-bottom: none;
}

.tab-nav a:hover {
    background-color: #ddd;
}

.tab-content {
    padding: 20px;
    border: 1px solid #ccc;
    border-top: none;
}

.tab-item {
    display: none;
}

.tab-item:target {
    display: block;
}

说明:

  • 点击链接时,URL会改变为 #tab1#tab2 等,:target 伪类会匹配对应的内容并显示。
  • 其他未匹配的内容会隐藏。

方法二:使用 <input type="radio">:checked 伪类

通过单选框(<input type="radio">)和 :checked 伪类实现选项卡切换,无需改变URL。

HTML结构

html 复制代码
<div class="tab-container">
    <div class="tab-nav">
        <label for="tab1">Tab 1</label>
        <label for="tab2">Tab 2</label>
        <label for="tab3">Tab 3</label>
    </div>
    <div class="tab-content">
        <input type="radio" name="tabs" id="tab1" checked>
        <div class="tab-item">Content 1</div>

        <input type="radio" name="tabs" id="tab2">
        <div class="tab-item">Content 2</div>

        <input type="radio" name="tabs" id="tab3">
        <div class="tab-item">Content 3</div>
    </div>
</div>

CSS样式

css 复制代码
.tab-container {
    width: 100%;
    max-width: 600px;
    margin: 0 auto;
}

.tab-nav {
    display: flex;
    border-bottom: 2px solid #ccc;
}

.tab-nav label {
    padding: 10px 20px;
    text-decoration: none;
    color: #333;
    background-color: #f1f1f1;
    margin-right: 5px;
    border: 1px solid #ccc;
    border-bottom: none;
    cursor: pointer;
}

.tab-nav label:hover {
    background-color: #ddd;
}

.tab-content {
    padding: 20px;
    border: 1px solid #ccc;
    border-top: none;
    position: relative;
}

.tab-item {
    display: none;
}

input[type="radio"] {
    display: none;
}

input[type="radio"]:checked + .tab-item {
    display: block;
}

说明:

  • 使用 <label> 标签关联 <input type="radio">,点击标签时会选中对应的单选框。
  • :checked 伪类用于匹配选中的单选框,显示对应的内容。

方法三:使用Flexbox布局(更简洁的实现)

结合Flexbox布局,可以更简洁地实现选项卡切换。

HTML结构

html 复制代码
<div class="tab-container">
    <div class="tab-nav">
        <input type="radio" name="tabs" id="tab1" checked>
        <label for="tab1">Tab 1</label>

        <input type="radio" name="tabs" id="tab2">
        <label for="tab2">Tab 2</label>

        <input type="radio" name="tabs" id="tab3">
        <label for="tab3">Tab 3</label>
    </div>
    <div class="tab-content">
        <div class="tab-item">Content 1</div>
        <div class="tab-item">Content 2</div>
        <div class="tab-item">Content 3</div>
    </div>
</div>

CSS样式

css 复制代码
.tab-container {
    width: 100%;
    max-width: 600px;
    margin: 0 auto;
}

.tab-nav {
    display: flex;
    border-bottom: 2px solid #ccc;
}

.tab-nav label {
    padding: 10px 20px;
    text-decoration: none;
    color: #333;
    background-color: #f1f1f1;
    margin-right: 5px;
    border: 1px solid #ccc;
    border-bottom: none;
    cursor: pointer;
}

.tab-nav label:hover {
    background-color: #ddd;
}

.tab-content {
    display: flex;
    overflow: hidden;
}

.tab-item {
    flex: 1;
    padding: 20px;
    border: 1px solid #ccc;
    border-top: none;
    display: none;
}

input[type="radio"] {
    display: none;
}

#tab1:checked ~ .tab-content .tab-item:nth-child(1),
#tab2:checked ~ .tab-content .tab-item:nth-child(2),
#tab3:checked ~ .tab-content .tab-item:nth-child(3) {
    display: block;
}

说明:

  • 使用Flexbox布局实现选项卡内容的动态切换。
  • 通过 :checked 伪类控制显示对应的内容。

总结

  • :target 方法:简单易用,但会改变URL,适合静态页面。
  • <input type="radio"> 方法:无需改变URL,适合动态页面。
  • Flexbox方法:结合布局更灵活,适合复杂场景。

根据项目需求选择合适的方式即可!

更多vue相关插件及后台管理模板可访问vue admin reference,代码详情请访问github

相关推荐
Moment几秒前
为什么 Electron 项目推荐使用 Monorepo 架构 🚀🚀🚀
前端·javascript·github
掘金安东尼5 分钟前
🧭前端周刊第437期(2025年10月20日–10月26日)
前端·javascript·github
浩男孩10 分钟前
🍀【总结】使用 TS 封装几条开发过程中常使用的工具函数
前端
Mintopia16 分钟前
🧠 AIGC + 区块链:Web内容确权与溯源的技术融合探索
前端·javascript·全栈
晓得迷路了20 分钟前
栗子前端技术周刊第 103 期 - Vitest 4.0、Next.js 16、Vue Router 4.6...
前端·javascript·vue.js
Mintopia21 分钟前
🚀 Next.js Edge Runtime 实践学习指南 —— 从零到边缘的奇幻旅行
前端·后端·全栈
GISer_Jing26 分钟前
不定高虚拟列表性能优化全解析
前端·javascript·性能优化
Predestination王瀞潞41 分钟前
WEB前端技术基础(第三章:css-网页美化的衣装-上)
前端·css
鹏多多1 小时前
深度解析React中useEffect钩子的使用
前端·javascript·react.js
超级大只老咪1 小时前
CSS基础语法
前端