用纯CSS如何制作流行的TAB菜单?

html-css04

用纯CSS如何制作流行的TAB菜单?,第1张

Tab菜单比较简单的方法是直接使用dreamweaver cs3的spry制作,其他方法制作难度比较大,不管哪种方法,必须要使用javascript调用。

<ul class="tabs">

<li>

<input type="radio" name="tabs" id="tab1" checked />

<label for="tab1">选项卡 1</label>

<div id="tab-content1" class="tab-content">

<p>选项卡内容 1</p>

</div>

</li>

<li>

<input type="radio" name="tabs" id="tab2" />

<label for="tab2">选项卡 2</label>

<div id="tab-content2" class="tab-content">

<p>选项卡内容 2</p>

</div>

</li>

</ul>

授之于鱼 不如.....呵呵 上面的给你了代码 我来给你说下思路吧。这里最重要的是 css里的 display:none 这个的使用,当鼠标点中一个选项卡时,将自身的css中的display:none去掉 让其显示出来,让其他的选项卡中的css中添加display:none, 隐藏其他的选项卡。其实就是这个简单 ,具体的实现方法就是用js实现的。实现的方法有多种,上面给你的只是其中的一种。你可以去网上多搜些这方面的源代码,多多学习,希望你能编写出属于自己的代码~

纯CSS写的选项卡效果代码,没有使用js,供学习参考。现在很多都是使用jQuery的,动态效果非常好。站长之家、懒人之家有好多样例可供选择下载。

代码说明

这里主要使用表单的单选按钮来实现这个TAB显示和隐藏,首页tab里的内容默认隐藏,如果单选按钮为选中状态(checked)就显示内容。具体请看下面代码。

关于兼容性,因为是用CSS3来制作的,所以如果不支持CSS3的浏览将会出现不兼容的情况。

HTML代码

<ul class="tabs">

<li>

<input type="radio" name="tabs" id="tab1" checked />

<label for="tab1">选项卡 1</label>

<div id="tab-content1" class="tab-content">

<p>选项卡内容 1</p>

</div>

</li>

<li>

<input type="radio" name="tabs" id="tab2" />

<label for="tab2">选项卡 2</label>

<div id="tab-content2" class="tab-content">

<p>选项卡内容 2</p>

</div>

</li>

</ul>

CSS代码

* {

margin: 0

padding: 0

-webkit-box-sizing: border-box

-moz-box-sizing: border-box

box-sizing: border-box

}

body {

padding: 20px

text-align: left

font-family: Lato

color: #fff

background: #9b59b6

}

.tabs {

width: 650px

float: none

list-style: none

position: relative

margin: 80px 0 0 10px

text-align: left

}

.tabs li {

float: left

display: block

}

.tabs input[type="radio"] {

position: absolute

top: -9999px

left: -9999px

}

.tabs label {

display: block

padding: 14px 21px

border-radius: 2px 2px 0 0

font-size: 20px

font-weight: normal

text-transform: uppercase

background: #8e44ad

cursor: pointer

position: relative

top: 4px

-webkit-transition: all 0.2s ease-in-out

-moz-transition: all 0.2s ease-in-out

-o-transition: all 0.2s ease-in-out

transition: all 0.2s ease-in-out

}

.tabs label:hover {

background: #703688

}

.tabs .tab-content {

z-index: 2

display: none

overflow: hidden

width: 100%

font-size: 17px

line-height: 25px

padding: 25px

position: absolute

top: 53px

left: 0

background: #612e76

}

.tabs [id^="tab"]:checked + label {

top: 0

padding-top: 17px

background: #612e76

}

.tabs [id^="tab"]:checked ~ [id^="tab-content"] {

display: block

}