Positioning

Extended Notes

Positioning Schemes

There are 3 positioning schemes for layout in CSS:

  1. Normal flow

    The box belongs to the formatting context. Positioning can be relative, sticky etc.

  2. Floats

    The box is first laid out according to the normal flow, then taken out of the flow and positioned, typically to the left or the right. Content may flow aside of the float.

  3. Absolute positioning

    The box is removed from the normal flow entirely and assigned a position with respect to a containing block, which, for a position: absolute element, can be a nearest position: relative ancestor.

We say a box is out-of-flow when it's floated, absolutely positioned or is the root element. Everything else is called in-flow.

Positioning with position

The references attached below have more details about them (and additionally sticky).

Stacking Context

Stacking with z-index doesn't always work in absolute offsets in relation to each other. It may be useful to uncover how the rendering engine figures out all that before diving into.

Drawing the concepts over a really broad stroke, a stacking context can be seen an independent rendering of things stacking on top of each other. Eventually the many layers in this stacking context will be squashed into a single flat drawing that may be used in drawing a parent stacking context.

The order of rendering (or painting) for the elements in your rendering context is generally first the non-positioned (position: static) elements, where an element comes later in the code appears to be over the elements before it in the code, followed by the other positioned elements, some of which absolute or fixed, similarly where an element comes later in the code appears to be over the elements before it in the code. Those positioned elements can be drawn earlier or later depending on their z-indices.

A semi-transparent element (opacity less than 1) can make its own stacking context; same for positioned elements with a non-zero z-index.

See the W3C working draft for detailed info (link attached in references).

References