visit
In today's article, you'll learn how HTML elements are stacked by default.
Note, I'll NOT teach you how to change the stacking order of elements (you may use the z-index
CSS property for this purpose) but only teach you how things work "naturally".
z-axis: You may recall from high school math classes the 3D coordinate system, which is formed by the x, y, and z axes. In this system, if you combine two axes you create a two-dimensional plane. The remaining axis will always be perpendicular to the plane and form with it the 3D coordinate system. Consider the below
You can think of the xy plane (the blue layer) as the screen you're looking at right now. Since you're facing the screen (you're perpendicular to the screen) you're on the z-axis.
Although the image only shows one layer on the xy plane, the plane can contain an infinite number of overlapping layers. The overlapping can be total or partial.
Stacking: Considering the system described above in the context of a web page, stacking is the total or partial superposition of elements. The higher the stack, the closer to the user-facing the screen.
Root element: The <html>
element, which is the top-level element of any HTML document, is also known as the root element.
Positioned element:
A positioned element is an element whose computed position value is either relative, absolute, fixed, or sticky. (In other words, it's anything except static.)
Descendant element: A descendant element is an element that is nested within another element, regardless of how deeply nested.
Descendant non-positioned element: An element that is a descendant of the root element and that has NOT been positioned.
Descendant positioned element: An element that is a descendant of the root element and that has been positioned.
Note: Keep in mind that the <html>
element is a container for the <head>
and the <body>
elements. The former is NOT visible to the user and contains metadata about your page, for instance, it links your page to external style sheets. The latter is a container for the visible portion of your page. Therefore, the descendants of the <html>
element that matters to us is the one inside the <body>
element.
<html>
element.
<body>
<div class="static1">Static #1</div>
<div class="relative">Relative</div>
<div class="absolute">Absolute</div>
<div class="fixed">Fixed</div>
<div class="sticky">Sticky</div>
<div class="static2">Static #2</div>
</body>
You can see from the demo and from the snippet above that both static elements go below the positioned elements when they overlap, even though Static #2 comes last in the source code. This proves that in this imaginary z-axis, positioned elements are closer to the user than non-positioned elements.
What about the root element?
I haven't set any styles to it, which makes it keep its transparent background-color
. The white background you see comes from the light your device's screen emits. Don't believe me? See the for the background-color
.
If you set a different background-color
to the root element you'll confirm that it is below its descendant elements.
The first thing to consider is that, when we talk about stacking, we're talking about a tridimensional conceptualization of HTML pages. Elements are stacked on the z-axis, which is an imaginary axis perpendicular to the screen.
Second, in order to understand how elements are stacked by default, i.e., without the z-index
property, we need to make a distinction between three types of elements:
<html>
element) which is a container for every other element you add to your page.position
value equals to static
.position
value equals to anything but static
.
Once you understand what stacking means in the context of web pages and the different types of elements that can be stacked, understanding the default stacking order becomes crystal clear.
From a philosophical perspective, when you compare the Important Terms section with the default stacking order section you can see that learning why may be more difficult, but it will make learning the how easier.
If you're on HackerNoon, don't forget to subscribe if you want to get my latest articles straight to your inbox.