如何实现圣杯布局
利用flex布局实现
利用flex弹性布局的特点,左右俩边固定宽度,剩余的全给中间。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
.header {
height: 80px;
background-color: #ccc;
}
.box {
height: 300px;
background-color: blueviolet;
display: flex;
}
.boxleft {
height: 300px;
width: 80px;
background-color: darkblue;
}
.boxmiddle {
height: 300px;
flex: 1;
background-color: darkred;
}
.boxright {
height: 300px;
width: 80px;
background-color: darkgreen;
}
.header {
height: 80px;
background-color: #ccc;
}
</style>
</head>
<body>
<div class="header"></div>
<div class="box">
<div class="boxleft"></div>
<div class="boxmiddle"></div>
<div class="boxright"></div>
</div>
<div class="footer"></div>
</body>
</html>
利用浮动实现
利用浮动将左盒子左浮动,右盒子右浮动实现布局
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
.header {
height: 80px;
background-color: #ccc;
}
.box {
height: 300px;
background-color: blueviolet;
}
.boxleft {
height: 300px;
width: 80px;
background-color: darkblue;
float: left;
}
.boxmiddle {
height: 300px;
flex: 1;
background-color: darkred;
}
.boxright {
height: 300px;
width: 80px;
background-color: darkgreen;
float: right;
}
.header {
height: 80px;
background-color: #ccc;
}
</style>
</head>
<body>
<div class="header"></div>
<div class="box">
<div class="boxleft"></div>
<div class="boxright"></div>
<div class="boxmiddle"></div>
</div>
<div class="footer"></div>
</body>
</html>
利用定位
利用子绝父相将左右盒子定位在左右俩边
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
.box {
position: relative;
width: 100%;
height: 300px;
background-color: blueviolet;
padding-left: 200px;
padding-right: 200px;
}
.boxleft {
position: absolute;
left: 0;
top: 0;
height: 300px;
width: 200px;
background-color: darkblue;
}
.boxmiddle {
height: 300px;
flex: 1;
background-color: darkred;
}
.boxright {
position: absolute;
right: 0;
bottom: 0;
height: 300px;
width: 200px;
background-color: darkgreen;
}
</style>
</head>
<body>
<div class="box">
<div class="boxleft"></div>
<div class="boxright"></div>
<div class="boxmiddle"></div>
</div>
</body>
</html>
总结:推荐使用flex布局,布局简单,效率快