search
HomeWeb Front-endCSS TutorialHow to set up css hierarchical relationship

In CSS, you can use the z-index attribute to set the level. This attribute can set the stacking order of elements. Elements with a higher stacking order will always be in front of elements with a lower stacking order; syntax Format "z-index:numeric;", negative values ​​are allowed.

How to set up css hierarchical relationship

The operating environment of this tutorial: Windows 7 system, CSS3&&HTML5 version, Dell G3 computer.

In css, you can use the z-index attribute to set the level.

z-index attribute sets the stacking order of elements. Elements with a higher stacking order will always appear in front of elements with a lower stacking order. (Z-index only works on positioned elements).

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.

Simple demonstration

<div style="width:200px;height:200px;"></div>
    <div style="position:relative; top:-50px; width:100px;height:100px;"><div>

Two DIVs, the second one moves up 50px, the normal situation should be like this

How to set up css hierarchical relationship

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

<div></div>
    <div><div>
<p>The result will look like this. The simplest application of z-index is like this</p>
<p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/024/1d996561d6b4fe03194a5cc37c8b4c04-1.png?x-oss-process=image/resize,p_40" class="lazy" title="How to set up css hierarchical relationship" alt="How to set up css hierarchical relationship"    style="max-width:90%"  style="max-width:90%" border="0"> </p>
<h3 id="Only-valid-for-positioned-elements">Only valid for positioned elements</h3>
<p>The z-index attribute is applicable to positioned elements (objects whose position attribute value is relative or absolute or fixed), used to determine whether the positioned element is perpendicular to the display screen The stacking order in the direction (called the Z axis), that is to say, if the element is not positioned, the z-index set for it will be invalid. </p><pre class='brush:php;toolbar:false;'><div style="width:200px;height:200px;z-index:30"></div>
<div style="position:relative; top:-50px; width:100px;height:100px;z-index:10;"><div>

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

How to set up css hierarchical relationship

Who goes up and who goes down with the same z-index

There are actually two situations for the same z-index

1. If two If neither element is positioned and overlaps in position, or two elements have been positioned and have the same z-index and overlap in position, then according to the document flow order, the later ones will overwrite the previous ones.

<div style="position:relative;width:200px;height:200px;"></div>
<div style="position:relative; top:-50px; width:100px;height:100px;"><div>

How to set up css hierarchical relationship

#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;"></div>
<div style=" width:100px;height:100px;"><div>

How to set up css hierarchical relationship

Parent-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 or not, and will be above the parent element

<div style="position:relative;width:200px;height:200px;z-index:10;">
        <div style="position:relative;width:100px;height:100px;z-index:-5;"><div>
    </div>

Although the child element sets the z-index smaller than the parent element, the child element still appears above the parent element

How to set up css hierarchical relationship

If the parent element z-index fails ( Not positioned or using the default value), then the z-index setting of the positioned child element takes effect

<div>
        <div>
<div>
</div>
<p>The child element z-index=-5 takes effect and is overridden by the parent element</p>
<p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/024/eb75caa902e9aae6dbbb5eb9ed1d19eb-6.png?x-oss-process=image/resize,p_40" class="lazy" title="How to set up css hierarchical relationship" alt="How to set up css hierarchical relationship"    style="max-width:90%"  style="max-width:90%" border="0"></p>
<h3 id="Child-elements-between-brothers">Child elements between brothers</h3>
<p>If the z-index of the brother element takes effect, then the coverage relationship of its child elements is determined by the parent element</p><pre class='brush:php;toolbar:false;'><div style="position:relative;width:100px;height:100px;z-index:5;">
        <div style="position:relative;width:50px;height:250px;z-index:50;"></div>
    </div>

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

Although the child elements of the first div The z-index is relatively high, but because its parent element has a lower z-index than the second div, the first div child element will be covered by the second div and its child elements

How to set up css hierarchical relationship

(Learning video sharing: css video tutorial)

The above is the detailed content of How to set up css hierarchical relationship. For more information, please follow other related articles on the PHP Chinese website!

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
So Many Color LinksSo Many Color LinksApr 13, 2025 am 11:36 AM

There's been a run of tools, articles, and resources about color lately. Please allow me to close a few tabs by rounding them up here for your enjoyment.

How Auto Margins Work in FlexboxHow Auto Margins Work in FlexboxApr 13, 2025 am 11:35 AM

Robin has covered this before, but I've heard some confusion about it in the past few weeks and saw another person take a stab at explaining it, and I wanted

Moving Rainbow UnderlinesMoving Rainbow UnderlinesApr 13, 2025 am 11:27 AM

I absolutely love the design of the Sandwich site. Among many beautiful features are these headlines with rainbow underlines that move as you scroll. It's not

New Year, New Job? Let's Make a Grid-Powered Resume!New Year, New Job? Let's Make a Grid-Powered Resume!Apr 13, 2025 am 11:26 AM

Many popular resume designs are making the most of the available page space by laying sections out in a grid shape. Let’s use CSS Grid to create a layout that

One Way to Break Users Out of the Habit of Reloading Too MuchOne Way to Break Users Out of the Habit of Reloading Too MuchApr 13, 2025 am 11:25 AM

Page reloads are a thing. Sometimes we refresh a page when we think it’s unresponsive, or believe that new content is available. Sometimes we’re just mad at

Domain-Driven Design With ReactDomain-Driven Design With ReactApr 13, 2025 am 11:22 AM

There is very little guidance on how to organize front-end applications in the world of React. (Just move files around until it “feels right,” lol). The truth

Detecting Inactive UsersDetecting Inactive UsersApr 13, 2025 am 11:08 AM

Most of the time you don’t really care about whether a user is actively engaged or temporarily inactive on your application. Inactive, meaning, perhaps they

Wufoo   ZapierWufoo ZapierApr 13, 2025 am 11:02 AM

Wufoo has always been great with integrations. They have integrations with specific apps, like Campaign Monitor, Mailchimp, and Typekit, but they also

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)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

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

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

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.