


Detailed explanation of CSS z-index attribute (with image analysis)_html/css_WEB-ITnose
Sometimes we use the z-index property in CSS to make some block elements more layered.
As shown below: The mouse can be placed behind the text through z-index, making the web page more layered
The code to achieve the above effect is as follows:
<span style="font-size:24px;"><html><head><style type="text/css">img.x{position:absolute;left:0px;top:0px;z-index:-1}</style></head><body><h1>这是一个标题</h1><img class="x" src="/i/eg_mouse.jpg" /> <p>默认的 z-index 是 0。Z-index -1 拥有更低的优先级。</p></body></html></span>
However, many novices who have just learned CSS are a little confused about the z-index attribute. They have clearly set the z-index attribute themselves, but they don’t see any effect when they ask why. ?
To solve this problem, you need to go to w3c and take a look at the official definition:
There is something special required here The explanation is: Z-index can only work in elements where the three positioning attributes of absolute, fixed or relative are clearly defined. If the position attribute is not defined, the z-index attribute will not work.
So Z-index can only work on positioned elements (such as position:absolute;)
Use the following code and diagram to compare:
1, the position attribute is not defined in the code
<span style="font-size:24px;"><!DOCTYPE html><html><head> <title>学习认识z-index</title> <meta charset="utf-8"> <style type="text/css"> #box{ width: 1000px; height:1000px; border: 1px royalblue solid; } #box1{ background-color: red; width: 200px; height: 200px; z-index: 999; } #box2{ background-color: yellow; width: 200px; height: 200px; margin-left: 150px; z-index: 99; } #box3{ background-color: green; width: 200px; height: 200px; margin-left: 300px; z-index: 9; } </style></head><body><div id="box"> <div id="box1"> <span >我的z-index为999</span> </div> <div id="box2"> <span >我的z-index为99</span> </div> <div id="box3"> <span >我的z-index为9</span> </div></div></body></html></span>
The result is displayed as shown below:
From the picture above, we can see that we have clearly set the z-index attribute, but why is there no cascading effect? This is a problem that most novices will encounter. At this time, we have to go to W3C to read the rules about z-index. There is a sentence in the above W3C rules that everyone must remember: "Z-index only It can work on positioned elements (such as position:absolute;)"
Let’s add the position:absolute; attribute to the code to see the display effect:
2, the code that defines the position:absolute; attribute:
<!DOCTYPE html><html><head> <title>学习认识z-index</title> <meta charset="utf-8"> <style type="text/css"> #box{ width: 1000px; height:1000px; border: 1px royalblue solid; } #box1{ background-color: red; width: 200px; height: 200px; z-index: 999; position: absolute; } #box2{ background-color: yellow; width: 200px; height: 200px; margin-left: 150px; z-index: 99; position: absolute; } #box3{ background-color: green; width: 200px; height: 200px; margin-left: 300px; z-index: 9; position: absolute; } </style></head><body><div id="box"> <div id="box1"> <span >我的z-index为999</span> </div> <div id="box2"> <span >我的z-index为99</span> </div> <div id="box3"> <span >我的z-index为9</span> </div></div></body></html>
At this time we will see the cascading effect as shown below .
To sum up, I have made the following summary for your reference.
z-index attribute in CSS
1. Definition and usage
The z-index attribute sets the stacking order of elements. Elements with a higher stacking order will always appear in front of elements with a lower stacking order.
Note: Elements can have negative z-index attribute values.
Note: Z-index can only work on positioned elements (such as position:absolute;),
Note: Z-index can only work on elements that have the three positioning attributes of absolute, fixed or relative clearly defined. In an element, if the position attribute is not defined, the z-index attribute has no effect.
2, Description
This attribute sets the position of a positioned element along the z-axis, which is defined as the axis extending vertically to the display area. If it is a positive number, it is closer to the user, and if it is a negative number, it is further away from the user.
In layman’s terms, the larger the value of z-index, the closer it is to us and the forward it will be displayed.

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

The Y-axis position adaptive algorithm for web annotation function This article will explore how to implement annotation functions similar to Word documents, especially how to deal with the interval between annotations...

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...
