1.1.1. 三栏布局的方案
- 浮动
- 绝对定位
- flex布局
- 百分比布局
- calc 布局
- 网格布局(grid布局)
<!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>
</head>
<style>
* {
margin: 0px;
padding: 0px;
}
.left {
background-color: aqua;
}
.main {
background-color: brown;
}
.right {
background-color: aquamarine;
}
.container {
display: grid;
grid-template-columns: 100px auto 200px;
grid-template-rows: 700px;
}
</style>
<body>
<div class="container">
<div class="left">Left</div>
<div class="main">Main</div>
<div class="right">Right</div>
</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>
</head>
<style>
* {
margin: 0px;
padding: 0px;
height: 700px;
}
.left {
width: 100px;
background-color: aqua;
display: table-cell;
}
.main {
background-color: brown;
display: table-cell;
}
.right {
width: 200px;
background-color: aquamarine;
display: table-cell;
}
.container {
display: table;
width: 100%;
}
</style>
<body>
<div class="container">
<div class="left">Left</div>
<div class="main">Main</div>
<div class="right">Right</div>
</div>
</body>
</html>
1.1.2. 垂直居中的方法
- 方案一:verticle-align: middle;
.child {
display: inline-block;
verticle-align: middle;
}
- 方案二:line-height 与 height 相等;
.child {
height:100px;
line-height: 100px;
}
.parent {
display: flex;
align-items: center;
}
- 方案四:transform: translateY(-50%);
.parent {
position: relative;
}
.child {
display: absolute;
top: 50%;
transform: translateY(-50%);
}
.parent {
display: table;
}
.child {
display: table-cell;
}