(固定定位)設置元素相對瀏覽器可視窗口水平居中,垂直居中


設置元素相對瀏覽器可視窗口水平居中,垂直居中的兩種方法

<div class="box">
   123
</div>

方法一:讓box中心與瀏覽器中心在同一位置,

 

 

 

.box{
    width:200px;
    height:200px;
    background: #f00;
    position: fixed;
    top:50%;
    left:50%;
    margin:-100px 0 0 -100px
   }

方法二:(五馬分屍法):各方向力度相同

 

 

 

.box{
    width:200px;
    height:200px;
    background: #f00;
    position: fixed;
    left:0;
    right:0;
    bottom:0;
    top:0;
    margin:auto;
   } 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM