html 元素定位position-relative, absolute, fixed, static

來源:互聯網
上載者:User

標籤:前端   can   div   flow   com   pos   abs   end   off   

看到這個,你有什麼想法? 

Difference between static and relative positioning

 

如果你能完全看明白,那幾本上css 元素定位的東西基本都會了。本文也不用往下看了。

自己接觸web這麼多年,雖說前端有接觸過,但還是沒有系統地學習過,和完整的實踐過。很多東西是似懂非懂。其實這裡就是問題的關鍵所在。

似懂非懂,其實本質上是不懂的。但是卻又自以為懂, 這就是自己給自己設定了個障礙,這個障礙看似無形卻比有形的障礙更可怕。因為每個人似乎

總是自以為是的認為自己認定的東西就是對的。因此很難,放空或者說歸零, 來從頭開始系統完整的學習一個新的東西。造成對新事物的理解總是停留

在陳舊的水平,故步自封。否定也許是進步的開始!!

------------------------------------------------------------------------------------------------

Static positioning is the default positioning model for elements. They are displayed in the page where they rendered as part of normal HTML flow. Statically positioned elements don‘t obey left, top, right and bottom rules:

Relative positioning allows you to specify a specific offset (left, top etc) which is relative to the element‘s normal position in HTML flow. So if I have a textbox inside a div I could apply relative positioning on the textbox to have it display at specific place relative to where it would normally be placed within the div:

There is also absolute positioning - whereby you specify the exact location of the element relative to the entire document, or the next relatively positioned element further up the element tree:

And when a position: relative is applied to a parent element in the hierarchy:

Note how our absolutely-position element is bound by the relatively-positioned element.

And lastly there is fixed. Fixed positioning restricts an element to a specific position in the viewport, which stays in place during scroll:

You may also observe the behaviour that fixed-positioned elements do not cause scroll because they are not considered to be bound by the viewport:

Whereas absolutely-positioned elements are still bound by the viewport and will cause scrolling:

..unless of course your parent element uses overflow: ? to determine the behaviour of the scroll (if any).

With absolute positioning and fixed positioning, the elements are taken out of HTML flow.

html 元素定位position-relative, absolute, fixed, static

聯繫我們

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