Position Relative vs Absolute?

前端 未结 10 1603
忘了有多久
忘了有多久 2020-11-22 08:16

What is the difference between position: relative and position: absolute in CSS? And when should you use them?

10条回答
  •  耶瑟儿~
    2020-11-22 08:46

    Another thing to note is that if you want a absolute element to be confined to a parent element then you need to set the parent element's position to relative. That will keep the child element contained within the parent element and it won't be "relative" to the entire window.

    I wrote a blog post that gives a simple example that creates the following affect:

    enter image description here

    That has a green div that is absolutely positioned to the bottom of the parent yellow div.

    1 http://blog.troygrosfield.com/2013/02/11/working-with-css-positions-creating-a-simple-progress-bar/

提交回复
热议问题