Attribute and usage of z-index in css

Source: Internet
Author: User

Z-index specification reference

• In W3C CSS2.1, each element has a three-dimensional spatial location, except for the horizontal and vertical positions we are familiar, the elements can be stacked on the "Z axis" and arranged forward in sequence;
• The order in which elements are displayed in the Z axis is determined by the cascade context and Cascade level. In the document, each element belongs to only one cascading context. In a given cascade context, each element has an integer Cascade level, which describes the display order of elements in the "Z axis" in the same cascade context;
• In the same stack context, a large stack level is displayed on the top, while a small stack level is displayed on the bottom. When the stack level is the same, the back-to-font principle is followed );
• In different stack contexts, the display order of elements is determined by the Cascade level of the parent stack context. It has nothing to do with its own cascade level;
• Each positioning element belongs to a stacking context. The root element forms the root stacking context, while other stacking context is generated by the positioning element (the z-index of the positioning element is defined as a non-auto z-index value ), the positioning sub-element takes the local stacking context as a reference and uses the same rules to determine the cascade order;
• When either element is stacked and another element is contained in different stacking context elements, the order of display is determined by the stack level of stacking context. That is to say, in the same stacking context, the order is determined by the z-index of the element itself. Otherwise, it is determined by the z-index of the parent element of the stacking context.


Example

The code is as follows: Copy code

<Html>
<Head>
<Style type = "text/css">
Img. x
{
Position: absolute;
Left: 0px;
Top: 0px;
Z-index:-1
}
</Style>
</Head>

<Body>
<H1> This is a title
<P> The default value of z-index is 0. Z-index-1 has a lower priority. </P>
</Body>

</Html>


Note: an element can have a negative z-index attribute value.

Note: Z-index can only work on positioning elements (for example, position: absolute ;)!

Problems in IE
When the 'Z-index' of the positioning element is not set (auto by default), a new local Cascade context is created under IE6 IE7 IE8 (Q. In other browsers, new local cascading contexts are not generated in strict accordance with the standards.

This problem will lead to a major difference in the stacked relationship of positioning elements in different browsers, which can lead to page layout disorder and content coverage.

The affected browsers include IE6 IE7 IE8 (Quriks Mode)

Copy the code directly from w3help and analyze the following code:

The code is as follows: Copy code

<Style type = "text/css">
Body {margin: 0 ;}
. P1 {top: 20px; height: 50px; width: 150px; background-color: blue ;}
. P2 {top: 10px; left: 20px; height: 30px; width: 100px; background-color: yellow ;}
. P3 {top: 0px; left: 50px; height: 100px; width: 50px; background-color: red ;}
</Style>

<Div style = "position: relative;" class = "p1"> 1
<Div style = "position: absolute; z-index: 1;" class = "p2"> 2 </div>
</Div>
<Div style = "position: absolute;" class = "p3"> 3 </div>


Solve IE7's absolute position on z-index

For more details, see: http://www.111cn.net/cssdiv/163/42221.htm

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.