圣杯布局的五种方式
Posted 饮尽杯中月
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了圣杯布局的五种方式相关的知识,希望对你有一定的参考价值。
方法一
center还在文本流之中,会影响到后面的元素,不影响前面的元素
<!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">
<title>Document</title>
<style>
.left,.right
width: 100px;
height: 100px;
.left
background-color: green;
float: left;
.right
background-color: red;
float: right;
.center
background-color: blue;
width: 100%;
height: 100px;
</style>
</head>
<body>
<!-- 圣杯布局的五种方式 -->
<div class="left"></div>
<div class="right"></div>
<div class="center">
这是内容
</div>
</body>
</html>
方法二:绝对定位
<!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">
<title>Document</title>
<style>
.left,.right
width: 100px;
height: 100px;
div
position: absolute;
.left
background-color: green;
left:0;
.right
background-color: red;
right:0;
.center
background-color: blue;
height: 100px;
right:100px;
left:100px;
</style>
</head>
<body>
<!-- 圣杯布局的五种方式 -->
<div class="left"></div>
<div class="center">
这是内容
</div>
<div class="right"></div>
</body>
</html>
方式三:伸缩布局(flex)
<!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">
<title>Document</title>
<style>
.left,.right
width: 100px;
height: 100px;
div
position: absolute;
.left
background-color: green;
left:0;
.right
background-color: red;
right:0;
.center
background-color: blue;
height: 100px;
right:100px;
left:100px;
</style>
</head>
<body>
<!-- 圣杯布局的五种方式 -->
<div class="left"></div>
<div class="center">
这是内容
</div>
<div class="right"></div>
</body>
</html>
方法四:table布局
<!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">
<title>Document</title>
<style>
.left,.right
width: 100px;
height: 100px;
.box
display: table;
width: 100%;
.box div
display: table-cell;
.left
background-color: green;
.right
background-color: red;
.center
background-color: blue;
</style>
</head>
<body>
<!-- 圣杯布局的五种方式 -->
<div class="box">
<div class="left"></div>
<div class="center">
这是内容
</div>
<div class="right"></div>
</div>
</body>
</html>
方法五:网格布局(grid)
<!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">
<title>Document</title>
<style>
.box
display: grid;
grid-template-columns: 100px auto 100px;
grid-template-rows: 100px;
.left
background-color: green;
.right
background-color: red;
.center
background-color: blue;
</style>
</head>
<body>
<!-- 圣杯布局的五种方式 -->
<div class="box">
<div class="left"></div>
<div class="center">
这是内容
</div>
<div class="right"></div>
</div>
</body>
</html>
以上是关于圣杯布局的五种方式的主要内容,如果未能解决你的问题,请参考以下文章