iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
💤

3 Guidelines for Managing z-index in CSS

に公開

When it comes to CSS for controlling the overlapping of elements, z-index is the primary property, but it's a bit quirky because higher values don't always mean an element will appear on top. Overlapping on websites is governed by a mechanism called the stacking context. Due to this relationship, a z-index of 1 can sometimes appear above a z-index of 9999.

The relationship between z-index and stacking contexts is explained in the following article:

https://ics.media/entry/200609/

In this article, I will introduce my own principles for managing z-index.

Global Overlapping and Local Overlapping

Overlapping on a website can be broadly categorized into two types: those you want to manage across the entire page, and those you want to manage within a specific area. In this article, I will refer to the former as global overlapping and the latter as local overlapping.

Global overlapping includes elements like sticky headers, modals, hamburger menus, and toast notifications. In most cases, these are elements you want to display on top of (or underneath) the page content area.

Local overlapping refers to the stacking relationship between specific elements, such as a notification icon on an avatar or text overlaying a photo. Since the focus is strictly on the relationship between specific elements, the overlap with other unrelated elements generally doesn't matter.

Based on these characteristics, I have established principles for managing z-index.

Principle 1: Contain Local Overlapping with z-index: 0

One common failure in stacking order is when an element intended for local overlapping ends up on top of global overlapping elements. This happens because they are in the same stacking context as the global elements. Local overlapping does not need to exist within the same stacking context as global elements.

To address this, you can create a new stacking context by applying z-index: 0 to an element with position: relative that serves as the base for the local overlap. This confines the local overlapping within that specific stacking context, preventing it from affecting the global stacking order. Within a closed stacking context, even if you set z-index: 9999, the stacking relationship will not influence anything outside of it.

Principle 2: Place Global Overlapping Elements Directly Under or Close to the <body> Tag

There are times when an element intended for global overlapping doesn't appear on top as expected. This happens because, contrary to Principle 1, it is trapped within an inappropriate stacking context. To achieve global overlapping, an element needs to be in the global stacking context. If it's nested deep within the hierarchy, there's a risk of it being unexpectedly confined to a local stacking context due to its parent elements.

To prevent this, place elements intended for global overlapping as direct children of the <body> tag or as close to that level as possible. If you are using React or Vue.js, you can easily achieve this by using a feature like Portal.

Elements such as modals and hamburger buttons should be placed close to the body tag. In particular, if a hamburger button is placed inside a header element, it might be trapped within the header's context (e.g., a fixed header). In such cases, trying to create a layer that is "above the header but below the hamburger button" becomes difficult due to stacking context relationships. Placing the hamburger button independently of the header element is one way to handle this.

Principle 3: Use Variables for z-index Values

While Principles 1 and 2 focused primarily on controlling the stacking context, this principle focuses on management within the same stacking context.

The order of overlapping within the same stacking context is determined by the magnitude of the z-index value. We manage these values using variables. Managing with variables offers the following advantages:

  • Giving meaning to values (Semantics)
  • Centralizing value management
  • Enabling flexible handling using calc()

Let's explain each of these.

Giving Meaning to Values

For example, even if a z-index value is 2, it doesn't carry much meaning beyond being above z-index: 1 and below 3. Furthermore, when multiple elements have the same value within the same stacking context, the order is determined by their appearance in the HTML, so the value itself doesn't strictly define the overlapping relationship between elements.

Therefore, by turning z-index values into variables like --header-z-index: 100, you can give meaning to the values. When z-index values carry meaning and become semantic, the positional relationships become more intuitive in the code.

z-index: var(--header-z-index); 

Looking at the code, it becomes clear that this z-index value represents the height of the header.

Centralizing Value Management

By using variables, you can manage the z-index values for various locations in a single place.

:root {
  --header-z-index: 100;
  --navigation-z-index: 200;
  --modal-z-index: 500;
  --local-z-index: 0;
}

In particular, managing global overlapping values in one place makes the hierarchy obvious at a glance. It's easier to notice unexpected mistakes in the stacking order when changing values, and you don't have to jump back and forth between different files.

Enabling Calculations with the calc() Function

Local overlapping can be managed clearly using variables and the calc() function. Specifically, it looks like this (where the value of local-z-index is 0):

.wrapper {
  position: relative;
  z-index: var(--local-z-index);
}

.item1 {
  position: absolute;
  z-index: calc(var(--local-z-index) + 1);
}

.item2 {
  position: absolute;
  z-index: calc(var(--local-z-index) + 2);
}

The benefit of using the calc() function and variables is that it clearly shows the context is a locally closed stacking context, and you can set the stacking order through addition.

Local stacking contexts tend to have more intricate overlapping relationships compared to global ones. It would be tedious to prepare a z-index variable for every single instance, and variable name duplicates might occur. This approach allows you to manage these relationships while keeping the impact local.

Summary

The z-index specification is complex, and sometimes the stacking order can fall apart before you know it. While the three principles introduced in this article are not a complete solution, they should help prevent z-index management from breaking down. Let's get comfortable with z-index and learn to control overlapping relationships effectively.

Discussion