CSS实现Tab布局的简单实例(必看)

2020-05-02 07:43:52易采站长站整理

 


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)针对布局二:

 


<div class="container">
<ul>
<li class="item active">
<p class="title">1</p>
<p class="content">1</p>
</li>
<li class="item">
<p class="title">2</p>
<p class="content ml1">2</p>
</li>
<li class="item">
<p class="title">3</p>
<p class="content ml2">3</p>
</li>
<li class="item">
<p class="title">4</p>
<p class="content ml3">4</p>
</li>
</ul>
</div>

 


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{