Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a difference between a static element and a relative element with left and top set to zero?

Tags:

css

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?

like image 530
bbuser Avatar asked Jul 07 '14 07:07

bbuser


People also ask

What is difference between static and relative?

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.

What's the difference between a relative fixed absolute and static positioned element?

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.

Which positioned elements are not affected by the top bottom left and right properties?

Static positioned elements are not affected by the top, bottom, left, and right properties.

What is the difference between position static and position fixed?

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.


3 Answers

  1. Relative element can make use of z-index while static can't.

  2. Top,right,bottom,left have no effect on static positioned element.

  3. 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.

like image 64
Matija Mrkaic Avatar answered Nov 09 '22 23:11

Matija Mrkaic


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.

like image 37
Danield Avatar answered Nov 09 '22 22:11

Danield


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).

like image 30
Oleg Avatar answered Nov 09 '22 23:11

Oleg