Hard to find official, or even authoritative, documentation on this issue, probably because it's not a common real-life scenario, but the issue has been discussed before by Ian Hickson and Tantek Celik, the editors of the CSS Specification.
Below are some parts of a thread from a W3C mailing list in which spec writers discuss the behavior of z-index
in relation to the body
element.
The essence of the discussion is this:
The browser is right to ignore z-index
on non-positioned elements (like body
), because z-index
applies only to positioned elements
z-indexing of an element is relative only to sibling elements, and not to parent elements, due to the creation of stacking contexts
... a child box cannot circumvent the z-order of its parent. it can
place itself behind or in front of any of its siblings, but it cannot
place itself behind it's parent box. it can place itself in such a
way that it is first child rendered before any other child ....
There's also an interesting note about the behavior of z-index
with position: fixed
on the body
element, but it doesn't look like it was implemented.
Tantek Celik:
I've got a question regarding the z-index
property: Why can I still
see elements which I've put behind the BODY element (see example
below)?
Because your browser has a bug or doesn't support z-index
.
Or the browser doesn't have a bug and is properly ignoring z-index
on non-positioned elements.
<BODY STYLE="z-index: 0; background-color: #456789;">
<P STYLE="z-index: -1; color: #fedcba;">This is a test showing a text
that shouldn't be visible as it lies behind the body element
<EM>("z-index" property equals "-1").</EM>
</BODY>
In the example given, both elements which have a z-index
do not have
their position property set, and since according to:
http://www.w3.org/TR/REC-CSS2/visuren.html#z-index
the z-index property "Applies to: positioned elements", the settings
on the z-index
property are ignored.
Tantek
response from spec writer:
hi,
(Tantek: i do agree with you that the spec says z-index
is only for
positioned elements, but as Ian says, i don't see why it cannot be applied for static boxes which overlap due to negative margins.)
look at the following markup:
<body style="z-index=0; background-color=green;">
<p style="z-index=-1; position: relative; top: 50; left: 20"> hello!!
</p>
<p> world!! </p>
</body>
P is a child of BODY, and according the CSS2 spec (my reading, anyway
:) z-indexing of an element is relative only to sibling elements, and
not to parent elements. this has to do with the creation of a z-index
contexts.
so the box for 'hello!!' would show up behind the box for 'world!!'
but NOT behind the box for BODY because BODY contains both P element
boxes.
!!! please correct if i'm wrong on this, because if i am, i have to
redo my renderer so that it does not respect hierarchical boundaries,
which is NOT a happy situation. !!!
the original intent of this thread (the question) can be attained by
making the z-index=-1
box a position: fixed
box (though the
positioning properties would be complex.) this would work because
fixed boxes break out of the hierarchy where they are found and
levitate to the viewport box, essentially becoming a sibling to the
BODY box.
-- ranjit
spec writer:
please,
pardon my use of implementation specific terms.
you are correct about the HTML assumption. but my point remains
valid, even if we look at the hierarchy of the document/box structure
regardless of markup.
currently, in my implementation, the following implicit rule is
dictated by the hierarchical structure of the boxes generated by
parsing a hierarchical document structure (XML, HTML, ...)
RULE: a child box cannot circumvent the z-order of its parent. it can
place itself behind or in front of any of its siblings, but it cannot
place itself behind it's parent box. it can place itself in such a
way that it is first child rendered before any other child, ....
i see this rule implied by the CSS2 spec as well, in the section where
there is talk of z-order contexts and so on.
as i see it, z-order values for a CHILD boxes are valid within the
parent z-order context. this can be illustrated with absolute
positioned boxes containing absolute positioned boxes where a child
absolute boxes have a z-ordering that cannot violate the z-ordering of
the parent absolute boxes.
again, let me say: if i have misunderstood something in the spec, let
me know, for if the above rule is invalid, then it seems to me the
notion of hierarchy is not preserved for non-fixed boxes.
regards,
-- ranjit
http://lists.w3.org/Archives/Public/www-style/1999Aug/0131.html