Table of Contents
1. Z-index golden rule and stack context
2. Create stack context and precautions
三、z-index在某些浏览器中的问题
四、参考链接:
Home Web Front-end HTML Tutorial In-depth CSS attributes (9): z-index_html/css_WEB-ITnose

In-depth CSS attributes (9): z-index_html/css_WEB-ITnose

Jun 24, 2016 am 11:55 AM

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>
Copy after login

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>Header</h1> 	<p>I am paragraph. <em> I am em</em></p>			</body></html>
Copy after login

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;		}
Copy after login

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;		}
Copy after login

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;		}
Copy after login

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;		}
Copy after login

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;}
Copy after login

感谢您的阅读,文中不妥之处,还望批评指正。

四、参考链接:

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:

 

Statement of this Website
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

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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24
Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

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

HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML, CSS, and JavaScript: Web Development Trends The Future of HTML, CSS, and JavaScript: Web Development Trends Apr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The Future of HTML: Evolution and Trends in Web Design The Future of HTML: Evolution and Trends in Web Design Apr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML vs. CSS vs. JavaScript: A Comparative Overview HTML vs. CSS vs. JavaScript: A Comparative Overview Apr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML: Building the Structure of Web Pages HTML: Building the Structure of Web Pages Apr 14, 2025 am 12:14 AM

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 vs. CSS and JavaScript: Comparing Web Technologies HTML vs. CSS and JavaScript: Comparing Web Technologies Apr 23, 2025 am 12:05 AM

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

HTML: Is It a Programming Language or Something Else? HTML: Is It a Programming Language or Something Else? Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

See all articles