z-index attribute in CSS: a powerful tool to control the order of page elements stacking
This article explores the z-index
attribute in CSS, which is used to control the cascade order of page elements. Elements with higher values will be displayed on elements with lower values. This is similar to the x-axis and y-axis on the page controlling the horizontal and vertical positions of elements respectively, and z-index
controls the stacking order of elements on the z-axis.
Key points:
-
The
- attribute in
z-index
controls the stacking order of page elements. The higher the value, the more the element is displayed. It only works on elements whose attribute values areposition
,absolute
orrelative
.fixed
By setting - without assigning values to
position: relative
,top
,right
,bottom
orleft
attributes, you can control the stacking order of elements without changing their original position on the page.
The parsing of the -
z-index
value is performed within its parent stacking context, which means that even if the element'sz-index
value is high, the element may still be displayed elsewhere if its parent container'sz-index
value is low. Under the element. - To improve organization and flexibility, it is recommended to use an increment of 100 to set the
z-index
value. This approach provides 99 available numerical choices when adding a new layer between two existing layers.
Default stacking order
When writing HTML, the back elements in the document will be stacked on top of the front elements by default.
<header class="site-header"></header> <main class="site-content"></main> <footer class="site-footer"></footer>
In this HTML snippet, if all elements are set to overlap, the footer will be stacked on top of the body content, which in turn will be stacked on top of the header.
Elements can be overlapped by combining position
attributes and top
, right
, bottom
, left
,
position
If the absolute
attribute of each element is set to
.site-header, .site-content, .site-footer { position: absolute; width: 400px; padding: 20px; } .site-header {top: 0; left: 0;} .site-content {top: 50px; left: 50px;} .site-footer {top: 100px; left: 100px;}
top
Use offset properties left
and
Stack context
position: absolute
Although overlapping elements can be created using , this does not create the so-called stacking context
The stacking context can be created in the following ways:
-
The
position: absolute
element has therelative
orz-index
attributes, and theauto
value is not . -
auto
flexbox items havez-index
values that are not . -
The opacity of the
opacity
element ( ) is less than 1. -
The
transform
attribute of thenone
element is set to a non- value.
The common way to create and use stacking contexts is the first, let's focus on it.
Go back to the previous example, we have three elements overlapping each other, but at the moment they do not have z-index
values.
z-index
attribute allows us to control the stacking order.
If we set the footer z-index
to 1, the body z-index
to 2, and the header z-index
to 3, the default stacking order can be completely reversed.
On the surface it seems simple: the higher the value, the higher the elements stack - so z-index
will always be above z-index: 9999
. Unfortunately, the reality is more complicated than this. z-index: 9
<header class="site-header"></header> <main class="site-content"></main> <footer class="site-footer"></footer>If you add a box inside the
container and position it outside the bottom right corner, we will see it sits above the green box, under the pink box. site-content
.site-header, .site-content, .site-footer { position: absolute; width: 400px; padding: 20px; } .site-header {top: 0; left: 0;} .site-content {top: 50px; left: 50px;} .site-footer {top: 100px; left: 100px;}Based on what we know about
, we might think that to make this yellow box appear on top of the pink box, we just need to set a higher value for z-index
. z-index
to 4 (above z-index
), we won't see any changes. People usually try to force stacking with very large numbers (like 9999), but this also has no effect. Seen in the code base such a z-index: 3
value is a code odor, so try to avoid it if possible. z-index
behaves in the stacking context. z-index
...(The HTML and CSS code in the MDN example is omitted here because the article is too long, but the key explanation part is retained)....
All of this can be explained by the fact that all
values are parsed within their parent stacking context. Because the parent container z-index
's value is higher than the footer, all positioning elements within .site-content
are evaluated in that context. z-index
.site-content
Understand the stacking order of elements within the stacking context, you can compare them to children in nested ordered lists.
This can be written as follows:
Header:
-
z-index: 5
Subject: -
z-index: 4
Box 1:-
z-index: 4.6
Box 2: -
z-index: 4.1
Box 3: -
z-index: 4.3
-
-
z-index: 2
Therefore, even if the header's
is 6, its rendering order is 4.6, which is still less than 5. Therefore, box 1 is displayed under the header. z-index
z-index
It was a little confusing at first, but with practice it really started to make sense!
z-index is only suitable for positioning elements
If you want to control the stacking order of elements, you can use z-index
. However, position
will only take effect when the absolute
value of the element is relative
, fixed
or z-index
.
Putting elements accurately with position
is great for building complex layouts or interesting UI patterns, but it is often desirable to control the stacking order without moving the elements in the original position on the page.
If this is the case, you can set only position: relative
but do not provide any value for top
, right
, bottom
or left
. The element will remain in its original position on the page, the document flow will not be interrupted, and the z-index
value will take effect.
Negative z-index value can be used
Hydrated elements are often used to build complex shapes or UI components. This usually means that the elements are superimposed on each other and the z-index
value continues to increase. To place an element below another element, it just needs to have a lower z-index
value, which can be negative.
This is useful when using a pseudo-element and want to position it behind its parent element content.
Due to the way stacking context works, if the
or element is to be positioned after the text content of its parent element, a negative :before
value needs to be set on it. :after
z-index
z-index strategy
Let's summarize a simple strategy I used when applying
in my project.
z-index
We used single-digit increments of the value
and z-index
? You have to change many values - possibly throughout the code base, which can cause problems and can cause CSS breaks in other parts of the website. z-index: 3
z-index: 4
Set z-index with 100 steps
When dealing with
, you often see code like this:
z-index
<header class="site-header"></header> <main class="site-content"></main> <footer class="site-footer"></footer>). Seeing such a value usually indicates that the developer does not understand the stacking context and is trying to force one layer to be on top of the other.
!important
Instead of using any number like 9999, 53 or 12, we can systematize our
z-index
I did not use single-digit increments of
z-index
.site-header, .site-content, .site-footer { position: absolute; width: 400px; padding: 20px; } .site-header {top: 0; left: 0;} .site-content {top: 50px; left: 50px;} .site-footer {top: 100px; left: 100px;}
This manual approach is very reliable when building a system, but when used in conjunction with a preprocessor like Sass, it can be made more flexible.
...(The FAQs part is omitted here because it is more repetitive with the previous output)...z-index
The above is the detailed content of Mastering z-index in CSS. For more information, please follow other related articles on the PHP Chinese website!

CSS Grid is a powerful tool for creating complex, responsive web layouts. It simplifies design, improves accessibility, and offers more control than older methods.

Article discusses CSS Flexbox, a layout method for efficient alignment and distribution of space in responsive designs. It explains Flexbox usage, compares it with CSS Grid, and details browser support.

The article discusses techniques for creating responsive websites using CSS, including viewport meta tags, flexible grids, fluid media, media queries, and relative units. It also covers using CSS Grid and Flexbox together and recommends CSS framework

The article discusses the CSS box-sizing property, which controls how element dimensions are calculated. It explains values like content-box, border-box, and padding-box, and their impact on layout design and form alignment.

Article discusses creating animations using CSS, key properties, and combining with JavaScript. Main issue is browser compatibility.

Article discusses using CSS for 3D transformations, key properties, browser compatibility, and performance considerations for web projects.(Character count: 159)

The article discusses using CSS gradients (linear, radial, repeating) to enhance website visuals, adding depth, focus, and modern aesthetics.

Article discusses pseudo-elements in CSS, their use in enhancing HTML styling, and differences from pseudo-classes. Provides practical examples.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
