惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 【当耐特】
WordPress大学
WordPress大学
T
The Exploit Database - CXSecurity.com
博客园_首页
MyScale Blog
MyScale Blog
The Cloudflare Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
美团技术团队
Stack Overflow Blog
Stack Overflow Blog
博客园 - 聂微东
M
MIT News - Artificial intelligence
Microsoft Security Blog
Microsoft Security Blog
F
Full Disclosure
V
V2EX
博客园 - Franky
博客园 - 三生石上(FineUI控件)
Hugging Face - Blog
Hugging Face - Blog
P
Proofpoint News Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
SecWiki News
SecWiki News
N
Netflix TechBlog - Medium
S
Secure Thoughts
酷 壳 – CoolShell
酷 壳 – CoolShell
Hacker News: Ask HN
Hacker News: Ask HN
爱范儿
爱范儿
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Webroot Blog
Webroot Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Martin Fowler
Martin Fowler
PCI Perspectives
PCI Perspectives
S
Security @ Cisco Blogs
Recorded Future
Recorded Future
Help Net Security
Help Net Security
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
AI
AI
Microsoft Azure Blog
Microsoft Azure Blog
K
Kaspersky official blog
G
GRAHAM CLULEY
H
Hackread – Cybersecurity News, Data Breaches, AI and More
C
CERT Recently Published Vulnerability Notes
U
Unit 42
T
Tor Project blog
Cloudbric
Cloudbric
Hacker News - Newest:
Hacker News - Newest: "LLM"
MongoDB | Blog
MongoDB | Blog
GbyAI
GbyAI
T
The Blog of Author Tim Ferriss
Security Latest
Security Latest
N
News and Events Feed by Topic
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO

Ahmad Shadeed

Better fluid sizing with round() Use Cases for Field Sizing The Basics of Anchor Positioning Item Flow CSS Relative Colors Balancing Text In CSS Should masonry be part of CSS grid? CSS display contents CSS Grid Areas CSS Cap Unit An Interactive Guide to CSS Container Queries CSS :has() Interactive Guide CSS Nesting UX in DevTools CSS Nesting Future CSS: State Container Queries Rebuilding a comment component with modern CSS Conditional CSS with :has and :nth-last-child CSS Text balancing with text-wrap:balance CSS Masking Do we need CSS flex-wrap detection? My CSS Wishlist Conditional CSS CSS Style Queries Inside the mind of a frontend developer: Article layout Inside the mind of a frontend developer: Hero section CSS container queries are finally here The CSS behind Figma First Look At The CSS object-view-box Property Learn CSS Subgrid CSS :has Parent Selector Aligning Content In Different Wrappers Flexbox Dynamic Line Separator Hello, CSS Cascade Layers Building UI Components With SVG and CSS A Deep CSS Dive Into Radial And Conic Gradients Defensive CSS Building Real-life Components: Facebook Messenger Conditional Border Radius In CSS CSS Container Query Units Aligning a Button Label Vertically Comparing Design Mockups To Code Result Using HSL Colors In CSS Custom Scrollbars In CSS Let CSS Container Queries For Designers The State of CSS Cross-Browser Development Overflow Issues In CSS Inspect Element As A Way To Increase Your Curiosity Handling Text Over Images in CSS Digging Into CSS Logical Properties Clipping Scrollable Areas On The inline-start Side Understanding Clip Path in CSS The Art of Building Real-life Components Handling Short And Long Content In CSS CSS Scroll Snap A Deep Dive Into CSS Grid minmax() CSS Variables 101 Finding The Root Cause of a CSS Bug Learn CSS centering How to detect browser support for Flexbox Gap CSS Mistakes While On Autopilot Digging Into the Flex Property Understanding CSS Multiple Backgrounds Aligning Logo Images in CSS Grid for layout, Flexbox for components Colors in CSS Thinking About The In-between Design Cases min(), max(), and clamp() CSS Functions Image Techniques On The Web Everything About Auto in CSS Learn Box Alignment Let Learn CSS Positioning Intrinsic Sizing In CSS CSS Grid Template Areas In Action Hiding Elements On The Web Creating a Variable Color Font From Scratch Building a Football Ticket With CSS and SVG Blending Modes in CSS CSS Variables With Inline Styles Implementing Dark Mode For My Website Rebuilding Apple Music Header in HTML & CSS Accessible Checkbox Layout Flickering On Browser Resize Enhancing The Clickable Area Size Custom Underlines with SVG Part 3: The Process of Implementing A UI Design From Scratch Part 2: The Process of Implementing A UI Design From Scratch Building An Old Nav Design CSS Flexbox: 5 Real World Use Cases I Used CSS Inline Flex For The First Time The Process of Implementing A UI Design From Scratch Common CSS Issues For Front-End Projects Handling Long and Unexpected Content in CSS How to Build Web Form Layouts With CSS Grid Grid Layout Ah-ha Moment Enhancing Our Components with CSS :empty Building Resizeable Components with Relative CSS Units CSS Writing Mode The Journey of Learning Front End Web Development on a Daily Basis
Understanding Z-Index in CSS
Ahmad Shadeed · 2021-02-16 · via Ahmad Shadeed

What is Z-Index?

The z-index property defines the order of the elements on the z-axis. Basically, it works on elements with a position value other than static.

The duck and the ring are within the same parent (the water). Can you guess which will come in front of the other?

<div class="water">
  <div class="duck"></div>
  <div class="ring"></div>
</div>

The ring has moved a bit. Notice how it’s in top of the duck. This is because latter elements in the HTML are displayed in front of former ones.

.ring {
  margin-top: -28px;
}

To make the duck appear in front of the ring, we need to apply position: relative to it.

.duck {
    position: relative;
}

Positioned elements appear in front of non-positioned ones.

How stacking works

Let’s imagine that we have a scene represented in the following HTML markup.

<div class="pool">
    <img class="kid" src="kid.svg"/>
    <img class="ring" src="ring.svg"/>
    <img class="water" src="water.svg"/>
</div>

How can we make them stack on top of each other like a real-life scene?

I want to make the images overlap with each other, so I added a negative margin.

img {
    margin-top: -40px;
}

Notice that the water is on top of the ring, and the ring is on top of the boy. Why is that happening?

By default, latter elements in the HTML code will appear in front of former elements.

To arrange them, we need to add position:relative and z-index to the first two items (The kid and the ring).

img {
    margin-top: -75px;
}

.kid {
    position: relative;
    z-index: 2;
}

.ring {
    position: relative;
    z-index: 1;
}

Elements with a higher z-index will be in front of an element that has a smaller one.

Negative Z-Index

It’s possible to use a negative value for z-index. Let’s learn how it works.

<div class="water">
    <div class="duck"></div>
    <div class="ring"></div>
</div>

The duck was able to add z-index: -1 to itself, and it’s hidden. Can you guess where?

.duck {
    position: relative;
    z-index: -1;
}

In our case, it’s below the parent (the water).

Based on how the source order works an element with a negative z-index should be above the background of its parent.

Adding position: relative and a z-index other than auto will prevent the duck from going under its parent.

.water {
    position: relative;
    z-index: 1;
}

.duck {
    position: relative;
    z-index: -1;
}

Now, the duck can’t go behind its parent even if you add z-index: -9999 to it. Why? Because the duck is a part of the water stacking context.

Let’s learn about stacking contexts.

Stacking contexts

It’s a parent that contains one or multiple elements. Those elements will be stacked according to the source order rules, and their z-index values only have meaning within their parent.

<div class="water">
  <div class="ring"></div>
</div>

To create a stacking context for the water and the ring, you should use a position value other than static, and a z-index value other than auto.

.water {
  position: relative;
  z-index: 1;
}

.ring {
  position: relative;
  z-index: 1;
}

I like to imagine stacking context as a folder directory.

Note that a stacking context can contian other stacking contexts (AKA nesting).

Water

Ring

Duck

Ring

Duck

Mosquito

What creates a new stacking context?

It’s not necessary to use z-index and positioning to create a stacking contexts. Here is a list of what triggers a stacking context.

Let’s explore a few examples on triggering a new stacking context(s).

  1. An element with a position value other than static and z-index other than auto.

  2. An element that is a child of a flex container with z-index other than auto.

  3. An element with opacity other than 1.
  4. An element that has a transform other than none. And more. View the full list on MDN.

Opacity

In the following example, we have a ring, and we want the duck to be in front of it.

<div class="water">
  <div class="duck"></div>
  <div class="ring ring-purple"></div>
</div>

Adding an opacity other than 1 creates a new stacking context.

.duck {
  opacity: 0.999;
}

I added an opacity that is less than 1 to the duck, and it’s done. The duck is in front of the two rings.

CSS transforms

I want to move the duck in front of the purple ring. How can I do this?

<div class="water">
  <div class="ring ring-red"></div>
  <div class="duck"></div>
  <div class="ring ring-purple"></div>
</div>

By rotating the duck, the brower will create a new stacking context for the duck. As a result, it will appear in front of the purple ring.

.duck {
  transform: rotate(10deg);
}

Clip Path

Applying clip-path to an element will create a new stacking context.

<div class="water">
  <div class="ring ring-purple"></div>
  <div class="duck"></div>
</div>

Adding a clip-path created a new stacking context. As a result, the purple ring is in front of the duck.

.ring-purple {
  clip-path: polygon(
    50% 0%,
    100% 50%,
    50% 100%,
    0% 50%
  );
}

Learn more about CSS clip path in this article.

To make the duck appear in front of the ring, we need to add a stacking context to it with a z-index other than auto.

.ring-purple {
  clip-path: polygon(
    50% 0%,
    100% 50%,
    50% 100%,
    0% 50%
  );
}

.duck {
  position: relative;
  z-index: 1;
}

Since the duck is a positioned element with z-index other than auto, it will be in front of the ring.

Let’s learn about how the default source order works.

How the default source order works

The children of a stacking context are stacked from bottom to top in the following order.

The list has been edited For simplicity’s sake. If you’re looking for the full stack order, please read the spec.

  • Child element(s) with negative z-index values
  • Non-positioned elements
  • Positioned elements with z-index: auto
  • Positioned elements with z-index: 1 or higher

Stacking contexts examples

Example 1: The duck and the mosquito

In the following figure, we have three elements. The water, the red ring, and the purple ring.

We want to position the duck above the mosquito.

<div class="water">
  <div class="ring ring-red">
    <div class="duck"></div>
    <div class="mosquito"></div>
  </div>
  <div class="ring ring-purple"></div>
</div>

Consider that the water has a stacking context.

.water {
  position: relative;
  z-index: 1;
}

I added z-index: 1 to the duck to make it above the mosquito.

.duck {
  position: relative;
  z-index: 1;
}

The duck is now above the mosquito and its happy. However, it appears above the purple ring. Why did that happen?

Since the purple ring is in the same stacking context as the red ring, but has no z-index, it appears below the duck.

To solve this, we need to introduce a stacking context to the red ring.

To create a stacking context for the red ring, I will add a CSS position and z-index: 1 to it.

.ring-red {
  position: relative;
  z-index: 1;
}

Another solution would be to add a z-index: -1 to the purple ring. This will make it behind the red ring.

Example 2: Feeding the duck

The duck is very hungry and it’s stuck inside the red ring since the red ring has a stacking context.

How can we make the fish closer to the duck, so it can eat?

<div class="water">
  <div class="ring ring-red">
    <div class="duck"></div>
  </div>
  <div class="fish fish-small"></div>
  <div class="fish fish-large"></div>
</div>
.water {
  position: relative;
  z-index: 1;
}

.ring-red {
  transform: rotate(90deg);
}

By removing the stacking context from the ring, the fish will be in front of the ring. That way, the duck can eat.

Based on the source order, the fish should come first since it’s the last element in the HTML.

.water {
  position: relative;
  z-index: 1;
}

.ring-red {
  transform: none;
}

Example 3: z-index: 9999

In this example, the fish has a higher z-index than the red ring.

<div class="water">
  <div class="ring ring-red">
    <div class="duck"></div>
  </div>
  <div class="fish fish-large"></div>
</div>
.water {
  position: relative;
  z-index: 1;
}

.fish {
  position: relative;
  z-index: 2;
}

.ring-red {
  position: relative;
  z-index: 1;
}

How can we move the duck in front of the fish? It’s impossible. If we apply a z-index to the duck, it will only have meaning within its stacking context (the red ring). Even with a z-index: 9999, it won’t do anything.

.duck {
  position: relative;
  z-index: 9999;
}

What’s tricky about z-index is that it only works within an element’s stacking context. In our case, we want the duck, which is a part of the red ring stacking context, to be in front of the fish (Another stacking context). This isn’t possible.

To make the duck appear in front of the fish, we need to remove the stacking context from the red ring.

.fish {
  position: relative;
  z-index: 2;
}

.duck {
  position: relative;
  z-index: 1;
}

.ring-red {
  position: static;
  z-index: auto;
}

By doing this, the current stacking context for the duck and the fish is the water element.

Further reading

I wrote a book

An ebook that will help you improve your debugging CSS skills and reduce the time you spend on bugs by showing proven methods and techniques.

If you’re interested, head over to debuggingcss.com for a free preview.