search
HomeWeb Front-endCSS TutorialSolution to the level problem that relative absolute cannot break through_Experience exchange

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:

<ul>   
<li>第一块</li>   
<li><span>第二块</span></li>   
<li>第三块</li>   
<li>第四块</li>   
<li>第五块</li>   
</ul>

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.

*{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;}

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:

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

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

*{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;}

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:

*{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; }

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:

<ul>   
<li><span>第一块</span></li>   
<li><span>第二块</span></li>   
<li><span>第三块</span></li>   
<li><span>第四块</span></li>   
<li><span>第五块</span></li>   
</ul>

the css can be changed to this:

*{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; }

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:

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

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

#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; }

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:

<p id="box_1">   
<p id="a">这是第一个块</p>   
</p>   
<p id="box_2">   
<p id="b">这是第二个块</p>   
</p>

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

#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;}

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:

#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;}
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
Demonstrating Reusable React Components in a FormDemonstrating Reusable React Components in a FormApr 16, 2025 am 10:36 AM

Components are the building blocks of React applications. It’s almost impossible to build a React application and not make use of components. It’s widespread

Filtering Data Client-Side: Comparing CSS, jQuery, and ReactFiltering Data Client-Side: Comparing CSS, jQuery, and ReactApr 16, 2025 am 10:35 AM

Say you have a list of 100 names:

Zero hands up.Zero hands up.Apr 16, 2025 am 10:34 AM

Asked an entire room full of webdevs yesterday if any of them knew that FF/Chrome/Opera/Brave/etc. for iOS weren't allowed to compete on engine quality.

Variable Fonts Link Dump!Variable Fonts Link Dump!Apr 16, 2025 am 10:22 AM

There's been a ton of great stuff flying around about variable fonts lately (our tag has loads of stuff as well). I thought I'd round up all the new stuff I

Link Underlines That Animate Into Block BackgroundsLink Underlines That Animate Into Block BackgroundsApr 16, 2025 am 10:14 AM

It's a cool little effect. The default link style has an underline (which is a good idea) and then on :hover you see the underline essentially thicken up

Preloading Pages Just Before They are NeededPreloading Pages Just Before They are NeededApr 16, 2025 am 09:53 AM

The typical journey for a person browsing a website: view a page, click a link, browser loads new page. That's assuming no funny business like a Single Page

Adaptive Photo Layout with FlexboxAdaptive Photo Layout with FlexboxApr 16, 2025 am 09:51 AM

Let’s take a look at a super lightweight way to create a horizontal masonry effect for a set of arbitrarily-sized photos. Throw any set of photos at it, and

The Many Ways to Link Up Shapes and Images with HTML and CSSThe Many Ways to Link Up Shapes and Images with HTML and CSSApr 16, 2025 am 09:45 AM

Different website designs often call for a shape other than a square or rectangle to respond to a click event. Perhaps your site has some kind of tilted or

See all articles

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

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),