CSS定位屬性position

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   strong   2014   div   

CSS定位元素有3種方式 :普通流、相對位置、絕對位置。通過設定position屬性來實現。

position屬性取值的含義
inherit

繼承父元素position 屬性的值。

static

預設值。沒有定位,元素出現在正常的流中(忽略 top, bottom, left, right 或者 z-index 聲明)。

relative

產生相對定位的元素,相對於元素本身正常位置進行定位。因此,"left:20" 會向元素的 LEFT 位置添加 20 像素。

absolute

產生絕對位置的元素,找到第一個非static的祖先元素進行定位。元素的位置通過 "left", "top", "right" 以及 "bottom" 屬性進行規定。

fixed

產生絕對位置的元素,相對於瀏覽器視窗進行定位。元素的位置通過 "left", "top", "right" 以及 "bottom" 屬性進行規定。

 

 

 

 

 

 

 

 

 

普通流定位是:static,預設定位  相對位置定位是:relative 相對於該元素本身作為普通流的位置,佔有文檔流的原來位置

絕對位置定位有2種:absolute和fixed,一個是相對自己的第一個非static祖先元素,一個是相對瀏覽器視窗,都不佔有原來的文檔流位置

 

例子1:static普通流定位,紅色div的top屬性失效
<
body> <div style="border: solid 1px #0e0; width: 200px;"> <div style="height: 100px; width:100px; background-color:red; top:20px;"></div> <div style="height:100px; width:100px; background-color:green; "></div> <div style="height:100px; width:100px; background-color:blue;"></div> </div></body>

 

例子2:relative相對位置,綠色div相對static的位置向右和向下移動了20px,綠色div原來的文檔位置還在。
<
body> <div style="border: solid 1px #0e0; width: 200px;"> <div style="height: 100px; width:100px; background-color:red; top:20px;"></div> <div style="height:100px; width:100px; background-color:green; position:relative; top:20px; left:20px;"></div> <div style="height:100px; width:100px; background-color:blue;"></div> </div></body>                                                                                                               

 

例子3.1:absolute絕對位置(不佔有原來的文檔流位置),沒有指定父元素div的position屬性,則綠色div的父級div是static定位,所以不選這個div做參照,選window作為參照
<
body> <div style="border: solid 1px #0e0; width: 200px;"> <div style="height: 100px; width:100px; background-color:red; top:20px;"></div> <div style="height:100px; width:100px; background-color:green; position:absolute; top:20px; left:20px; "></div> <div style="height:100px; width:100px; background-color:blue;"></div> </div></body>

 

例子3.2:absolute絕對位置,父級div的position是relative,不再是static,所以選父級div為參考。
<
body> <div style="border: solid 1px #0e0; width: 200px; position:relative;"> <div style="height: 100px; width:100px; background-color:red; top:20px;"></div> <div style="height:100px; width:100px; background-color:green; position:absolute; top:20px; left:20px; "></div> <div style="height:100px; width:100px; background-color:blue;"></div> </div></body>

 

例子4:fixed絕對位置,相對於視窗window的,不佔文檔流位置。
<
body> <div style="border: solid 1px #0e0; width: 200px; position:relative;"> <div style="height: 100px; width:100px; background-color:red; top:20px;"></div> <div style="height:100px; width:100px; background-color:green; position:fixed; bottom:20px; left:20px; "></div> <div style="height:100px; width:100px; background-color:blue;"></div> </div></body>

 

CSS定位屬性position

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.