如何讓div處於body居中的狀態


<!DOCTYPE html>
<html lang="en" >
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!--<link rel="stylesheet" href="css/style.css" type="text/css">-->

</head>
<body>
<!--在body里添加“style="text-align: center;"這一句沒有效果真正起作用的是下面的margin:auto;這一句”-->
<div style="margin-left:auto;margin-right:auto;width:50%;height:30px;background-color: aqua; padding:5%;">
    <div style="width:50%;background-color: bisque">
        <div style="width:50%;background-color: blueviolet"></div>
    </div>
</div>
</body>
</html>

 

瀏覽器默認從左往右渲染元素,在沒有超出父容器的寬度的前提下  如果子容器的寬度能夠被容納  設置margin-right是沒有用的

(這句話來自https://blog.csdn.net/zhangshab/article/details/80344704

如果覺得有幫助的話也請支持下原作者。


免責聲明!

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



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