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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 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)

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

The article discusses the &lt;iframe&gt; tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

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

See all articles