用CSS实现tab切换的方法原理和代码是什么
Admin 2022-06-21 群英技术资讯 1405 次浏览
今天这篇给大家分享的知识是“用CSS实现tab切换的方法原理和代码是什么”,小编觉得挺不错的,对大家学习或是工作可能会有所帮助,对此分享发大家做个参考,希望这篇“用CSS实现tab切换的方法原理和代码是什么”文章能帮助大家解决问题。tab切换在项目中也算是常用技术,一般实现tab切换都用js或者jq实现,今天介绍两种只用css实现tab切换方法:
方法一:
原理:通过label标签的关联属性和input的单选类型实现相应div的显示
1.创建一个类名为wrap的div当作容器
2.创建四个label标签,这将作为tab切换项
3.在每一个label中创建一个span标签(导航内容),input标签(实现选中于取消选中)type类型为radio,还要创建一个div作为这个导航项被点中是显示内容框,
这里要注意的是input标签的name必须是相同的,我这边取名叫tab
最终HTML为下面这样:
<div class="wrap">
<label>
<span>home</span>
<input type="radio" name="tab" checked>
<div>home-page</div>
</label>
<label>
<span>list</span>
<input type="radio" name="tab">
<div>list-page</div>
</label>
<label>
<span>news</span>
<input type="radio" name="tab">
<div>news-page</div>
</label>
<label>
<span>mine</span>
<input type="radio" name="tab">
<div>mine-page</div>
</label>
</div>
重要的css,通过将input的width设为0使得input的那个小圆点不现实,又通过label的关联用导航项的点击实现input的checked,然后通过input:checked+div{display:block}实现相应div的显示
<style type="text/css">
*{margin: 0;padding: 0;}
.wrap{
margin: 20px auto;
width: 403px;
height: 600px;
border:1px solid brown;
position: relative;
}
label{
width: 100px;
height: 30px;
float: left;
text-align: center;
line-height:30px;
border-right: 1px solid brown;
border-bottom: 1px solid brown;
}
label:nth-of-type(4){
border-right: none;
}
label span{
cursor: pointer;
}
label div{
width: 403px;
height: 568px;
position: absolute;
left: 0;
top: 31px;
background: #eeeeee;
display: none;
}
label input{
width: 0;
}
input:checked+div{
display: block;
}
</style>
方法二:
原理:通过a标签的锚点实现切换,也就a的href的路径是要切换div的id
1.创建一个类名为wrap的div作为容器
2.创建一个类名为nav的div,在里边创建四个a标签,a标签的href分别是要切换到的div的id
3.创建一个和nav兄弟关系的类名为sh的容器用来放置切换的div
4.创建显示内容div,id分别和上面a标签对应
最终代码如下:
<div class="wrap">
<div class="nav">
<a href="#home">home</a>
<a href="#list">list</a>
<a href="#news">news</a>
<a href="#mine">mine</a>
</div>
<div class="sh">
<div id="home">home-page</div>
<div id="list">list-page</div>
<div id="news">news-page</div>
<div id="mine">mine-page</div>
</div>
</div>
css样式设置,即将类名为sh下的div设置为display:none;然后通过div:target{display:block}实现显示选中项
<style type="text/css">
*{margin: 0;padding: 0}
.wrap{
width: 400px;
height: 600px;
border: 1px solid brown;
margin: 20px auto;
position: relative;
}
.nav{
width: 100%;
height: 30px;
}
.nav a{
width: 99px;
height: 30px;
text-align: center;
line-height: 30px;
border-right: 1px solid brown;
border-bottom: 1px solid brown;
float: left;
text-decoration: none;
color:black;
}
.sh{
width: 400px;
height: 569px;
position: absolute;
left: 0;
top:31px;
background: #eeeeee;
}
.sh div{
display: none;
text-align: center;
}
.sh div:target{
display: block;
}
</style>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要介绍了原生 JS+CSS+HTML 实现时序图的方法,本文通过实例代码图文介绍的非常详细,需要的朋友可以参考下
在css中,可以给图片元素添加“图片元素{animation:名称 时间}@keyframes 名称{0%{opacity:1;}99%{opacity:1;}100%{opacity:0;}}”样式,实现图片出现一秒后消失效果。
css中,可以利用overflow属性来实现字体溢出隐藏效果。下面通过代码示例来介绍一下overflow属性如何实现字体溢出隐藏<!DOCTYPE html><html><head><meta charset="utf-8"><sty
详解CSS实现定位HTML元素的知识有哪些?小编觉得挺不错的,对大家学习或是工作可能会有所帮助,对此分享发大家做个参考,希望这篇文章能帮助大家解决问题。
css中文字在背景图片上的效果是怎样做的?很多朋友在做网页的时候,想要文字在背景图上方的效果,其实实现方法很简单,设置文字z-index属性高于图片就可以啦,接下来我们来看具体的实例。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008