網絡通訊方法:(下降請求次數,下降傳輸量)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
不會產生邊距效果