div塊水平居中,垂直居中


水平居中
一個div想要水平居中於它的父div中只需要給它加css屬性margin:0 auto; 即可

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<style>
#box{
    width: 200px;
    height: 200px;
    border:2px solid #000;
    line-height: 200px;
}
#one{
    background: #000;
    width:50px;
    height: 50px;
    margin:0 auto; 
}
   
</style>
<body>
    <div id="box">
          <div id="one"></div>
    </div>
</body>
</html>

垂直居中
一個div想要垂直居中於它的父div中,需要給它添加css屬性display: inline-block;再給它的父div添加css屬性vertical-align: middle;即可

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<style>
#box{
    width: 200px;
    height: 200px;
    border:2px solid #000;
    line-height: 200px;
    vertical-align: middle;
}
#one{
    background: #000;
    width:50px;
    height: 50px;
    display: inline-block;
}    
#two{
    background: green;
    width:50px;
    height: 50px;
    display: inline-block;
}    
   
</style>
<body>
    <div id="box">
          <div id="one"></div>
          <div id="two"></div>
    </div>
</body>
</html>

原文網頁鏈接:https://blog.csdn.net/weixin_44535476/article/details/103455164


免責聲明!

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



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