絕對位置 position:absolute,相對定位 position:relative,absoluterelative

來源:互聯網
上載者:User

絕對位置 position:absolute,相對定位 position:relative,absoluterelative

《1》

當我們要使用絕對位置的時候,必須要有兩個條件
1》必須給父元素加定位屬性,一般建議使用 position:relative(即:給父元素設為相對定位);
2》給子項目,加絕對位置position:absolute(給子項目設定為絕對位置); 同時加方向屬性(top ,left,rigth,bottom)

絕對位置是以父元素為基準點,進行定位(如果他沒有父元素,或者它的父元素沒有設定position:relative屬性)它就會以<body>為基準點進行定位。絕對位置會脫離文檔流(即:他浮動起來了,不再佔據原來的位置了)

絕對位置

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    <title></title>    <style type="text/css">        body {            margin:0px;            padding:0px;        }        div {            width: 100px;            height: 100px;        }        #main {        width:500px;        height:500px;        border:1px solid red;        margin:0px 250px;                }        #a {            background-color: red;        }        #b {            background-color: green;            top:100px;            left:100px        }        #c {            background-color: blue;            height:150px;            width:150px;        }    </style></head><body>    <div id="main">        <div id="a">A</div>        <div id="b">B</div>        <div id="c">C</div>    </div></body></html>


《2 給子項目添加絕對位置,不給父元素添加相對定位,那麼子項目進行絕對位置就是以body為基準進行定位的》

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    <title></title>    <style type="text/css">        body {            margin:0px;            padding:0px;        }        div {            width: 100px;            height: 100px;        }        #main {        width:500px;        height:500px;        border:1px solid red;        margin:0px 250px;        /*position:relative;*/ /*不給父元素設定相對定位,那麼子項目就會以body為基準進行定位*/        }        #a {            background-color: red;        }        #b {            background-color: green;            top: 100px;            left: 100px ;                        position:absolute; /*給B設定絕對位置*/        }        #c {            background-color: blue;            height:150px;            width:150px;        }    </style></head><body>    <div id="main">        <div id="a">A</div>        <div id="b">B</div>        <div id="c">C</div>    </div></body></html>


《3最終,給父元素添加相對定位,給子項目添加絕對位置,那麼子項目進行絕對位置就是以父元素為基準進行定位的》

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    <title></title>    <style type="text/css">        body {            margin:0px;            padding:0px;        }        div {            width: 100px;            height: 100px;        }        #main {        width:500px;        height:500px;        border:1px solid red;        margin:0px 250px;        position:relative; /*給父元素設定相對定位,那麼子項目進行絕對位置的時候就會以父元素為基準進行定位*/        }        #a {            background-color: red;        }        #b {            background-color: green;            top: 100px;            left: 100px ;                        position:absolute; /*給B設定絕對位置*/        }        #c {            background-color: blue;            height:150px;            width:150px;        }    </style></head><body>    <div id="main">        <div id="a">A</div>        <div id="b">B</div>        <div id="c">C</div>    </div></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.