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:

The future of HTML will develop in a more semantic, functional and modular direction. 1) Semanticization will make the tag describe the content more clearly, improving SEO and barrier-free access. 2) Functionalization will introduce new elements and attributes to meet user needs. 3) Modularity will support component development and improve code reusability.

HTMLattributesarecrucialinwebdevelopmentforcontrollingbehavior,appearance,andfunctionality.Theyenhanceinteractivity,accessibility,andSEO.Forexample,thesrcattributeintagsimpactsSEO,whileonclickintagsaddsinteractivity.Touseattributeseffectively:1)Usese

The alt attribute is an important part of the tag in HTML and is used to provide alternative text for images. 1. When the image cannot be loaded, the text in the alt attribute will be displayed to improve the user experience. 2. Screen readers use the alt attribute to help visually impaired users understand the content of the picture. 3. Search engines index text in the alt attribute to improve the SEO ranking of web pages.

The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

HTML is used to build websites with clear structure. 1) Use tags such as, and define the website structure. 2) Examples show the structure of blogs and e-commerce websites. 3) Avoid common mistakes such as incorrect label nesting. 4) Optimize performance by reducing HTTP requests and using semantic tags.


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 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
