方法1:table-cellcss
html結構:html
<div class=「box1」> <span>水平垂直居中</span> </div>
css代碼:flex
.box1{ display: table-cell; vertical-align: middle; text-align: center; }
方法2:display:flexspa
html結構:code
<div class="box2"> <div>水平垂直居中</div> </div>
css代碼:orm
.box2{ display: flex; justify-content:center; align-items:center; }
方法3:絕對定位和0htm
.box3{position:relative} .box3 div{ position:absolute; top:0; left:0; right:0;bottom:0; margin:auto; }
方法4:絕對定位和負邊距it
.box4{position:relative} .box4 div{ width:100px; height:50px; position:absolute; top:50%; left:50%; margin:-25px 0 0 -50px; text-align:center; }
方法5:絕對定位和translateio
.box5{position:relative} .box5 div{ position:absolute; top:50%; left:50%; transform:translate(-50%,-50%); text-align:center; }
方法6:display:flex和margin:autotable
.box6{ display: flex; text-align: center; } .box6 div{ margin: auto; }