CSS中margin和position:relative的定位問題


一、代碼

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        .bb{
            background: burlywood;
            height: 50px;

            margin-left: 100px;
            width: 100%;
        }

        .cc{
            background: burlywood;
            height: 50px;

            margin-left: 100px;
            /*此為默認的,等同於不寫*/
            width: auto;
        }

        .dd{
            background: burlywood;
            height: 50px;

            position: relative;
            left: 100px;
            width: 100%;
        }

        .ee{
            background: burlywood;
            height: 50px;

            position: relative;
            left: 100px;
            /*此為默認的,等同於不寫*/
            width: auto;
        }
    </style>
</head>
<body style="position: relative">
<div class="bb"></div>
<br>
<div class="cc"></div>
<br>
<div class="dd"></div>
<br>
<div class="ee"></div>
</body>
</html>

二、效果圖

最左邊的時候:

最后邊的時候:

三、結論

  1. div不明確設置width,那么默認的為width:auto.
  2. 只有使用 margin-left+auto 定位的時候,才能使div剛好占滿整個屏幕。
  3. 可以理解的在使用 margin-left+100% 的時候,div會向右邊多出margin-left的距離。(因為100%是相對於其父元素而言,即當前的body)

四、問題

無法理解的是使用position:relative的時候,不管width如何設置都不能剛好占滿屏幕,why?

 


免責聲明!

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



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