网络通讯方法:(下降请求次数,下降传输量)css
代码层: (节省内存)canvas
margin: 0 auto
<div class= "parent">
<div class= "child"></div>
</div>
<style>
.parent {
position: relative;
}
.child {
position: absolute;
left: 50%;
transform: translateX(-50%);
}
</style>
复制代码
justify-content: center
margin: 0 auto
justify-content: center
实现水平居中margin: 0 auto
实现<div class= "parent">
<div class= "child"></div>
</div>
<style>
.parent {
position: relative;
}
.child {
position: absolute;
margin: 0 auto; /*水平居中*/
left: 0; /*不能省略,且为0*/
right: 0; /*不能省略,且为0*/
}
</style>
复制代码
line-height
flex-direction: column
,在使用justify-content: center
margin-top
向上偏移元素高度的一半(已知高度)<div class= "parent">
<div class= "child">固定高度的块级元素垂直居中</div>
</div>
<style>
.parent {
position: relative;
}
.child {
position: absolute;
top: 50%;
height: 100px;
margin-top: -50%;
}
</style>
复制代码
<div class= "parent">
<div class= "child">未知高度的块级元素垂直居中</div>
</div>
<style>
.parent {
position: relative;
}
.child {
position: absolute;
top: 50%;
transform: translateY(-50%);
}
</style>
复制代码
align-items: center
属性,使子元素垂直居中<div class= "parent">
<div class= "child" style="width: 100px; height= 100px;"></div>
</div>
<style>
.parent {
position: relative;
}
.child {
position: absolute;
top: 50%;
left: 50%;
margin: -50px 0 0 -50px;
}
</style>
复制代码
<div class= "parent">
<div class= "child" style="width: 100px; height= 100px;"></div>
</div>
<style>
.parent {
position: relative;
height: 200px; /*父容器必须有高度*/
}
.child {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
}
</style>
复制代码
.parent {
position: relative;
height: 200px; /*父容器必须有高度*/
}
.child {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
复制代码
.parent {
height: 100px;
display: flex;
justify-content: center;
align-items: center;
}
复制代码
margin: auto
便可(最简单的,也不兼容低版本IE).parent {
height: 100px;
display: flex;
}
.child {
margin: auto;
}
复制代码
padding-left,padding-right,margin-left,margin-right
都会产生边距效果,垂直方向的padding-top,padding-bottom,magin-top,margin-bottom
不会产生边距效果