使一个盒子水平垂直居中以及如何实现双飞翼(圣杯)布局?
一、如何使一个盒子水平垂直居中
方法一:利用定位
html代码片段:
<body>
<div class="parent">
<div class="child">我是子元素</div>
</div>
</body>
css代码片段:
<style>
.parent {
width: 500px;
height: 500px;
border: 1px solid #000;
position: relative;
}
.child {
width: 100px;
height: 100px;
border: 1px solid #999;
position: absolute;
top: 50%;
left: 50%;
margin-top: -50px;
margin-left: -50px;
}
</style>
方法二:利用 margin:auto
html代码片段:
<body>
<div class="parent">
<div class="child">我是子元素</div>
</div>
</body>
css代码片段:
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.parent {
width: 500px;
height: 500px;
border: 1px solid #000;
position: relative;
}
.child {
width: 100px;
height: 100px;
border: 1px solid #999;
position: absolute;
margin: auto;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
</style>
方法三:利用 display:table-cell
html代码片段:
<body>
<div class="parent">
<div class="child">我是子元素</div>
</div>
</body>
css代码片段:
<style>
.parent {
width: 500px;
height: 500px;
border: 1px solid #000;
display: table-cell;
vertical-align: middle;
text-align: center;
}
.child {
width: 100px;
height: 100px;
border: 1px solid #999;
display: inline-block;
}
</style>
方法四:利用 display:flex;设置垂直水平都居中
html代码片段:
<body>
<div class="parent">
<div class="child">我是子元素</div>
</div>
</body>
css代码片段:
<style>
.parent {
width: 500px;
height: 500px;
border: 1px solid #000;
display: flex;
justify-content: center;
align-items: center;
}
.child {
width: 100px;
height: 100px;
border: 1px solid #999;
}
</style>
方法五:计算父盒子与子盒子的空间距离(这跟方法一是一个道理)
html代码片段:
<body>
<div class="parent">
<div class="child">我是子元素</div>
</div>
</body>
css代码片段:
<style>
.parent {
width: 500px;
height: 500px;
border: 1px solid #000;
}
.child {
width: 100px;
height: 100px;
border: 1px solid #999;
margin-top: 200px;
margin-left: 200px;
}
</style>
方法六:利用 transform
html代码片段:
<body>
<div class="parent">
<div class="child">我是子元素</div>
</div>
</body>
css代码片段:
<style>
.parent {
width: 500px;
height: 500px;
border: 1px solid #000;
position: relative;
}
.child {
width: 100px;
height: 100px;
border: 1px solid #999;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
二、如何实现双飞翼(圣杯)布局?
1、利用定位实现两侧固定中间自适应
1、父盒子设置左右 padding 值
2、给左右盒子的 width 设置父盒子的 padding 值,然后分别定位到 padding 处
3、中间盒子自适应
html代码片段:
<div class="father">
<div class="left"></div>
<div class="center"></div>
<div class="right"></div>
</div>
css代码片段:
<style>
.father {
height: 400px;
background-color: pink;
position: relative;
padding: 0 200px;
}
.left,.right {
width: 200px;
height: 300px;
background-color: yellow;
position: absolute;
top: 0;
}
.left {
left: 0;
}
.right {
right: 0;
}
.center {
background-color: blue;
height: 350px;
}
</style>
2、利用 flex 布局实现两侧固定中间自适应
1、父盒子设置 display:flex;
2、左右盒子设置固定宽高
3、中间盒子设置 flex:1
html代码片段:
<div class="father">
<div class="left"></div>
<div class="center"></div>
<div class="right"></div>
</div>
css代码片段:
<style>
.father {
height: 400px;
background-color: pink;
display: flex;
}
.left {
width: 200px;
height: 300px;
background-color: yellow;
}
.right {
width: 200px;
height: 300px;
background-color: yellow;
}
.center {
flex: 1;
background-color: blue;
}
</style>
3、利用 bfc 块级格式化上下文, 实现两侧固定中间自适应
1、左右固定宽高,进行浮动
2、中间 overflow: hidden;
html代码片段:
<!-- 注意:left 和 right 必须放在 center 前面 -->
<div class="father">
<div class="left"></div>
<div class="right"></div>
<div class="center"></div>
</div>
css代码片段:
<style>
.father {
height: 500px;
background-color: pink;
}
.left {
float: left;
width: 200px;
height: 400px;
background-color: blue;
}
.right {
float: right;
width: 200px;
height: 400px;
background-color: blue;
}
.center {
height: 450px;
background-color: green;
overflow: hidden;
}
</style>