Appendix E. Elaborate description of Stacking Contexts

Contents

This chapter defines the CSS2.1 painting order in more detail than described in the rest of the specification.

E.1 Definitions

Tree Order
Preorder depth-first traversal of the rendering tree, in logical (not visual) order for bidirectional content, after taking into account properties that move boxes around such as the 'run-in' value of 'display'.
Element
In this description, "element" refers to both actual elements and pseudo-elements. Pseudo-elements and anonymous boxes are treated as descendants in the appropriate places. For example, an outside list marker comes before an adjacent ':before' box in the line box, which comes before the content of the box, and so forth.

E.2 Painting order

The bottom of the stack is the furthest from the user, the top of the stack is the nearest to the user:

               |     |         |    |        
               |          |    |    |     ⇦ ☻
               |          |         |     user
  z-index:  canvas  -1    0    1    2

The stacking context background and most negative positioned stacking contexts are at the bottom of the stack, while the most positive positioned stacking contexts are at the top of the stack.

The canvas is transparent if contained within another, and given a UA-defined color if it is not. It is infinite in extent and contains the root element. Initially, the viewport is anchored with its top left corner at the canvas origin.

The stacking order for an element generating a stacking context (see the 'z-index' property) is:

  1. If the element is a root element:
    1. background color of element over the entire canvas.
    2. background image of element, unclipped, painted at the origin that would be used if it was painted for the root element.
  2. If the element is a block, list-item, or other block equivalent:
    1. background color of element unless it is the root element.
    2. background image of element unless it is the root element.
    3. border of element.
    Otherwise, if the element is a block level table:
    1. table backgrounds (color then image) unless it is the root element.
    2. column group backgrounds (color then image).
    3. column backgrounds (color then image).
    4. row group backgrounds (color then image).
    5. row backgrounds (color then image).
    6. cell backgrounds (color then image).
    7. all table borders (in tree order for separated borders).
  3. Stacking contexts formed by positioned descendants with negative z-indices (excluding 0) in z-index order (most negative first) then tree order.
  4. For all its in-flow, non-positioned, block-level descendants in tree order: If the element is a block, list-item, or other block equivalent:
    1. background color of element.
    2. background image of element.
    3. border of element.
    Otherwise, the element is a table:
    1. table backgrounds (color then image).
    2. column group backgrounds (color then image).
    3. column backgrounds (color then image).
    4. row group backgrounds (color then image).
    5. row backgrounds (color then image).
    6. cell backgrounds (color then image).
    7. all table borders (in tree order for separated borders).
  5. All floating descendants, in tree order. For each one of these, treat the element as if it created a new stacking context, but any descendants which actually create a new stacking context should be considered part of the parent stacking context, not this new one.
  6. First for the element, then for all its in-flow, non-positioned, block-level descendants in tree order:
    1. For each element in each line box of the element, if any, or if the element is inline-level (note that all elements in line boxes are forcably inline-level), on a per-line-box basis:
      1. Background color of element.
      2. Background image of element.
      3. Border of element.
      4. For inline elements:
        1. Underline of element.
        2. Overline of element.
        3. Text of element.
        4. Then all the element's in-flow, non-positioned, inline-level descendants, including anonymous inline elements generated for text nodes inside the element, in tree order:
          1. Jump to step 6.1 for this element.
        5. Line-through of element.
        For inline-block and inline-table elements:
        1. For each one of these, treat the element as if it created a new stacking context, but any descendants which actually create a new stacking context should be considered part of the parent stacking context, not this new one.
        For inline-level replaced elements:
        1. The replaced content, atomically.
      5. Optionally, the outline of the element (see 9 below).
    2. Otherwise, if the element is a block-level replaced element, then the replaced content, atomically.
    3. Optionally, if the element is block-level, the outline of the element (see 9 below).
  7. All positioned descendants with 'z-index: auto' or 'z-index: 0', in tree order. For those with 'z-index: auto', treat the element as if it created a new stacking context, but any descendants which actually create a new stacking context should be considered part of the parent stacking context, not this new one. For those with 'z-index: 0', treat the stacking context generated atomically.
  8. Stacking contexts formed by positioned descendants with z-indices greater than or equal to 1 in z-index order (smallest first) then tree order.
  9. Finally, implementations that do not draw outlines in steps above must draw outlines from this stacking context at this stage.

E.3 Notes

If the root element is a block-level element, its background is only painted once, over the whole canvas. If the root element is not a block-level element, then its background is painted twice, once for canvas, and once for the box(es) generated by the element.

While the backgrounds of bidirectional inlines are painted in tree order, they are positioned in visual order. Since the positioning of inline backgrounds is unspecified in CSS2.1, the exact result of these two requirements is UA-defined. CSS3 may define this in more detail.