search
HomeWeb Front-endCSS TutorialIn-depth analysis of css z-index (with examples)

In-depth analysis of css z-index (with examples)

Students who have done page layout should be very familiar with the z-index attribute. Z-index is a special attribute for web page display. Because the pattern displayed by the monitor is a two-dimensional plane, it has an x-axis and a y-axis to represent the position attribute. In order to represent the three-dimensional concept such as the superposition order of the upper and lower layers of display elements, the z-index attribute is introduced to represent the difference in the z-axis. Indicates the upper and lower three-dimensional relationship of an element in the superposition order.

Elements with a larger z-index value will be superimposed on elements with a smaller z-index value. For positioned objects that do not specify this property, objects with positive z-index values ​​are above them and objects with negative z-index values ​​are below them. (Recommended tutorial: CSS video tutorial)

Simple demonstration

<div style="width:200px;height:200px;background-color:#0e0;"></div>
    <div style="position:relative; top:-50px; width:100px;height:100px;background-color:#00e;"><div>

Two p, the second one moves up 50px, the normal situation should be Like this

In-depth analysis of css z-index (with examples)

The second p covers the first p, and adding the z-index attribute to the second one

<div style="width:200px;height:200px;background-color:#0e0;"></div>
    <div style="position:relative; top:-50px; width:100px;height:100px;background-color:#00e;z-index:-5;"><div>

The result will become Like this, the simplest application of z-index is like this

In-depth analysis of css z-index (with examples)

Only valid for positioned elements

The z-index attribute is applicable to Positioned elements (objects whose position attribute value is relative or absolute or fixed) are used to determine the stacking order of positioned elements in the direction perpendicular to the display screen (called the Z axis). That is to say, if the element is not positioned, it The set z-index will be invalid.

<div></div>
<div><div>
<p>Although the z-index of the first p is larger than that of the second p, because the first p is not positioned and its z-index attribute does not work, it will still be covered by the second p. . </p>
<p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/024/1e4fe8e9378310c98376898c90b4454c-2.png?x-oss-process=image/resize,p_40" class="lazy"   style="max-width:90%" title="In-depth analysis of css z-index (with examples)" alt="In-depth analysis of css z-index (with examples)"  style="max-width:90%"  style="max-width:90%" border="0"></p>
<h3 id="strong-Who-goes-up-and-who-goes-down-for-the-same-z-index-strong"><strong>Who goes up and who goes down for the same z-index</strong></h3>
<p>There are actually two situations for the same z-index</p>
<p>1. If neither element is positioned and the position overlaps, or both elements are positioned and the z-index is the same, the position of the element overlaps, then according to the document flow order, the later one will overwrite the previous one. </p><pre class='brush:php;toolbar:false;'><div style="position:relative;width:200px;height:200px;background-color:#0e0;"></div>
<div style="position:relative; top:-50px; width:100px;height:100px;background-color:#00e;"><div>

In-depth analysis of css z-index (with examples)

#2. If neither element has z-index set, the default value is used, one is positioned and the other is not positioned, then the positioned element covers the unpositioned element

<div style="position:relative;top:50px;width:200px;height:200px;background-color:#0e0;"></div>
<div style=" width:100px;height:100px;background-color:#00e;"><div>

In-depth analysis of css z-index (with examples)

Father-child relationship processing

If the z-index of the parent element is valid, then the child element will be consistent with the parent element regardless of whether the z-index is set. Will be above the parent element

<div>
        <div>
<div>
    </div>
<p>Although the child element sets the z-index smaller than the parent element, the child element still appears above the parent element</p>
<p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/024/1e4fe8e9378310c98376898c90b4454c-5.png?x-oss-process=image/resize,p_40" class="lazy"   style="max-width:90%" title="In-depth analysis of css z-index (with examples)" alt="In-depth analysis of css z-index (with examples)"  style="max-width:90%"  style="max-width:90%" border="0"></p>
<p>If the parent The z-index of the element is invalid (not positioned or using the default value), then the z-index setting of the positioned child element takes effect</p><pre class='brush:php;toolbar:false;'><div style="position:relative;width:200px;height:200px;background-color:#0e0;">
        <div style="position:relative;width:100px;height:100px;background-color:#00e;z-index:-5;"><div>
</div>

The z-index=-5 of the child element takes effect and is overwritten by the parent element

In-depth analysis of css z-index (with examples)

Child elements between brothers

If the z-index of the sibling element is effective, then the coverage relationship of its child elements is determined by the parent element

<div style="position:relative;width:100px;height:100px;background-color:#0e0;z-index:5;">
        <div style="position:relative;width:50px;height:250px;background-color:#00e;z-index:50;"></div>
    </div>

    <div style="position:relative;width:100px;height:100px;background-color:#0e0;z-index:10;margin-top:4px;">
        <div style="position:relative;width:30px;height:150px;background-color:#e0e;z-index:-10;"></div>
    </div>

Although the z-index of the child element of the first p is relatively high, because its parent element has a lower z-index than the second p, the child element of the first p will be replaced by the second p and its child elements. Overwriting

In-depth analysis of css z-index (with examples)

Application

There is often such a mistake. In the last row of the table, put a td and a p, and click to pop up the subtitles. The menu does some operations such as deletion and modification, but every time the pop-up menu is covered by p in the following lines, like the picture below, the pop-up menu is not at the top of the page.

In-depth analysis of css z-index (with examples)

写个简单的例子看看

nbsp;html>

    
        <title>Test</title>
        <style>
            html,body
            {
                height:100%;
                width:100%;
                padding:0;
                margin:0;
            }
            
            .menu
            {
                background-color:#0e0;
                position:relative;
                z-index:10;
            }
            
            .options
            {
                display:none;
                position:absolute;
                top:
                z-index:30;
            }
            
            .options div
            {
                background-color:#00e;
            }
        </style>
    
    
    
                                                                                                                                                                                                                                        
NameAgeOptions
Byron24                 
                    
Options
                    
                        
Opion1
                        
Opion2
                        
Opion3
                        
Opion4
                    
                
            
Byron24                 
                    
Options
                    
                        
Opion1
                        
Opion2
                        
Opion3
                        
Opion4
                    
                
            
Byron24                 
                    
Options
                    
                        
Opion1
                        
Opion2
                        
Opion3
                        
Opion4
                    
                
            
    
期望样式 In-depth analysis of css z-index (with examples) 实际样式 In-depth analysis of css z-index (with examples)

这时候习惯于增大options 的z-index却发现于事无补,为什么呢?因为每个menu的z-index相同,它们的层叠顺序按文档流顺序,无论子元素z-index调到多大,上面menu的options还是会被下面menu遮盖。这时候我的做法一般是把options放到外面,所有的menu用一个,使menu与options没有父子关系,或者干脆在点击menu的时候把它的z-index调大,这样其子元素就不会被遮盖住了。

最后

本文的例子都是以符合W3C的Chrome浏览器做验证,但在IE6,7 z-index的默认值并不是auto而是0,这样会导致很多奇怪现象,这时候就需要考虑这点了。

更多编程相关知识,请访问:编程入门!!

The above is the detailed content of In-depth analysis of css z-index (with examples). For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:博客园. If there is any infringement, please contact admin@php.cn delete
利用CSS怎么创建渐变色边框?5种方法分享利用CSS怎么创建渐变色边框?5种方法分享Oct 13, 2021 am 10:19 AM

利用CSS怎么创建渐变色边框?下面本篇文章给大家分享CSS实现渐变色边框的5种方法,希望对大家有所帮助!

css ul标签怎么去掉圆点css ul标签怎么去掉圆点Apr 25, 2022 pm 05:55 PM

在css中,可用list-style-type属性来去掉ul的圆点标记,语法为“ul{list-style-type:none}”;list-style-type属性可设置列表项标记的类型,当值为“none”可不定义标记,也可去除已有标记。

css与xml的区别是什么css与xml的区别是什么Apr 24, 2022 am 11:21 AM

区别是:css是层叠样式表单,是将样式信息与网页内容分离的一种标记语言,主要用来设计网页的样式,还可以对网页各元素进行格式化;xml是可扩展标记语言,是一种数据存储语言,用于使用简单的标记描述数据,将文档分成许多部件并对这些部件加以标识。

css3怎么实现鼠标隐藏效果css3怎么实现鼠标隐藏效果Apr 27, 2022 pm 05:20 PM

在css中,可以利用cursor属性实现鼠标隐藏效果,该属性用于定义鼠标指针放在一个元素边界范围内时所用的光标形状,当属性值设置为none时,就可以实现鼠标隐藏效果,语法为“元素{cursor:none}”。

rtl在css是什么意思rtl在css是什么意思Apr 24, 2022 am 11:07 AM

在css中,rtl是“right-to-left”的缩写,是从右往左的意思,指的是内联内容从右往左依次排布,是direction属性的一个属性值;该属性规定了文本的方向和书写方向,语法为“元素{direction:rtl}”。

css怎么实现英文小写转为大写css怎么实现英文小写转为大写Apr 25, 2022 pm 06:35 PM

转换方法:1、给英文元素添加“text-transform: uppercase;”样式,可将所有的英文字母都变成大写;2、给英文元素添加“text-transform:capitalize;”样式,可将英文文本中每个单词的首字母变为大写。

css怎么设置i不是斜体css怎么设置i不是斜体Apr 20, 2022 am 10:36 AM

在css中,可以利用“font-style”属性设置i元素不是斜体样式,该属性用于指定文本的字体样式,当属性值设置为“normal”时,会显示元素的标准字体样式,语法为“i元素{font-style:normal}”。

怎么设置rotate在css3的旋转中心点怎么设置rotate在css3的旋转中心点Apr 24, 2022 am 10:50 AM

在css3中,可以用“transform-origin”属性设置rotate的旋转中心点,该属性可更改转换元素的位置,第一个参数设置x轴的旋转位置,第二个参数设置y轴旋转位置,语法为“transform-origin:x轴位置 y轴位置”。

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)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)