css水平垂直居中(绝对定位居中)

2016/12/22 8:39:45   阅读:1696    发布者:1696

使用绝对定位有个限制就是父集必须设置一个固定的高度。

首先HTML

1 <div id="box"> 
2     <div class="child"></div> 
3 </div>    

CSS

 1 #box { 
 2     position: relative; 
 3     height: 500px; 
 4     background: red; 
 5 } 
 6 .child { 
 7     width: 100px; 
 8     height: 100px; 
 9     background: blue; 
10     margin: auto; 
11     position: absolute; 
12     top: 0; 
13     right: 0; 
14     bottom: 0; 
15     left: 0; 
16 }

示例图: