不用js,html+css也能轻松实现tab选项卡

很多人做tab选项卡会用到js,通过排他思想去实现选项卡,其实只用css也可以轻松实现。

效果如下
在这里插入图片描述
在这里插入图片描述

html代码如下

<div class="tab">
			
			<input type="radio" name="tab-one" id="r1" class="hid" checked />
			<input type="radio" name="tab-one" id="r2" class="hid" />
			<input type="radio" name="tab-one" id="r3" class="hid" />
			
			<ul class="label-list">
				<li>
					<label for="r1">选项卡1</label>
				</li>
				<li>
					<label for="r2">选项卡2</label>
				</li>
				<li>
					<label for="r3">选项卡3</label>
				</li>
			</ul>
			
			<div class="box-one">1</div>
			<div class="box-two">2</div>
			<div class="box-three">3</div>
			
		</div>

css

		<style>
			*{
				padding: 0px;
				margin: 0px;
			}
			body{
				background-color: #fff;
			}
			.tab{
				background-color: antiquewhite;
				width: 500px;
				height: 300px;
				position: absolute;
				top: 0px;
				bottom: 0px;
				left: 0px;
				right: 0px;
				margin: auto;
			}
			.box-one,
			.box-two,
			.box-three{
				width: 100%;
				height: 85%;
				display: none;
			}
			#r1:checked ~ .box-one{
				display: block;
			}
			#r1:checked ~ .label-list>li label[for="r1"]{
				color: blueviolet;
				background-color: antiquewhite;
			}
			#r2:checked ~ .box-two{
				display: block;
			}
			#r2:checked ~ .label-list>li label[for="r2"]{
				color: blueviolet;
				background-color: antiquewhite;
			}
			#r3:checked ~ .box-three{
				display: block;
			}
			#r3:checked ~ .label-list>li label[for="r3"]{
				color: blueviolet;
				background-color: antiquewhite;
			}
			.label-list{
				display: flex;
				background-color: #63ff40;
				height: 15%;
			}
			.label-list li{
				list-style-type: none;
			}
			.label-list li label{
				height: 100%;
				display: inline-block;
				display: flex;
				align-items: center;
				padding: 0px 10px;
				cursor: pointer;
			}
			.hid{
				display: none;
			}
		</style>

使用html+css实现tab选项卡,我们会用到单选框,一组单选框的每一个选项对应一个卡片。当单选框被选中时,对应的卡片就显示。

这里我们会用到一个css选择器,相邻选择器,也就是哪个波浪线(~),相邻选择器可以选择同级元素。

#r1:checked ~ .box-one{
				display: block;
			}
  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
HTML代码: ``` <div class="tab"> <button class="tablinks" onclick="openTab(event, 'tab1')">Tab 1</button> <button class="tablinks" onclick="openTab(event, 'tab2')">Tab 2</button> <button class="tablinks" onclick="openTab(event, 'tab3')">Tab 3</button> </div> <div id="tab1" class="tabcontent"> <h3>Tab 1 Content</h3> <p>This is the content of Tab 1.</p> </div> <div id="tab2" class="tabcontent"> <h3>Tab 2 Content</h3> <p>This is the content of Tab 2.</p> </div> <div id="tab3" class="tabcontent"> <h3>Tab 3 Content</h3> <p>This is the content of Tab 3.</p> </div> ``` CSS代码: ``` /* 隐藏所有选项内容 */ .tabcontent { display: none; } /* 样式化选项按钮 */ .tab { overflow: hidden; border: 1px solid #ccc; background-color: #f1f1f1; } .tab button { background-color: inherit; float: left; border: none; outline: none; cursor: pointer; padding: 14px 16px; transition: 0.3s; } .tab button:hover { background-color: #ddd; } .tab button.active { background-color: #ccc; } /* 样式化选项内容 */ .tabcontent { padding: 6px 12px; border: 1px solid #ccc; border-top: none; } ``` JavaScript代码: ``` function openTab(evt, tabName) { // 隐藏所有选项内容 var i, tabcontent, tablinks; tabcontent = document.getElementsByClassName("tabcontent"); for (i = 0; i < tabcontent.length; i++) { tabcontent[i].style.display = "none"; } // 取消所有选项按钮的激活状态 tablinks = document.getElementsByClassName("tablinks"); for (i = 0; i < tablinks.length; i++) { tablinks[i].className = tablinks[i].className.replace(" active", ""); } // 显示当前选项内容并激活按钮 document.getElementById(tabName).style.display = "block"; evt.currentTarget.className += " active"; } ``` 以上代码实现了一个简单的选项切换效果。点击每个按钮时,会显示对应的选项内容,并且激活该按钮。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值