1. 程式人生 > >新get到的一個水平垂直居中的方法

新get到的一個水平垂直居中的方法


參考地址:http://www.zhangxinxu.com/study/201209/vw-vh-css-custom-dialog.html



平常方法:

.demo{
    position: absolute;
    top:50%;
    left:50%;
    transform: translate(-50%,-50%);
}
 
  
 
  
 
  
 
  

新get到的方法:

html:

<div class="container">
    <div class="box">
        <
img src="duobao/images/5.png" alt=""> </div> </div>

css:

.container{
    width:100%;
    height:100vh;
    background:#cccccc;
    z-index:10;
    text-align:center;
}
.container::after{
    display: inline-block;
    content:'';
    width:0;
    height:100%;
    vertical-align
: middle; } .box{ display: inline-block; border:1px solid red; }
 
   
 
   
相容性很好,雖然現在已經很少考慮相容了。 不過學了一個方法也是美滋滋。