As background, below are the two relevant sections from W3C's CSS2.1 specification, chapter 9.
Within each stacking context, the following layers are painted in back-to-front order:
- the background and borders of the element forming the stacking context.
- the child stacking contexts with negative stack levels (most negative first).
- the in-flow, non-inline-level, non-positioned descendants.
- the non-positioned floats.
- the in-flow, inline-level, non-positioned descendants, including inline tables and inline blocks.
- the child stacking contexts with stack level 0 and the positioned descendants with stack level 0.
- the child stacking contexts with positive stack levels (least positive first).
...and this:
Within each stacking context, positioned elements with stack level 0 (in layer 6), non-positioned floats (layer 4), inline blocks (layer 5), and inline tables (layer 5), are painted as if those elements themselves generated new stacking contexts, except that their positioned descendants and any would-be child stacking contexts take part in the current stacking context.
Questions
- When we say that a "new stacking context" is generated by an element, does that mean that ONLY itself and its descendant (child/containing) elements are ordered in accordance to the new stacking context, and that that whole new stacking context is ordered (atomically) withing the root's stacking context (assuming no other contexts)?
- In the code below, I have a float, non-inline/non-positioned descendants of root, and inline-level/non-positioned descendants. Regardless, the float is not painted on top of the non-inline-level (block-level) box, as the spec seems to say it should. Only the background is painted on top of. Why is that?
.float {
background-color: red;
margin-right: -25px;
border: 5px solid green;
float: left;
color: gray;
font-size: 5rem;
}
.old {
background: aqua;
font-size: 3rem;
}
.new {
background: yellow;
font-size: 3rem;
}
<span class="old">tesssss</span>
<div class="float">testTwo</div>
<div class="new">foo</div>