search
HomeWeb Front-endCSS TutorialExplanation of the hierarchical relationship priority concept of CSS z-index

This article mainly introduces the concept of hierarchical relationship priority of CSS z-index. The explanation is more detailed. Friends in need can refer to it.

The z-index attribute in CSS is used to set the stacking order of nodes. Nodes with a higher stacking order will be displayed in front of nodes with a lower stacking order. This is our common understanding of the z-index attribute. . At the same time, we are always unclear about the stacking order. Setting the z-index value to a large value may not necessarily display the node at the front. This article will analyze the use of z-index through some examples. And bring you the concept of z-index hierarchical tree.

Order rules

If you do not set the position attribute of the node, the node located at the back of the document flow will cover the front The node.

The code is as follows:

<p id="a">A</p>
<p id="b">B</p>

CSS z-index层级关系优先级的概念说明

Example of CSS z-index attribute order rule

Positioning rule

If position is set to static, nodes located behind the document flow will still cover the floating nodes in front, so position:static will not affect the covering relationship of nodes.

The code is as follows:

<p id="a" style="position:static;">A</p>
<p id="b">B</p>

CSS z-index层级关系优先级的概念说明

Examples of CSS z-index attribute positioning rules, static

If position is set to relative (relative positioning), absolute (absolute positioning) or fixed (Fixed positioning), such nodes will cover nodes that have no position attribute set or whose attribute value is static, indicating that the former has a higher default level than the latter.

The code is as follows:

<p id="a" style="position:relative;">A</p>
<p id="b">B</p>

CSS z-index层级关系优先级的概念说明

Examples of CSS z-index attribute positioning rules, relative | absolute | fixed

Without interference from the z-index attribute, according to these order rules and positioning rules, we can do to create a more complex structure. Here we do not set position for A and B, but set position:relative for A's child node A-1. According to the order rules, B will cover A, and according to the positioning rules A' will cover B.

The code is as follows:

<p id="a">
 <p id="a-1" style="position:relative;">A-1</p>
</p>
<p id="b">B</p>

CSS z-index层级关系优先级的概念说明

Example of mutual overwriting of CSS z-index properties

When is the above mutual overwriting used? Such an implementation? It may seem unorthodox, but it is actually very commonly used. For example, the category display list in the side column of an e-commerce website can be implemented using this technique.

The picture below is the category display area of ​​a website , the floating layer of the second-level category covers the outer frame of the first-level category list, and the nodes of the first-level category cover the floating layer of the second-level category. If CSS is used to achieve the display effect, the outer frame of the first-level category is equivalent to the above In the example of A, the node of the first-level category is equivalent to A-1, and the floating layer of the second-level category is equivalent to B.

Category display list in the side column of an e-commerce website

CSS z-index层级关系优先级的概念说明

Participation Rules

We try not to use the position attribute, but add the z-index attribute to the node. It is found that z-index has no effect on the node. Function.

The code is as follows:

<p id="a" style="z-index:2;">A</p>
<p id="b" style="z-index:1;">B</p>
<p id="c" style="z-index:0;">C</p>

CSS z-index层级关系优先级的概念说明

Example of CSS z-index attribute participation rules, when there is no clear positioning

W3C The description of the z-index property mentions that the z-index property only takes effect when the node's position property is relative, absolute or fixed.

The z-index property specifies the stack order of an element. Only works on positioned elements(position: absolute;, position: relative; or position: fixed;).

The code is as follows:

<p id="a" style="z-index:2;">A</p>
<p id="b" style="position:relative;z-index:1;">B</p>
<p id="c" style="position:relative;z-index:0;">C</p>

CSS z-index层级关系优先级的概念说明

CSS z-index property Examples of participation rules, only explicitly positioned nodes can use the z-index attribute

Default value rule

If all nodes have position:relative defined. z -Nodes with an index of 0 and no z-index defined are in the same level; however, nodes with a z-index greater than or equal to 1 will cover nodes with no z-index defined; nodes with a negative z-index will Overwritten by nodes without z-index defined.

The code is as follows:

<p id="a" style="position:relative;z-index:1;">A</p>
<p id="b" style="position:relative;z-index:0;">B</p>
<p id="c" style="position:relative;">C</p>
<p id="d" style="position:relative;z-index:0;">D</p>

CSS z-index层级关系优先级的概念说明

CSS z-index 属性默认值规则的例子

通过检查我们还发现, 当 position 设为 relative, absolute 或者 fixed, 而没有设置 z-index 时, IE8 以上和 W3C 浏览器 (下文我们统称为 W3C 浏览器) 的 z-index 默认值是 auto, 但 IE6 和 IE7 是 0.

从父规则

如果 A, B 节点都定义了 position:relative, A 节点的 z-index 比 B 节点大, 那么 A 的子节点必定覆盖在 B 的子节点前面.

代码如下:

<p id="a" style="position:relative;z-index:1;">
<p id="a-1">A-1</p>
</p>

<p id="b" style="position:relative;z-index:0;">
<p id="b-1">B-1</p>
</p>

CSS z-index层级关系优先级的概念说明

CSS z-index 属性从父规则的例子, 子节点不设定层级

如果所有节点都定义了 position:relative, A 节点的 z-index 和 B 节点一样大, 但因为顺序规则, B 节点覆盖在 A 节点前面. 就算 A 的子节点 z-index 值比 B 的子节点大, B 的子节点还是会覆盖在 A 的子节点前面.

代码如下:

<p id="a" style="position:relative;z-index:0;">
<p id="a-1" style="position:relative;z-index:2;">A-1</p>
</p>

<p id="b" style="position:relative;z-index:0;">
<p id="b-1" style="position:relative;z-index:1;">B-1</p>
</p>

CSS z-index层级关系优先级的概念说明

CSS z-index 属性从父规则的例子, 不可逾越的层级

很多人将 z-index 设得很大, 9999 什么的都出来了, 如果不考虑父节点的影响, 设得再大也没用, 那是无法逾越的层级.

层级树规则

可能你会觉得在 DOM 结构中的兄弟节点会拎出来进行比较并确定层级, 其实不然.

代码如下:

<p id="a" style="position:relative;z-index:2;">
<p id="a-1" style="position:relative;z-index:0;">A-1</p>
</p>

<p id="b">
<p id="b-1" style="position:relative;z-index:1;">B-1</p>
</p>

CSS z-index层级关系优先级的概念说明

CSS z-index 属性层级树规则的例子

我们认为同时将 position 设为 relative, absolute 或者 fixed, 并且 z-index 经过整数赋值的节点, 会被放置到一个与 DOM 不一样的层级树里面, 并且在层级树中通过对比 z-index 决定显示的层级. 上面的例子如果用层级树来表示的话, 应该如下图所示.

CSS z-index层级关系优先级的概念说明

CSS z-index 的层级树

图中虽然 A-1 (z-index:0) 的值比 B-1 (z-index:1) 小, 但因为在层级树里 A (z-index:2) 和 B-1 在一个层级, 而 A 的值比 B-1 大, 根据从父规则, A-1 显示在 B-1 前面.

参与规则 2

前面提到的参与规则认为只要节点的 position 属性为 relative, absolute 或者 fixed, 即可参与层级比较, 其实不准确. 如果所有节点都定义了 position:relative, 并且将 z-index 设为整数值, 根据从父规则, 父节点的层级决定了子节点所在层级.

代码如下:

<p id="a" style="position:relative;z-index:0;">
<p id="a-1" style="position:relative;z-index:100;">A-1</p>
</p>

<p id="b">
<p id="b-1" style="position:relative;z-index:0;">
 <p id="b-1-1" style="position:relative;z-index:10;">B-1-1</p>
</p>
</p>

<p id="c" style="position:relative;z-index:0;">
<p id="c-1">
 <p id="c-1-1">
  <p id="c-1-1-1" style="position:relative;z-index:1;">C-1-1-1</p>
 </p>
</p>
</p>

例子中 A, B-1, C-1-1 作为父节点, z-index 的值相同, 根据顺序规则, C-1-1 在 B-1 之前, B-1 在 A 之前; 又根据从父规则, 无论子节点的 z-index 值是什么, C-1-1-1 在 B-1-1 之前, B-1-1 在 A-1 之前.

CSS z-index层级关系优先级的概念说明

CSS z-index 属性参与规则 2 的例子, 所有节点参与层级比较

如果我们将所有父节点的 z-index 属性去除, 诡异的事情发生了. IE6 和 IE7 浏览器显示效果不变, 而 W3C 浏览器的子节点不再从父, 而是根据自身的 z-index 确定层级.

代码如下:

<p id="a" style="position:relative;">
<p id="a-1" style="position:relative;z-index:100;">A-1</p>
</p>

<p id="b">
<p id="b-1" style="position:relative;">
 <p id="b-1-1" style="position:relative;z-index:10;">B-1-1</p>
</p>
</p>

<p id="c" style="position:relative;">
<p id="c-1">
 <p id="c-1-1">
  <p id="c-1-1-1" style="position:relative;z-index:1;">C-1-1-1</p>
 </p>
</p>
</p>

根据默认值规则, IE6 / IE7 和 W3C 浏览器上的元素存在 z-index 默认值的区别. 我们相信, 仅当 position 设为 relative, absolute 或者 fixed, 并且 z-index 赋整数值时, 节点被放置到层级树; 而 z-index 为默认值时, 只在 document 兄弟节点间比较层级. 在 W3C 浏览器中, A, B-1 和 C-1-1 的 z-index 均为 auto, 不参与层级比较.

CSS z-index层级关系优先级的概念说明

CSS z-index 属性参与规则 2 的例子, z-index 为 auto 的节点不参与层级比较

而在 IE6 和 IE7 中, 因为 z-index 的默认值是 0, 所以也参与了层级比较.

CSS z-index层级关系优先级的概念说明

CSS z-index 属性参与规则 2 的例子, IE6 和 IE7 中 z-index 默认为 0

设置了 position 而没有 z-index 的节点虽然不参与层级树的比较, 但还会在 DOM 中与兄弟节点进行层级比较.

代码如下:

<p id="a" style="position:relative;">
<p id="a-1" style="position:relative;z-index:100;">A-1</p>
</p>

<p id="b">
<p id="b-1">
 <p id="b-1-1" style="position:relative;z-index:10;">B-1-1</p>
</p>
</p>

<p id="c" style="position:relative;">
<p id="c-1">
 <p id="c-1-1">
  <p id="c-1-1-1" style="position:relative;z-index:1;">C-1-1-1</p>
 </p>
</p>
</p>

我们对上个例子改造一下, 将 B-1 的 position 属性删除后, W3C 浏览器显示如下图. 根据定位规则, A 和 C-1-1 会显示在 B-1 的前面; 而根据顺序规则, C-1-1 又显示在 A 前面.

CSS z-index层级关系优先级的概念说明

CSS z-index 属性参与规则 2 的例子, position 为 auto 的节点不参与层级树比较, 但仍参与 DOM 兄弟节点间的层级比较, W3C 浏览器

在 IE6 和 IE7 中, 因为 A 和 C-1-1 设置了 position:relative, 而且 z-index 的默认值为 0, 所以也参与层级树比较, 所以有如下效果.

CSS z-index层级关系优先级的概念说明

CSS z-index 属性参与规则 2 的例子, position 为 auto 的节点不参与层级树比较, 但仍参与 DOM 兄弟节点间的层级比较, IE6 和 IE7

总结

浏览器节点显示层级是一个费力的活, 今天你觉得 A 区块会永远置顶, 但明天因为需求变动, 突然出现 B 元素需要置顶. 一层一层往上堆砌, 某天回头一看, 发现很多区块交错在一起, 而且他们的值一个比一个大, 根本搞不清头绪. 我觉得在操刀干活之前, 最好先将 position, z-index 和层级的关系搞搞清楚, 以免后患无穷.

另外, 非情非得已, 切勿用 JavaScript 计算 z-index, 并将某个节点的 z-index 设置成所有节点中层级最高.

因为篇幅太长, 本文仅从节点属性角度进行讨论, 没有涉及 select 和 iframe 等特殊页面节点考虑, 如果有机会下次再为大家分享.

以上就是CSS z-index 层级关系优先级的概念全部内容,希望能给大家一个参考,也希望大家多多支持PHP中文网。

The above is the detailed content of Explanation of the hierarchical relationship priority concept of CSS z-index. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Next Level CSS Styling for CursorsNext Level CSS Styling for CursorsApr 23, 2025 am 11:04 AM

Custom cursors with CSS are great, but we can take things to the next level with JavaScript. Using JavaScript, we can transition between cursor states, place dynamic text within the cursor, apply complex animations, and apply filters.

Worlds Collide: Keyframe Collision Detection Using Style QueriesWorlds Collide: Keyframe Collision Detection Using Style QueriesApr 23, 2025 am 10:42 AM

Interactive CSS animations with elements ricocheting off each other seem more plausible in 2025. While it’s unnecessary to implement Pong in CSS, the increasing flexibility and power of CSS reinforce Lee's suspicion that one day it will be a

Using CSS backdrop-filter for UI EffectsUsing CSS backdrop-filter for UI EffectsApr 23, 2025 am 10:20 AM

Tips and tricks on utilizing the CSS backdrop-filter property to style user interfaces. You’ll learn how to layer backdrop filters among multiple elements, and integrate them with other CSS graphical effects to create elaborate designs.

SMIL on?SMIL on?Apr 23, 2025 am 09:57 AM

Well, it turns out that SVG's built-in animation features were never deprecated as planned. Sure, CSS and JavaScript are more than capable of carrying the load, but it's good to know that SMIL is not dead in the water as previously

'Pretty' is in the eye of the beholder'Pretty' is in the eye of the beholderApr 23, 2025 am 09:40 AM

Yay, let's jump for text-wrap: pretty landing in Safari Technology Preview! But beware that it's different from how it works in Chromium browsers.

CSS-Tricks Chronicles XLIIICSS-Tricks Chronicles XLIIIApr 23, 2025 am 09:35 AM

This CSS-Tricks update highlights significant progress in the Almanac, recent podcast appearances, a new CSS counters guide, and the addition of several new authors contributing valuable content.

Tailwind's @apply Feature is Better Than it SoundsTailwind's @apply Feature is Better Than it SoundsApr 23, 2025 am 09:23 AM

Most of the time, people showcase Tailwind's @apply feature with one of Tailwind's single-property utilities (which changes a single CSS declaration). When showcased this way, @apply doesn't sound promising at all. So obvio

Feeling Like I Have No Release: A Journey Towards Sane DeploymentsFeeling Like I Have No Release: A Journey Towards Sane DeploymentsApr 23, 2025 am 09:19 AM

Deploying like an idiot comes down to a mismatch between the tools you use to deploy and the reward in complexity reduced versus complexity added.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!