This property applies to all elements. It has five possible values:
staticrelativeabsolutefixedinheritFrom section 9 Visual formatting model:
The box is a normal box, laid out according to the normal flow. The
top,
right,
bottom,
andleft
properties do not apply.
top does not apply because in a ‘static’ context, the computed value of box offsets is always auto.
position property of an element has the value of static, that element is not said to be positioned.static is the initial value (the default value), there is no need to include such styling in a declaration block unless it is to overwrite a different value.From section 9 Visual formatting model:
The box’s position is calculated according to the normal flow (this is called the position in normal flow). Then the box is offset relative to its normal position. When a box B is relatively positioned, the position of the following box is calculated as though B were not offset.
direction of the containing block is ltr, the value of ‘left’ wins and ‘right’ becomes -’left’. If direction of the containing block is rtl, ‘right’ wins and ‘left’ is ignored. Authors could take advantage of how things work by setting equal value to opposite properties.top, right, bottom, and left properties cannot stretch nor shrink the box (they cannot change its size).
From section 9 Visual formatting model:
The box’s position (and possibly size) is specified with the
top,right,bottom, andleftproperties. These properties specify offsets with respect to the box’s containing block. A absolutely positioned box is removed from the normal flow entirely (it has no impact on later siblings) and assigned a position with respect to a containing block. Also, though absolutely positioned boxes have margins, they do not collapse with any other margins.
top:0;left:0; for example, that box would be at the top left corner of the viewport.display:none. The box has been removed from the flow.display is block.float is none.body).top, right, bottom, and left property values. For examples, zeroing out all properties will make the box stretch to match the dimensions of the padding box of its containing block. See zeroing out all box offsets (note: ie6 does not stretch the box).fixed instead of absolute or style body with position:relative as the initial positioning block is the viewport (styling html would not work in IE). As this overlay demo shows.position:absolute triggers haslayout.Because this positioning scheme removes boxes from the flow, it is considered bad pratice to use it for layout.
From section 9 Visual formatting model:
Fixed positioning is a subcategory of absolute positioning. The only difference is that for a fixed positioned box, the containing block is established by the viewport. For continuous media, fixed boxes do not move when the document is scrolled. In this respect, they are similar to fixed background images. For paged media (where the content of the document is split into one or more discrete pages), boxes with fixed positions are repeated on every page. This is useful for placing, for instance, a signature at the bottom of each page. Boxes with fixed position that are larger than the page area are clipped. Parts of the fixed position box that are not visible in the initial containing block will not print.
Things to notice in this demo:
Things to remember:
@media print {
#logo {position: static;}
}
position:absolute, position:fixed will trigger haslayout in IE.If position:inherit is specified for a given box, then it will take the same computed value as the property for the box’s parent.
Note that IE 6 and 7 do not support this keyword except when used with direction and visibility (see the CSS Property Value inherit).
Be aware that for absolutely and fixed positioned boxes, values set in percentage for top, right, bottom, and left are computed according to the dimensions of the containing block (which may not be the parent box).
A box styled with overflow:hidden will clip relatively positioned elements (nested boxes), but it will not always hide absolutely positioned ones. This is because the parent box is not always the containing block (the nearest ancestor with a ‘position’ of ‘absolute’, ‘relative’ or ‘fixed’).
In short, this means absolutely positioned elements will show outside of a box styled with overflow:hidden unless their containing block is the box itself or an element inside the said box. This demo page shows how things work.
Authors can use margins on elements regardless of their positioning scheme.
In IE, ‘positioning’ a box may be a blessing or a curse:
position:relative (with haslayout) can be used to prevent a box styled with negative margins from being clipped by a parent container (see how positioning the box fixes this issue).Read PPK’s article, the [sixth] position value, to find out why mobile browser vendors cannot really support position:fixed.
A "ghost" analogy by
DrLangbhani:
An element with position relative is always offset relative to it’s normal position in the flow. In other words, it is shifted relative to where it would be under normal circumstances, and shifting it doesn’t affect the layout of elements around it. It’s like a ghost that has left its body behind. A body that has width and height and affects its surroundings but is invisible. The ghostly box is able to move but is still connected to the old body in that its position is still measured from it. Now an element with position absolute is even easier. It no longer affects its surroundings at all (it’s pulled out of the layout flow). It’s now a true ghost with no body left behind. As far as its sibling elements are concerned it’s as if it no longer exists. To get its position, look through each of its parent elements until you find one with either position: relative, [position: fixed,] or position: absolute. That element will serve as the reference point. Only if you don’t find a "positioned" element will it be offset from the document.
December 14, 2010 at 1:42 pm
Nice post!
Understanding the difference between absolute and relative positioning seems to be a source of a lot of confusion for folks who haven’t used it much in practice. The ghost analogy helps. I have my own analogy is that position:relative leaves a “footprint” behind, whereas position:absolute doesn’t.
Incidentally this is also a good interview question.. just in case you have to interview folks as part of your job.
December 14, 2010 at 10:16 pm
Hi David,
You’re right. There is a lot of confusion when it comes to the position property. I believe the most frequent mistake people make is to believe containing blocks play a role on the positioning of nested relatively positioned elements.
Thanks for your feedback!
December 15, 2010 at 10:17 pm
Hello!
I’ve found several spelling errors.
In the “position:absolute” section,the red box,the third line,it’s “An absolute position box”.
In the last “Things to consider” section,the “the case of IE”,the first term,the second line,it’s “margin”,not “nargin”.
Yours
December 17, 2010 at 9:54 am
I was surprised not to see any discussion of how the position property works on mobile devices. See this post at Quirksmode:
http://www.quirksmode.org/blog/archives/2010/12/the_fifth_posit.html
December 18, 2010 at 10:11 am
@tycable
Thanks a lot for reporting these typos.
@Peter
Good point, I should have mentioned that ‘fixed’ is broken when it comes to mobile devices (as I do for IE6). I’ll add that and include PPK’s link as well.
As a side note, that should be the sixth position value.
Thanks for your feedback
December 26, 2010 at 6:38 pm
Thoroughly enjoyed your comments to the A List Apart CSS Positioning 101 article.
Also glad I read this article first.