The influence of Position:absolute and Z-index on hierarchy structure in CSS

Source: Internet
Author: User
Css

Do not use position
In this case, each layer follows the HTML positioning rule, where the Left,right,top,bottom positioning information is not valid and Z-index does not work because there is no cascade.

2. Use of absolute

If the Z-index properties of two absolute anchored objects have the same value, they are stacked according to the order in which they are declared in the HTML document.

2.1 not used Z-index

In this case, they cascade according to the order they are declared in the HTML document, because the default is 0 if Z-index is not set.

<div id = "001" style= "Top:10"; left:30; width:100px; height:50px; border:1px solid #999; Background:green; Position:absolute; " >001</div> <div id = "002" style= "top:40"; left:40; width:100px; height:50px; border:1px solid #999; background:red; Position:absolute; " >002</div> <div id = "003" style= "top:80"; left:50; width:100px; height:50px; border:1px solid #999; Background:yellow;position:absolute; " >003</div>

001: At the bottom 002: At the middle tier 003: at the highest level

2.2 using Z-index

Z-index is an integer value that has no units, and can be negative. The larger the number, the higher the value, the more the outer layer.

<div id = "001" style= "Top:10"; left:30; width:100px; height:50px; border:1px solid #999; Background:green; Position:absolute; " >001</div> <div id = "002" style= "top:30"; left:40; width:100px; height:50px; border:1px solid #999; background:red; Position:absolute;z-index:1 ">002</div> <div id =" 003 "style=" TOP:50; left:20; width:100px; height:50px; border:1px solid #999; background:yellow;position:absolute;z-index:-1; " >003</div>

001: In the middle tier 002: At the top 003: at the lowest level

2.3 Parent-child elements

For a parent-child element, the child element is subordinate to the parent element's hierarchy, and the child element is above the parent layer

<div id = "001" style= "width:100px; height:50px; border:1px solid #999; Background:green; Position:absolute;z-index:1 ">001 <div id =" 002 "style=" TOP:20; left:40; width:100px; height:50px; border:1px solid #999; Background:red;position:absolute;z-index:-1 ">002</div> </div> <div id =" 003 "style=" TOP:20; left:40; width:100px; height:50px; border:1px solid #999; background:yellow;position:absolute;z-index:0 ">003</div>

001: In the middle tier 002: At the top 003: at the lowest level

3. Mixed use of absolute

For elements that do not have the Position:absolute property set, regardless of the Z-index setting, the number is 0, but below the Position:absolute z-index:0 of elements

<div id = "001" style= "top:30"; left:30; width:100px; height:50px; border:1px solid #999; Background:green; Position:absolute; " >001</div> <div id = "002" style= "top:20"; left:40; width:100px; height:50px; border:1px solid #999; Background:red;z-index:1 ">002</div> <div id =" 003 "style=" TOP:20; left:40; width:100px; height:50px; border:1px solid #999; Background:yellow;position:absolute;z-index:-1 ">003</div>

001: At the top 002: In the middle tier 003: at the lowest level



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.