CSS实现Tab布局的示例代码分享(图)

下面小编就为大家带来一篇CSS实现Tab布局的简单实例(必看)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

一、布局方式

1、内容与tab分离

内容1

内容2

内容3

内容4

ul,li{  margin:0;  padding:0;  list-style:none;}.container{  width:400px;  height:300px;  background-color:silver;}.tab-content{  width:100%;  height:80%;  overflow:hidden;}.tab-content .item{  width:100%;  height:100%;}.tab-control{  width:100%;  height:20%;}.tab-control ul{  height:100%;}.tab-control li{  width:25%;  height:100%;  float:left;  border:1px solid silver;  box-sizing:border-box;  background-color:white;  cursor: pointer;}.tab-control li:hover{  background-color:#7b7474}.tab-control a{  display:inline-block;  width:100%;  height:100%;  line-height:100%;  text-align:center;  text-decoration: none;}.tab-control a::after{  content:"";  display:inline-block;  height:100%;  vertical-align:middle;}.tab-content .item:target{  background:yellow;}

CSS实现Tab布局的示例代码分享(图)

2、内容与tab一体 

 

  • 1

    1

  • 2

    2

  • 3

    3

  • 4

    4

 

ul,li,p{  margin:0;  padding:0;  list-style:none;}.container{  width:400px;  height:300px;  background-color:silver;  border:1px solid silver;}.container ul{  width:100%;  height:100%;  overflow:hidden;}.container .item{  float:left;  width:25%;  height:100%;  background-color:white;}.container .item .title{  line-height:40px;  border:1px solid silver;  box-sizing:border-box;  text-align:center;  cursor:pointer;}.container .item .content{  width:400%;  height:100%;  background-color:yellow;}.ml1{  margin-left:-100%;}.ml2{  margin-left:-200%;}.ml3{  margin-left:-300%;}.active{  position:relative;  z-index:1}.container .item:hover{  position:relative;  z-index:1}.container .item:hover .title{  border-bottom:none;  background-color:yellow;}

CSS实现Tab布局的示例代码分享(图)

利用负margin,将内容区对齐,然后内容去添加背景色,避免不同tab对应的区域透视重叠。

二、CSS实现交互

立即学习“前端免费学习笔记(深入)”;

1、锚点实现(target)

(1)针对布局一:item从上往下排列,父元素tab-content加上overflow:hidden。利用锚点,点击不同a标签的时候,具有对应ID的item会切换到tab-content的视图中,然后利用hover给tab按钮加上切换样式。

 

内容1

内容2

内容3

内容4

 

ul,li{  margin:0;  padding:0;  list-style:none;}.container{  width:400px;  height:300px;  background-color:silver;}.tab-content{  width:100%;  height:80%;  overflow:hidden;}.tab-content .item{  width:100%;  height:100%;}.tab-control{  width:100%;  height:20%;}.tab-control ul{  height:100%;}.tab-control li{  width:25%;  height:100%;  float:left;  border:1px solid silver;  box-sizing:border-box;  background-color:white;  cursor: pointer;}.tab-control li:hover{  background-color:#7b7474}.tab-control a{  display:inline-block;  width:100%;  height:100%;  line-height:100%;  text-align:center;  text-decoration: none;}.tab-control a::after{  content:"";  display:inline-block;  height:100%;  vertical-align:middle;}

上述方法只是利用了锚点切换,没有使用:target。修改CSS

 

ul,li{  margin:0;  padding:0;  list-style:none;}.container{  width:400px;  height:300px;  background-color:silver;}.tab-content{  position:relative;  width:100%;  height:80%;  overflow:hidden;}.tab-content .item{  position:absolute;  left:0;  top:0;  width:100%;  height:100%;}.tab-control{  width:100%;  height:20%;}.tab-control ul{  height:100%;}.tab-control li{  width:25%;  height:100%;  float:left;  border:1px solid silver;  box-sizing:border-box;  background-color:white;  cursor: pointer;}.tab-control li:hover{  background-color:#7b7474}.tab-control a{  display:inline-block;  width:100%;  height:100%;  line-height:100%;  text-align:center;  text-decoration: none;}.tab-control a::after{  content:"";  display:inline-block;  height:100%;  vertical-align:middle;}.tab-content .item:target{  z-index:1;  background-color:yellow;}

item使用绝对定位,然后使用:target修改元素z-index达到切换效果(其实也可以通过控制元素的display来达到切换效果)

(2)针对布局二:

 

 

ul,li,p {  margin: 0;  padding: 0;  list-style: none;}.container {  width: 400px;  height: 300px;  background-color: silver;  border: 1px solid silver;}.container ul {  width: 100%;  height: 100%;  overflow: hidden;}.container .item {  float: left;  width: 25%;  height: 100%;  background-color: white;}.container .item .title {  line-height: 40px;  border: 1px solid silver;  box-sizing: border-box;  text-align: center;  cursor: pointer;}.container .item a {  display:inline-block;  width:100%;  height:100%;  text-decoration: none;}.container .item .content {  width: 400%;  height: 100%;  background-color: yellow;}.ml1 {  margin-left: -100%;}.ml2 {  margin-left: -200%;}.ml3 {  margin-left: -300%;}.active {  position: relative;  z-index: 1}.container .item:target {  position: relative;  z-index: 1}.container .item:target .title {  border-bottom: none;  background-color: yellow;}

 

2、hover实现

(1)针对布局一:

无法简单的通过CSS实现

(2)针对布局二:

 

  • 1

    1

  • 2

    2

  • 3

    3

  • 4

    4

 

ul,li,p{  margin:0;  padding:0;  list-style:none;}.container{  width:400px;  height:300px;  background-color:silver;  border:1px solid silver;}.container ul{  width:100%;  height:100%;  overflow:hidden;}.container .item{  float:left;  width:25%;  height:100%;  background-color:white;}.container .item .title{  line-height:40px;  border:1px solid silver;  box-sizing:border-box;  text-align:center;  cursor:pointer;}.container .item .content{  width:400%;  height:100%;  background-color:yellow;}.ml1{  margin-left:-100%;}.ml2{  margin-left:-200%;}.ml3{  margin-left:-300%;}.active{  position:relative;  z-index:1}.container .item:hover{  position:relative;  z-index:1}.container .item:hover .title{  border-bottom:none;  background-color:yellow;}

3、label与:checked实现

(1)针对布局一:

 

内容1

内容2

内容3

内容4

 

ul,li {  margin: 0;  padding: 0;  list-style: none;}.container {  width: 400px;  height: 300px;  background-color: silver;}.tab-content {  position: relative;  width: 100%;  height: 80%;  overflow: hidden;}input {  margin: 0;  width: 0;}.tab-content .item {  position: absolute;  left: 0;  top: 0;  width: 100%;  height: 100%;}.tab-control {  width: 100%;  height: 20%;}.tab-control ul {  height: 100%;}.tab-control li {  width: 25%;  height: 100%;  float: left;  border: 1px solid silver;  box-sizing: border-box;  background-color: white;}.tab-control li:hover {  background-color: #7b7474}.tab-control label {  display: inline-block;  width: 100%;  height: 100%;  line-height: 100%;  text-align: center;  text-decoration: none;  cursor: pointer;}.tab-control label::after {  content: "";  display: inline-block;  height: 100%;  vertical-align: middle;}.tab-content .radio-item{  display:none;}.tab-content .radio-item:checked+.item {  z-index: 1;  background-color: yellow;}

利用css :checked与+(选择紧接在另一个元素后的元素,而且二者有相同的父元素)选择符。

(2)针对布局二:

 

  • 1

  • 2

  • 3

  • 4

 

ul,li,p{  margin:0;  padding:0;  list-style:none;}.container{  width:400px;  height:300px;  background-color:silver;  border:1px solid silver;}.container ul{  width:100%;  height:100%;  overflow:hidden;}.container .item{  float:left;  width:25%;  height:100%;  background-color:white;}.container .item .title{  display:inline-block;  width:100%;  line-height:40px;  border:1px solid silver;  box-sizing:border-box;  text-align:center;  cursor:pointer;}.container .item .content{  position:relative;  width:400%;  height:100%;  background-color:yellow;}.ml1{  margin-left:-100%;}.ml2{  margin-left:-200%;}.ml3{  margin-left:-300%;}.radio-item{  display:none;}.radio-item:checked~.title{  background-color:yellow;  border-bottom:none;}.radio-item:checked~.content{  background-color:yellow;  z-index:1;}

以上就是CSS实现Tab布局的示例代码分享(图)的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1608482.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月23日 21:53:44
下一篇 2025年12月23日 21:54:02

相关推荐

  • 表格细边框的两种CSS实现方法

    在网页制作中,细边框这个制作方法是必不可少的。这里admin10000.com介绍2种常见的表格细边框制作方法,均通过XHTML验证。 表格细边框的两种CSS实现方法 /* 利用表格样式 border-collapse: collapse 实现细边框 */ .tab1 { width: 300px;…

    好文分享 2025年12月23日
    000
  • CSS命名及其书写的规范化

    CSS命名规范 一.文件命名规范 全局样式:global.css;框架布局:layout.css;字体样式:font.css;链接样式:link.css;打印样式:print.css; 二.常用类/ID命名规范 页 眉:header内 容:content容 器:container页 脚:footer…

    好文分享 2025年12月23日
    000
  • 用CSS hack技术解决浏览器兼容性问题

    什么是CSS Hack?   不同的浏览器对CSS的解析结果是不同的,因此会导致相同的CSS输出的页面效果不同,这就需要CSS Hack来解决浏览器局部的兼容性问题。而这个针对不同的浏览器写不同的CSS 代码的过程,就叫CSS Hack。 CSS Hack 形式   CSS Hack大致有3种表现形…

    好文分享 2025年12月23日
    000
  • 万能清除浮动样式

    这个是一个很流行的清除浮动的方法,在很多大项目上已经被完全采用。 这个方法来源于positioniseverything,通过after伪类:after和IEhack来实现,完全兼容当前主流浏览器。 .clearfix:after { content: “.”; clear: both; heigh…

    好文分享 2025年12月23日
    000
  • CSS初始化代码

    为什么要初始化CSS? CSS初始化是指重设浏览器的样式。不同的浏览器默认的样式可能不尽相同,所以开发时的第一件事可能就是如何把它们统一。如果没对CSS初始化往往会出现浏览器之间的页面差异。每次新开发网站或新网页时候通过初始化CSS样式的属性,为我们将用到的CSS或html标签更加方便准确,使得我们…

    好文分享 2025年12月23日
    000
  • 关于IE6下的CSS多类选择符

      多类选择符的写法。例如:   #my.c1.c2 { color:red;}  .c1.c2 { color:red;}   以上写法在IE7+/FF/Opera/Safari 等浏览器都支持。   但在IE6中,后一个类名会覆盖前一个类名,也就是说,上例被IE6理解为:   #my.c2 { …

    好文分享 2025年12月23日
    000
  • CSS实现背景透明,文字不透明(兼容各浏览器)

    在 FF/Chrome 等较新的浏览器中可以使用css属性background-color的rgba轻松实现背景透明,而文字保持不透明。而IE6/7/8浏览器不支持rgba,只有使用IE的专属滤镜filter:Alpha来实现,但是这样写法会把文字也变为透明,因此只有在透明容器的子节点(文本节点除外…

    2025年12月23日
    000
  • 教你如何用CSS创建打印页面

    用CSS创建打印页面,不必为打印而专门建立一个HTML文件,可以节省一些体力,其前提是按“WEB标准”用CSS+p布局HTML页面。 第一、在HTML页面加入为打印机设置的CSS文件 media=”screen” ,是面向屏幕的; media=”print&#82…

    好文分享 2025年12月23日
    000
  • CSS清除浮动方法总汇

    使用xhtml+css布局经常性地会使用到float,很多邪门的事儿都有可能是浮动在作怪,那么清除浮动就是必须要做的,而且随时性地对父级元素清除浮动的做法也被认为是书写CSS的良好习惯之一。 此为未清除浮动源代码,运行代码无法查看到父级元素浅黄色背景。 body { font-size:24px; …

    2025年12月23日
    000
  • 去掉超链接或按钮点击时出现的虚线边框

      在前端制作的过程中会发现,一些文字/图片链接,或者一些input控件,在点击时会在周围出现虚线边框,一般会在火狐和IE浏览器下出现虚线框,谷歌下不会有。   这些虚线边框是作为对视觉设计的一种辅助,在不使用鼠标,而用键盘Tab键进行页面浏览时,会标示出当前所在的链接或控件的位置,便于浏览。这对那…

    好文分享 2025年12月23日
    000
  • CSS命名规范及网站常用中英文对照表

    本文整理了常见的CSS文件命名规范、CSS样式ID命名规范及其网站中常用的中英文对照。 一、样式文件命名规范 主要的 master.css布局,版面 layout.css专栏 columns.css文字 font.css打印样式 print.css主题 themes.css 二、CSS注释 /*CS…

    好文分享 2025年12月23日
    000
  • CSS浮动与浮动清除(BFC)简单教程

    浮动 1. 什么是浮动 当元素的 float 属性不为 none 时就产生了浮动。 float .float { float: left; width: 100px; height: 100px; background-color: #ddd;} 2. 浮动的影响 浮动会使元素脱离文档流,具体表现为…

    2025年12月23日
    000
  • css各种实用技巧

    其它技巧和经验列表(*以下实例默认运行环境都为Standard mode): 如何让层在falsh上显示? 方法: 设置flash的wmode值为transparent或opaque 如何使用标准的方法在页面上插入flash? 方法: ![](*.jpg)至于flash的宽高可以在css里设置 如何…

    好文分享 2025年12月23日
    000
  • css之px自动转rem

    作为一名前端开发,尤其是在做移动端适配时,rem是我们经常用到的单位,它的好处大家可以自行搜索,网上已经有很多了。但是我们再将设计稿上的px转换成rem时,得手动的去计算,这是一个很耗时、费力的过程,有没有什么办法可以“解放”我们呢?(原谅我的懒~) 1.CSS处理器 Sass、LESS以及Post…

    2025年12月23日
    000
  • CSS教程(三)伪类——动态链接

    伪类可以看做是一种特殊的类选择符,是能被支持css的浏览器自动所识别的特殊选择符。它的最大的用处就是可以对链接在不同状态下定义不同的样式效果。 1.  语法 伪类的语法是在原有的语法里加上一个伪类(pseudo-class):selector:pseudo-class {property: valu…

    2025年12月23日
    000
  • CSS教程(四)如何在网页中插入CSS

    前两章我们了解了css的语法,但要想在浏览器中显示出效果,就要让浏览器识别并调用。当浏览器读取样式表时,要依照文本格式来读,这里介绍四种在页面中插入样式表的方法:链入外部样式表、内部样式表、导入外表样式表和内嵌样式。   链入外部样式表 链入外部样式表是把样式表保存为一个样式表文件,然后在页面中?l…

    2025年12月23日
    000
  • CSS教程(五)如何使用DW4创建CSS

    1. css styles面板 通过前面几章的学习,相信大家对css有了一定的了解,这一章我们来讲解如何利用dreamweaver4来创建css。首先运行dreamweaver4,启动后,选择菜单下的windows->css styles(或按shitf+f11),系统弹出css styles…

    2025年12月23日
    000
  • CSS教程(六) DW4中CSS属性详解

    在dreamweaver4的css样式里包含了w3c规范定义的所有css1的属性,dreamweaver4把这些属性分为type(类型)、background(背景)、block(块)、box(盒子)、border(边框)、 list(列表)、positioning(定位)、extensions(扩…

    2025年12月23日
    000
  • CSS教程(七) 滤镜

    css提供了一些内置的多媒体滤镜特效,使用这种技术可以把可视化的滤镜和转换效果添加到一个标准的html元素上,例如图片、文本容器、以及其他一些对象。dreamweaver4提供了16种滤镜可供选择,如下图: 下面,我们就来看看在dreamweaver4里如何方便的使用这些css滤镜。 建立一个自定义…

    2025年12月23日
    000
  • CSS教程(八) 简单介绍CSS结合JS的运用

    八、 简单介绍css结合js的运用(针对事件动作) 利用css配合javascript的可以做很多更酷的动态页面效果,在本教程的最后给大家简单介绍一下css配合js的应用。首先,我们要搞清楚事件和动作的概念。在客户端脚本中,javascript 通过对事件进行响应来获得与用户的交互。例如,当用户单击…

    2025年12月23日
    000

发表回复

登录后才能评论
关注微信