Home Web Front-end CSS Tutorial Solution to the level problem that relative absolute cannot break through_Experience exchange

Solution to the level problem that relative absolute cannot break through_Experience exchange

May 16, 2016 pm 12:05 PM
css

if we set li to position: relative; set span to position: absolute; then we will find that no matter how high the z-index value of span is set, it will always be below the parent behind it.

some time ago, i remember who asked a question in the group that really made everyone confused:

1

2

3

4

5

6

7

<ul>   

<li>第一块</li>   

<li><span>第二块</span></li>   

<li>第三块</li>   

<li>第四块</li>   

<li>第五块</li>   

</ul>

Copy after login

if we set li to position:relative ;set span to position:absolute; then we will find that no matter how high the z-index value of span is set, it will always be below the parent behind it.

1

2

3

*{margin:0; padding:0; list-style:none;}   

li {width:100px; height:100px; margin:0 5px 0 0; background:#000; float:left; position:relative; z-index:1;}   

li span {width:200px; height:100px; background:#c00; position:absolute; top:0; left:100px; z-index:1000;}

Copy after login

it is easy to find our children by trying it. the z-index value reaches 1000 and is set to position:absolut; the children are all filed under the parent. i thought about it for a long time, and i think the fundamental problem is: setting the same position: relative/absolute; the level between labels of the same level cannot be surpassed by z-index. in our example above, the level of the first li will always be smaller than the level of the next li, so we set position:absolute; on the children in li, giving a very high z-index value.

maybe you will think about it: as long as you set position: relative; very correct. when no other li sets position:relative; then the child we need can float above all content. but what if, in fact, span is required in all lis, and all properties need to be the same? of course we don't necessarily need this effect. but we need to have such an effect: all children are hidden, appear when there is a mouse reaction and float above all content. we need to know that this is indeed a headache, because as we saw above, the children are pressed under the next parent label when displayed. let's implement the positioning effect of this mouse response:

1

2

3

4

5

6

7

<ul>   

<li><a href="" title=""><span>第一块</span></a></li>   

<li><a href="" title=""><span>第二块</span></a></li>   

<li><a href="" title=""><span>第三块</span></a></li>   

<li><a href="" title=""><span>第四块</span></a></li>   

<li><a href="" title=""><span>第五块</span></a></li>   

</ul>

Copy after login

we complete this display-hide effect through the linked mouse event:

1

2

3

4

5

6

*{margin:0; padding:0; list-style:none;}   

li {height:100px; margin:0 5px 0 0; float:left; width:100px;}   

li a {position:relative; z-index:1; display:block; height:100px; width:100px; background:#000;}   

li a:hover {background:#000000;}   

li span {display:none;}   

li a:hover span {display:block; background:#c00; width:200px; height:200px; position:absolute; top:0; left:100px; z-index:1000;}

Copy after login

we set a to position:relative ;in this way, its children will be positioned based on the upper left corner of the parent as the coordinate origin. then we set the specific shape and positioning properties of the span, and then hide it. we then use a's pseudo-class:hover to activate span. if we look at the results, we'll see that everything that should be on top is now on the bottom. so how do we solve this problem? in fact, it is impossible to force a breakthrough with css, so we think about it, can we make the parent tag that has not been triggered not have the position:relative; attribute, but only when it is triggered? assign such a value to this parent? in fact, thinking of this can basically solve all the problems:

1

2

3

4

5

6

*{margin:0; padding:0; list-style:none;}   

li {height:100px; margin:0 5px 0 0; float:left; width:100px;}   

li a {display:block; height:100px; width:100px; background:#000;}   

li a:hover {position:relative; z-index:1; }   

li span {display:none;}   

li a:hover span {display:block; width:200px; height:200px; background:#c00; position:absolute; top:0; left:100px; z-index:1000; }

Copy after login

we only need to set the attribute of a:hover to position:relative;, so that a will be assigned a relative positioning attribute only when the mouse is triggered. this completes the problem of being blocked by other parent tags.

of course, if you don’t mind browsers like ie5, we can also simplify the code:

1

2

3

4

5

6

7

<ul>   

<li><span>第一块</span></li>   

<li><span>第二块</span></li>   

<li><span>第三块</span></li>   

<li><span>第四块</span></li>   

<li><span>第五块</span></li>   

</ul>

Copy after login

the css can be changed to this:

1

2

3

4

5

*{margin:0; padding:0; list-style:none;}   

li {height:100px; margin:0 5px 0 0; float:left; width:100px; background:#000;}   

li:hover {position:relative; z-index:1;}   

li span {display:none;}   

li:hover span {display:block; width:200px; height:200px; background:#c00; position:absolute; top:0; left:100px; z-index:1000; }

Copy after login

additional:
the article "position: relative/absolute cannot break through the level" published some time ago talked about the level in positioning. when i read it again in the past few days, i found that the article was not thorough and did not point to the key point. therefore, i would like to make a special supplement here in the hope that the levels in position can be explained more clearly and explicitly.

we all know that position has four different values, namely: static | absolute | fixed | relative. this is explained in su yu's "css2 chinese manual": static: no special positioning, the object follows html positioning rules; absolute: drag the object out of the document flow, use left, right, top, bottom and other attributes to make absolute position. and its cascading is defined through the z-index attribute. at this time, the object does not have margins, but there are still padding and borders; relative: the object cannot be stacked, but will be offset in the normal document flow based on attributes such as left, right, top, bottom, etc.; fixed: ie5.5 and ns6 are not yet available this property is not supported.

but to change the stacking position of an object, you need another css property: z-index. but this z-index is not omnipotent. it is restricted by the html code level. z-index can only reflect its effect on html of the same level. what needs to be stated here is that z-index can only be used when the position value of the object is relative/absolute. below we will give some examples to explain the characteristics of levels:

1

2

3

4

<p id="box_1">   

<p id="a">这是第一个块</p>   

<p id="b">这是第二个块</p>   

</p>

Copy after login

for the above html code, we also need to write a css to define it:

1

2

3

#a,#b {position:absolute; width:300px; height:100px; }   

#a {z-index:10; left:0; top:0; background:#000; }   

#b {z-index:1; left:20px; top:20px; background:#c00; }

Copy after login

this is the most common in this case, the stacking level of #a and #b can be set through z-index. this is not asked, so under what circumstances will problems arise? let’s look at another example:

1

2

3

4

5

6

<p id="box_1">   

<p id="a">这是第一个块</p>   

</p>   

<p id="box_2">   

<p id="b">这是第二个块</p>   

</p>

Copy after login

write another css based on this structure. pay attention to the different places in this css:

1

2

3

4

#box_1, #box_2 {position:relative; width:300px; height:100px; margin:10px; background:#ddd;}   

#a, #b {position:absolute; width:100px; height:300px; }   

#a {background:#c00; z-index:100; }   

#b {background:#0c0; z-index:1; left:50px;}

Copy after login

at this time we see, no matter #a is set to no matter how high the value is, it cannot exceed #b, so z-index cannot break through the level of html. it must be at the same level before it can exert its power. so how to solve this problem? i can think about it the other way around. , the order between cousins ​​cannot be reorganized, why not reorganize the level of the parents? so we add a z-index: 100; to the css of #box_1 and z-index: 1; to the css of #box_2. let’s take a look at the effect again:

1

2

3

4

5

6

#box_1, #box_2 {position:relative; width:300px; height:100px; margin:10px; background:#ddd;}   

#box_1 {z-index:100;}   

#box_2 {z-index:1;}   

#a, #b {position:absolute; width:100px; height:300px; }   

#a {background:#c00; }   

#b {background:#0c0; left:50px;}

Copy after login
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)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
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)

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

How to view the date of bootstrap How to view the date of bootstrap Apr 07, 2025 pm 03:03 PM

Answer: You can use the date picker component of Bootstrap to view dates in the page. Steps: Introduce the Bootstrap framework. Create a date selector input box in HTML. Bootstrap will automatically add styles to the selector. Use JavaScript to get the selected date.

See all articles