One of the most common pitfalls in CSS is to discover, to our great frustration, that our meticulously arranged elements overlap in unexpected ways. Understanding CSS stacking is essential to master element layering and solve these display problems. In this guide, we will explore the stacking context, painting order, and document flow to help you take full control of your CSS layouts.
Stacking context
The stacking context can be thought of as a set of superimposed layers that determine how HTML elements are visually stacked on top of each other. Each stacking context creates a self-contained environment in which elements are ordered independently.
Imagine a web page as a complex structure composed of multiple overlapping layers. Each layer can contain other sub-layers, creating a hierarchy of stacking contexts. This hierarchical structure determines which elements appear in front of or behind others.
It is important to note that child elements cannot intermix with elements from another stacking context. They are always grouped within their parent stacking context. This means that a child element, even with a very high z-index value, cannot appear above an element in a higher-level stacking context.
The root element, <html>, establishes the base stacking context for the entire page. Every other stacking context is nested within this root context. New stacking contexts are created by elements with certain CSS properties, such as a position other than “static” combined with a z-index value, elements with opacity less than 1, elements with transform, filter, or other specific properties.
These stacking context mechanisms allow you to precisely control the arrangement of elements on your web page. By understanding how stacking contexts are created and how they interact with each other, you can solve most CSS layering problems.
Painting order
This concept defines the order in which elements are rendered on the web page. Understanding the painting order is essential for mastering how elements visually overlap.
The painting order refers to the sequence in which the browser draws elements on the screen. This order determines which elements appear in front of or behind others when they overlap.
Here is a simplified hierarchy of the painting order of elements within a stacking context:
- Elements with a position other than “static” and a negative “z-index” value (displayed first, at the back).
- Non-positioned (static) “block” type elements.
- Non-positioned floating elements.
- Non-positioned inline, table, and inline-block elements (these are the inline-level elements).
- Positioned elements (other than “static”) with a “z-index” of “auto” or equal to 0.
- Positioned elements that have a “z-index” other than “auto” and greater than 0 (displayed last, at the front).
- These elements are arranged according to their “z-index” value, with the highest values displayed in front.
Understanding the painting order is crucial for controlling element layout in CSS. When elements overlap, their display order depends on both their stacking context and their position in the painting order.
- Within the same stacking context, elements follow the painting order described above. A positioned element with a positive z-index will always appear in front of a non-positioned block element.
What happens when multiple elements are at the same level and share the same characteristics?
They are simply stacked like a deck of cards, following the priority order defined by the stacking order and the painting order.
To visualize this situation, you can use a negative margin to make an element overlap with the previous one. You will then see that the second element appears on top of the first, because it comes later in the source order.
Here is an image that illustrates this concept:



As you can observe, block-type elements are not all at the same level. Some appear in front of others depending on their position in the stacking order and their z-index value.
CSS stacking: summary
As you have noticed, it is clear that not all block-type elements are at the same level. Their stacking depends on several factors including the stacking context, the painting order, and their position in the document flow.
Document flow is a fundamental concept in CSS and plays an essential role in how elements are arranged on a page. It refers to the default order in which elements are displayed by the browser, following the order in which they appear in the HTML source code.
This means that if you have two block elements A and B, and A appears before B in the HTML code, then A will be rendered first, and B will be placed after A in the document flow.
It is important to understand this concept, as it allows you to predict and control the layout of elements on a web page. Elements that are later in the source code will naturally appear on top of earlier elements when they overlap, unless their stacking context or z-index dictates otherwise.
Thus, mastering document flow is essential for solving CSS overlap problems and creating well-structured web layouts.

There you go, now you can impress your friends at parties…






0 Comments