關於定位布局中的相對定位

來源:互聯網
上載者:User
關於CSS中定位布局的相對定位-relative

相對定位一個最大特點是:自己通過定位跑開了還佔用著原來的位置,不會讓給他周圍的諸如文字資料流之類的對象。相對定位也比較獨立,做什麼事它自己說了算,要定位的時候,它是以自己本身所在位置位移的(相對對象本身位移)。

相對定位常與絕對位置結合用,一般是給父級設定相對定位方式,子級元素就可以相對它進行方便的絕對位置了

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Document</title>    <style>    .orange{        width: 400px;        height: 300px;        background-color: orange;    }/*紅色區塊設定為相對布局,雖然往下便宜150PX,但仍佔據了位置,使得黃色,綠色無法浮動到最左邊*/    .red{        width: 100px;        height: 100px;        background-color: red;        float: left;        position:relative;        margin-top: 150px;    }    .yellow{        width: 100px;        height: 100px;        background-color: yellow;        float: left;    }        .green{        width: 100px;        height: 100px;        background-color: green;        float: left;    }/*藍色區塊相對自身偏離了30px,10px,但黑色區塊的位置是以藍色之前的位置向下位移10px*/    .blue{        width:50px;        height: 50px;        background-color: blue;        position: relative;        top:30px;        left:10px;    }    .black{        width:30px;        height: 30px;        background-color: black;        margin-top: 10px;    }    </style></head><body>    <p class="orange">        <p class = "red">            <p class = "blue"></p>            <p class = "black"></p>        </p>        <p class = "yellow"></p>        <p class = "green"></p>    </p></body></html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.