css 常用的绝对定位元素水平垂直居中的方法

两种方法都能够实现:

1.

div {  
  height:80%; /*一定要设置高度*/ 
 overflow:hidden;/*建议设置*/ margin: auto; position: absolute; top: 0; left: 0; bottom: 0; right: 0; }

2.

div{  
   position: absolute;  
   width: 80%;  
   height: 60%;  
   padding: 2%;   
   top: 50%;
   left: 50%;  
   margin-left: -41%;  /* (width + padding)/2 */  
   margin-top: -31%;  /* (height + padding)/2 */  
}     

  

posted @ 2017-09-25 13:17  lilelile  阅读(850)  评论(1编辑  收藏  举报