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

WBOY
WBOYOriginal
2016-06-24 11:55:571204browse

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>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:

 

Statement:
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