css常用布局之三列布局学习
圣杯布局
使用position实现
- 步骤
- 首先main,left,rigth放在main中,center放在最前面
<div class="main">
<div class="center">中间</div>
<div class="left">左</div>
<div class="right">右</div>
</div>
- main设置
width:100%
,定位设置成绝对定位relative
,设置两边的padding
- center设置
width:100%
- left设置宽度为第二步所设置的
padding
,定位设置为相对定位absolute
,设置left,top:0
- rigth设置宽度为第二步所设置的
padding
,定位设置为相对定位absolute
,设置rigth:left.width+rigth.width
,top:0
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
*{
margin: 0;
padding: 0;
color: black;
font-size: 45px
}
.main div{
box-sizing: border-box;
}
.main{
width: 100%;
background: red;
position: relative;
padding-left: 200px;
padding-right: 200px;
}
.center{
width: 100%;
background: pink
}
.left{
background: yellow ;
width: 200px;
position: absolute;
left: 0;
top: 0;
}
.right{
background: blue ;
width: 200px;
position: absolute;
top: 0;
right:400px;
}
</style>
</head>
<body>
<div class="main">
<div class="center">中间</div>
<div class="left">左</div>
<div class="right">右</div>
</div>
</