tags:

views:

238

answers:

2

what is the containing block of an absolutely positioned element? it seems the rule can be a bit complicated...

the spec should be here: http://www.w3.org/TR/CSS21/visudet.html#containing-block-details

i want to verify if the following is true:

for simplicity, assume the containing block is a block element (not inline element)...

1) if the absolute positioned element has a closest ancestor that is positioned "non static" (relative, fixed, or absolute), then that ancestor is the containing block. the absolute positioned element is relative to it.

2) if there is no such ancestor, then the viewport is the containing block, and so the absolute positioned element is relative to the viewport.

no matter what the containing block is above, the width:100% or n% and height:100% or n% are both relative to the containing block.

that's why a

<div style="position:absolute;width:100%;height:100%;background:green"></div>

right under <body> will cover up the whole viewport exactly -- no more, no less.

we could also use position: fixed, except IE 6 doesn't support it... and so the poor programmer need to use position: absolute instead... (well, not a big deal)

Is that an accurate description of an absolute positioned element? If so, i think IE 6 and above, FF, Safari, Chrome all follow this behavior accurately?

+2  A: 

You are correct. The containing block is the last positioned element. so if you want to explicitly set the container then give it position:relative. Most browsers get this right. CSS doesn't really have a 'viewport', I think the top is the HTML element though don't hold me to that. IE prior to 7 had an unnamed element above HTML though.

SpliFF
coz the spec says "If there is no such ancestor, the containing block is the initial containing block." and "the initial containing block. For continuous media, it has the dimensions of the viewport and is anchored at the canvas origin; it is the page area for paged media."
動靜能量
it is a matter of scope. the browser finds the closest parent of your absolute position object and so your object is absolute positioned relatively to that specific parent.
vsync
closest parent witch has position:relative of course. else, it just obeys the window viewport.
vsync
Window viewport is for all intents and purposes the body tag in most (if not all) cases.
ajm
+1  A: 

Summary:

position: relative

Does nothing except set the positioning context for all the elements contained within it. You can then position: absolute any element it contains by setting (typically one or two) values from the possible top, right, bottom or left.

If you give an element with position: relative a top, right, bottom or left value, it will shift position accordingly but leave a blank space where it would be by default. In other words, it remains within the document flow but offset.

position: absolute

To position something absolutely, you need to ask 'absolutely, but relative to which containing element'? It will either be the entire body (the default) or some other element on the page that is already positioned (usually with relative or absolute - fixed is also useful and supported in IE 7 but see this bug). It is then taken out of the document flow - other elements might appear beneath it, but won't flow around it. If it appears behind another element, you need to set the z-index property to move it in front.

A common solution is to have a centred container (margin: 0 auto) with position: relative within which other items are placed with position: absolute.

Finally, I like this little interactive CSS positioning demo.

Dave Everitt