If you are not a csser novice, you must have a general understanding of the usage of z-index. Z-index can control the positioning of elements in the direction perpendicular to the display screen (Z axis) ), this article does not go into how to use the basic API, but to have a deeper understanding of how z-index works, what are the problems when using z-index, and the use of z-index in daily development use. Let's introduce today's text through an example. The code example:
<style type="text/css"> .red, .green, .blue { position: absolute; width: 100px; height: 100px; text-align: center; line-height: 100px; color: #fff; } .red { background-color: red; z-index: 1; } .green { background-color: green; top: 70px; left: 70px; } .blue { background-color: blue; top: 140px; left: 140px; }</style> <div> <span class="red">Red box</span></div><div> <span class="green">Green box</span></div><div> <span class="blue">Blue box</span></div>
As shown below:
The above code is easy to understand. Here is a question for everyone to think about: After following the following How to use red span element after green and blue element in case of rules?
1) Cannot change html tags in any way;
2) Cannot add or change the z-index attribute of any element;
3) Do not add or change any The position attribute of the element;
Please think about it, how to solve this problem? Explain the reason? ----------------------------------Separating line------------- ----------------------------------
1. Z-index golden rule and stack context
1) A box has the same stack level as its parent, unless the box is assigned a different stack level through the z-index attribute;
2) z-index The attribute is only applicable to element objects whose position attribute is relative, absolute, or fixed;
3) Setting an opacity attribute value less than 1 for a positioned element means creating a stack context. ), just like adding a z-index value to the element;
4) For a positioned box, if the z-index attribute is specified, it means:
-> The stack level of the box is in the current stack context;
-> The box establishes a local stack context;
5) If the box does not specify z-index, the element will be pressed down The order of stacking (stacked) (from back to front):
-> boxes in normal flow, according to the sequence in the source code;
-> floating boxes;
-> After computed, the display attribute value is inline/inline-block/inline-table boxes;
-> Positioned boxes and boxes set the opacity value to less than 1, according to the source code Sequence;
Therefore, when we set the z-index to a positioned element, we do two things:
1) This element shares the same stack context with the elements before or after it, which is why when we change the z-index value, the element will move other elements in front or behind.
2) Creates a new stack context for any element within that element. Once you create a stack context, any layers inside that have (stack context) will stay in this stack context. Through the above golden rules, maybe you already know the answer to the above question. In the golden rule, we mentioned a new term "stack context". Let's introduce it through an example:
<!DOCTYPE html><html><html lang="en"><head> <meta charset="utf-8"> <title>z-index example</title></head><body><h1 id="Header">Header</h1> <p>I am paragraph. <em> I am em</em></p> </body></html>
A very special case is that in a document, there is no positioning. Document has one and only one stacking environment - created through HTML. Next, we add the following styles to the above example:
h1, p { position: relative; } h1 { z-index: 2; } p { z-index: 1; }
In this case, h1 and p have created a stack context, and both stack contexts are within the stack context of the document. After adding the style, h1 is above the p element. What will happen if we add the following style to the em element:
h1, p, em { position: relative; } h1 { z-index: 2; background-color: #f0f; } p { z-index: 1; background-color: #00f; line-height: 40px; } em { z-index: 1; background-color: #f00; }
After adding this style, em creates a stack context. Due to the z-index attribute of em, its internal text is larger than the text in the p tag. Other text is closer to the user. Because it is inside the stack context of p, it is always lower than the text in h1. Note: If you increase the z-index value, you cannot use em above h1. If you want elements of one context to be on top of elements in another context, you must raise the entire context or set them to the same context. The following are two solutions: Solution 1:
h1, p, em { position: relative; } h1 { z-index: 2; background-color: #f0f; } p { /* raise the entire context,p and em 都在h1 之上了*/ z-index: 3; background-color: #00f; line-height: 40px; margin-top: -40px; } em { z-index: 1; background-color: #f00; }
Solution 2:
h1, p, em { position: relative; } h1 { z-index: 2; background-color: #f0f; } p { background-color: #00f; line-height: 40px; margin-top: -40px; } em { /* put them into the same context */ z-index: 2; background-color: #f00; }
2. Create stack context and precautions
Then create stack context What are the ways?
1) When an element is the root element of a document (theelement)
2) When an element has a position value other than static and a z-index value other than auto
3) When an element has an opacity value less than 1
Update: In addition to opacity, several newer CSS properties also create stacking contexts. These include: transforms, filters, css-regions, paged media, and possibly others. As a general rule, it seems that if a CSS property requires rendering in an offscreen context, it must create a new stacking context.
In WebKit, styling a box with position:fixed or -webkit-overflow-scrolling:touch implicitly creates a stacking context, just like adding a z-index value.
Also, be aware of these CSS3 “triggers”:
transform != none
transform-style: preserve-3d
filter != none clip-path, mask
Lastly, even though a relatively positioned element without a z-index set does not establish a stacking context… A common IE bug, often seen in drop-down menus, is that any relatively positioned element that has haslayout set to true establishes a stacking context. One may visualize this bug by setting [A] and [B] to position:relative, while [a] gets position:relative; z-index:1. Now, dragging [A] under [B] hides [a] - in Internet Explorer, that is. Any positioned child with a z-index is caught by this wrong stacking context of its parent.
三、z-index在某些浏览器中的问题
1) IE6中的 select元素是一个窗口控件,所以它总是出现在层叠顺序的顶部而不会顾及到自然层叠顺序、position属性或者是z-index。可以在div元素上添加一个iframe设置为position:absolute,并设置div的z-index比iframe的高。
2) 因父容器(元素)被定位的缘故,IE6/7会错误的对其stacking context进行重置。
3) 在Firefox2版本中,一个负的z-index值会使元素位于stacking context的后面,而不是位于公认的背景和边框这样的元素stacking context之前。 本文到此结束,最后附上本文开始时提出的问题的答案:
/* add this */div:first-child {opacity: .99;}
感谢您的阅读,文中不妥之处,还望批评指正。
四、参考链接:
Find out how elements stack and start using low z-index values
The Z-Index CSS Property: A Comprehensive Look
Elaborate description of Stacking Contexts
Overlapping And ZIndex
CSS/Properties/z-index
Understanding CSS z-index(MDN)
What No One Told You About Z-Index
测试Demo:

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

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

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

HTMLisaspecifictypeofcodefocusedonstructuringwebcontent,while"code"broadlyincludeslanguageslikeJavaScriptandPythonforfunctionality.1)HTMLdefineswebpagestructureusingtags.2)"Code"encompassesawiderrangeoflanguagesforlogicandinteract

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.

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.

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.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6
Visual web development tools