A block element with position: relative;
is often used as containing element for absolutely positioned elements. When I don't have such a block element I usually change a static one into a relative one. Does this change anything else except the element being able to act as
container for absolutely positioned elements?
Is there a difference between a block with
position: static;
and
position: relative;
left: 0;
top: 0;
except the one mentioned above?
Static: By default the position of elements is static. If you add property such as top, bottom, right, or left nothing will be implemented. Relative: The change in position will be relevant to that div's original place. Fixed: The fixed property would stay at the same place even after we scroll the page.
The main difference between relative and absolute positioning is that position: absolute will take a child element completely out of the normal flow of the document. And that child will be positioned in relation to the first parent element that has its own position set.
Static positioned elements are not affected by the top, bottom, left, and right properties.
Fixed: The position of the element will be positioned relative to the viewport. Static: The elements will be positioned according to the normal flow of the page. Relative: The element remains in the normal flow of the document but left, right, top, and bottom affects.
Relative
element can make use of z-index
while static
can't.
Top
,right
,bottom
,left
have no effect on static
positioned element.
IE7 needs relative
position for the element to make use of overflow
. Past stack-overflow question regarding this issue. Also, post from Jonathan Snook about it.
One difference that I can think of is that
an element with position:relative
will respect the z-index
property.
Demo
Edit:
As already mentioned, the offset properties top,left,bottom,right
only apply to positioned elements (and not static ones)
Here is one (unintuitive) application of using this offset in relatively positioned elements - which also may influence you when deciding to set position:relative
on an element.
Note: this will not be expressed when offset is 0 (ie top:0;left:0
) as mentioned in the question, but is an important thing to realise when using position:relative
.
From the spec:
Offsetting a box (B1) in this way has no effect on the box (B2) that follows: B2 is given a position as if B1 were not offset and B2 is not re-positioned after B1's offset is applied. This implies that relative positioning may cause boxes to overlap.
Take a look at this demo
You can see that when I placed a margin on the static element - it effected the following p
element, however when I used positioning on the relative element - the following p
element stays put.
Positioned elements with calculated z-indices other than "auto" generate stacking contexts (w3 spec). A less clinical definition of stacking contexts' behaviour is available on MDN.
The elements themselves appear in a different painting order (compare #3 with others).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With