css让div水平垂直居中
示例1:
.div1{ width:200px; height:300px; border:1px solid #000; position: relative; } .div2{ width: 40px; height:40px; background:green; position: absolute; margin: auto; top:0; left:0; right:0; bottom:0; }
<div class="div1"> <div class="div2">居中</div> </div>
示例2:
.div3{ width:200px; height:300px; border:1px solid #000; display: table-cell; vertical-align: middle; } .div4{ width: 40px; margin: 0 auto; }
<div class="div3">
<div class="div4">居中</div>
</div>