“圣杯” 布局是一种典型的网页布局结构,如下所示:
实现方式有多种,下面一一给出代码实现。
1. Flex布局来实现
代码语言:javascript复制<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
* {
margin: 0;
padding: 0;
}
.header,
.footer {
height: 40px;
width: 100%;
background: red;
}
.container {
display: flex;
}
.middle {
flex: 1;
background: yellow;
}
.left {
width: 200px;
background: pink;
}
.right {
background: aqua;
width: 200px;
}
</style>
</head>
<body>
<div class="header">这里是头部</div>
<div class="container">
<div class="left">左边</div>
<div class="middle">中间部分</div>
<div class="right">右边</div>
</div>
<div class="footer">这里是底部</div>
</body>
</html>
2. Float布局来实现(全部float:left)
代码语言:javascript复制<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<style>
* {
margin: 0;
padding: 0;
}
.header,
.footer {
height: 40px;
width: 100%;
background: red;
}
.footer {
clear: both;
}
.container {
padding-left: 200px;
padding-right: 200px;
}
.container div {
position: relative;
float: left;
}
.left {
width: 200px;
background: pink;
margin-left: -100%;
left: -200px;
}
.middle {
width: 100%;
background: yellow;
}
.right {
width: 200px;
background: aqua;
margin-left: -200px;
left: 200px;
}
</style>
</head>
<body>
<div class="header">这里是头部</div>
<div class="container">
<div class="middle">中间部分</div>
<div class="left">左边</div>
<div class="right">右边</div>
</div>
<div class="footer">这里是底部</div>
</body>
</html>
3. Float布局来实现(左边 float: left, 右边 float: right)
代码语言:javascript复制<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<style>
* {
margin: 0;
padding: 0;
}
.header,
.footer {
height: 40px;
width: 100%;
background: red;
}
.container {
overflow: hidden;
}
.middle {
background: yellow;
}
.left {
float: left;
width: 200px;
background: pink;
}
.right {
float: right;
width: 200px;
background: aqua;
}
</style>
</head>
<body>
<div class="header">这里是头部</div>
<div class="container">
<div class="left">左边</div>
<div class="right">右边</div>
<div class="middle">中间部分</div>
</div>
<div class="footer">这里是底部</div>
</body>
</html>
4. 绝对定位来实现
代码语言:javascript复制<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<style>
* {
margin: 0;
padding: 0;
}
.header,
.footer {
height: 40px;
width: 100%;
background: red;
}
.container {
min-height: 22px;
position: relative;
}
.container>div {
position: absolute;
}
.middle {
left: 200px;
right: 200px;
background: yellow;
}
.left {
left: 0;
width: 200px;
background: pink;
}
.right {
right: 0;
width: 200px;
background: aqua;
}
</style>
</head>
<body>
<div class="header">这里是头部</div>
<div class="container">
<div class="left">左边</div>
<div class="right">右边</div>
<div class="middle">中间部分</div>
</div>
<div class="footer">这里是底部</div>
</body>
</html>
5. Grid布局来实现
代码语言:javascript复制<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<style>
body {
display: grid;
text-align: center;
}
.header {
background: red;
grid-row: 1;
grid-column: 1/5;
}
.left {
grid-row: 2;
grid-column: 1/2;
background: orange;
}
.middle {
grid-row: 2;
grid-column: 2/4;
background: rebeccapurple
}
.right {
grid-row: 2;
grid-column: 4/5;
background: cadetblue;
}
.footer {
background: gold;
grid-row: 3;
grid-column: 1/5;
}
</style>
</head>
<body>
<div class="header">这里是头部</div>
<div class="left">左边</div>
<div class="middle">中间部分</div>
<div class="right">右边</div>
<div class="footer">这里是底部</footer>
</div>
</body>
</html>