You must know about principle of z-index
Published: 2024-07-10 16:46:23

## **Introduction**

Hi, I'm (flitter)[https://flitter.dev] maintainer and recently I provide z-index widget. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ru1rp878soehm61vnhoh.png)

This article explores the workings of the z-index property and how to implement it within SVGs.

### **Intended Audience**

- Those struggling with z-index not applying as expected.
- Anyone unfamiliar with the concept of stacking context.
- Individuals looking to implement z-index directly in SVG.
- People interested in adding z-index behavior to charts or diagrams.

### **Keywords**

- [z-index](https://developer.mozilla.org/en-US/docs/Web/CSS/z-index)
- [stacking context](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_positioned_layout/Understanding_z_index/The_stacking_context)

# **What is Z-Index?**

The z-index is a CSS property that adjusts the vertical stacking order of elements. Its default value is **`auto`**, equivalent to **`z-index: 0`**. However, **`z-index: 0`** creates a new stacking context, altering the precedence for child elements differently from **`z-index: auto`**. A stacking context will be explained further below.

### **Typical Z Order**

In the absence of a specified z-index, the vertical order of stack elements matches the order in which DOM nodes are rendered. This ordering is consistent with a Depth-First Search (DFS), where elements rendered later take precedence over those rendered earlier. For example, a pink box drawn over an orange box is higher in the stack..

![image-1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mt5a46d9omist0unkbc6.png)

### **Adding Z Elements**

Assigning a z-index of 9999 to a purple circle places it at the top, as shown in the illustration. Despite the pink box being the last reach in a depth-first search, the z-index rearrangement prioritizes the purple circle to be drawn last.


![example-2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lr6pin0fximwkryvs2k6.png)


# **What is a Stacking Context?**

Stacking context, along with an element's z-index, determines its vertical stack priority. Simply put, think of a stacking context as an array of a parent's z-index values. Even if a child element has a high z-index, it can still be placed lower in priority due to its stacking context.


![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1qb8q0o3qyx0f75jg2mv.png)


As shown in the illustration above, despite the purple circle having a z-index of 9999, it is obscured by the pink box below because it belongs to a new stacking context created by its parent's z-index of 0.

- Situations Creating Stacking Context
    
    Stacking context can form not only when a parent element's z-index is specified but also under the following conditions:
    
    - Root element of the document (`<html>`).
    - Element with a [`position`](https://developer.mozilla.org/en-US/docs/Web/CSS/position) value `absolute` or `relative` and [`z-index`](https://developer.mozilla.org/en-US/docs/Web/CSS/z-index) value other than `auto`.
    - Element with a [`position`](https://developer.mozilla.org/en-US/docs/Web/CSS/position) value `fixed` or `sticky` (sticky for all mobile browsers, but not older desktop browsers).
    - Element with a [`container-type`](https://developer.mozilla.org/en-US/docs/Web/CSS/container-type) value `size` or `inline-size` set, intended for [container queries](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_container_queries).
    - Element that is a child of a [flex](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_flexible_box_layout/Basic_concepts_of_flexbox) container, with [`z-index`](https://developer.mozilla.org/en-US/docs/Web/CSS/z-index) value other than `auto`.
    - Element that is a child of a [`grid`](https://developer.mozilla.org/en-US/docs/Web/CSS/grid) container, with [`z-index`](https://developer.mozilla.org/en-US/docs/Web/CSS/z-index) value other than `auto`.
    - Element with an [`opacity`](https://developer.mozilla.org/en-US/docs/Web/CSS/opacity) value less than `1` (See [the specification for opacity](https://www.w3.org/TR/css-color-3/#transparency)).
    - Element with a [`mix-blend-mode`](https://developer.mozilla.org/en-US/docs/Web/CSS/mix-blend-mode) value other than `normal`.
    - Element with any of the following properties with value other than `none`:
        - [`transform`](https://developer.mozilla.org/en-US/docs/Web/CSS/transform)
        - [`filter`](https://developer.mozilla.org/en-US/docs/Web/CSS/filter)
        - [`backdrop-filter`](https://developer.mozilla.org/en-US/docs/Web/CSS/backdrop-filter)
        - [`perspective`](https://developer.mozilla.org/en-US/docs/Web/CSS/perspective)
        - [`clip-path`](https://developer.mozilla.org/en-US/docs/Web/CSS/clip-path)
        - [`mask`](https://developer.mozilla.org/en-US/docs/Web/CSS/mask) / [`mask-image`](https://developer.mozilla.org/en-US/docs/Web/CSS/mask-image) / [`mask-border`](https://developer.mozilla.org/en-US/docs/Web/CSS/mask-border)
    - Element with an [`isolation`](https://developer.mozilla.org/en-US/docs/Web/CSS/isolation) value `isolate`.
    - Element with a [`will-change`](https://developer.mozilla.org/en-US/docs/Web/CSS/will-change) value specifying any property that would create a stacking context on non-initial value (see [this post](https://dev.opera.com/articles/css-will-change-property/)).
    - Element with a [`contain`](https://developer.mozilla.org/en-US/docs/Web/CSS/contain) value of `layout`, or `paint`, or a composite value that includes either of them (i.e. `contain: strict`, `contain: content`).
    - Element placed into the [top layer](https://developer.mozilla.org/en-US/docs/Glossary/Top_layer) and its corresponding [`::backdrop`](https://developer.mozilla.org/en-US/docs/Web/CSS/::backdrop). Examples include [fullscreen](https://developer.mozilla.org/en-US/docs/Web/API/Fullscreen_API) and [popover](https://developer.mozilla.org/en-US/docs/Web/API/Popover_API) elements.

## **Stacking Context Operational Rules**

Let's delve into how stacking contexts are formed and influence the order in which nodes are drawn, explained step by step. For those interested in directly jumping to the implementation logic of z-index, please proceed to the next section titled "Z-Index Implementation Logic."


![image-3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ommypzrt2hg6893zsj7a.png)


The illustration above represents a schematic of the DOM structure. The numbers on each node denote the order they are visited in a preorder traversal. Nodes 2 and 4 possess distinct stacking contexts. Node 3 inherits the **stacking context** from node 2.

Comparing nodes 2 and 4, even though node 4 has the same z-index, its higher node number grants it priority in the stacking order. Since node 3 inherits the **stacking context** from node 2, no matter how high its z-index, it will be ranked lower in the stacking order compared to node 4.

### Example: Complicated Stacking Context

![image-4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5j84xznn4y473yfz04sp.png)

In the illustration, nodes 3 and 4 are situated below node 2, and node 7 is located beneath node 6. Notably, there exists a stacking context with a z-index of -1, as seen with node 6, and there are nodes, like node 5, that do not belong to any stacking context. Nodes without a separate stacking context are considered to have a z-index of 0.

![image-5](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/11bnjd2phd9ks9i8g2j6.png)

The nodes can be divided according to the drawing order as shown above. Node 1 takes precedence over nodes 6 and 7 in the vertical layer, and node 5 is superior to nodes 2, 3, and 4 in the vertical layer. Since nodes 3 and 4 are both below node 2, node 3 with the higher z-index takes precedence over node 4 in the vertical layer.
Therefore, the drawing order is as follows:ㅋ

![image-6](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mrhd0m7l10ls8tzpk7le.png)

# **Z-Index Implementation Logic**

Implementing z-index equates to creating a logic for arranging DOM drawing order, considering the stacking contexts. A stacking context can be identified through a pre-order traversal of the DOM tree. First, let's define a Stacking Context.

```tsx
type StackingContext = {
  zIndex: number //stacking context를 형성한 dom의 z-index
  domOrder: number //stacking context를 형성한 dom의 전위순회 순서
}

type CollectedDom = {
   contexts: StackingContext[] //dom이 물려받은 stacking context
   domOrder: number //dom의 전위순회 순서
   dom: HTMLELEMENT 
}
```

A StackingContext is formed by a DOM element that has a z-index. It collects the zIndex and domOrder of the formed DOM into the stacking context. A child inherits the StackingContext from its parent. If the child has a new z-index, it adds a new context to the Stacking context inherited from the parent. Then, the CollectedDom information corresponding to node 7 would be as follows:

```tsx
const 7Node = {
  contexts: [{zIndex: -1, domOrder: 6}] //6번 노드로부터 물려받음
  domOrder: 7
  dom: div태그
}
```

If you have collected `const collectedDoms: CollectedDom[]` while traversing the DOM, you can sort them in the correct order by considering the stacking context. There are three points to note:

1. If they are in the same stacking context, the order is determined by the DOM order.
2. If there is no stacking context, the z-index is considered to be 0.
3. In a stacking context, the parent element is drawn first regardless of the z-index of the child elements. (In vertical layer priority, it gives way to its children).

### 정렬로직

```tsx
function sort(a: CollectedDom, b: CollectedDom) {
   const limit = Math.min(a.contexts.length, b.contexts.length)
   
   /*
     stacking context를 순회하며 비교합니다.
   */
	 for(let i = 0; i < limit; ++i) {
     const aContext = a.contexts[i]
		 const bContext = b.contexts[i]

     /*
      * stacking context가 서로 다른 경우의 노드는 context의 
			* zIndex를 먼저 비교하고, 그 다음의 dom 순서를 비교합니다.
     */
     if(aContext.zIndex !== bContext.zIndex) {
			 return aContext.zIndex - bContext.zIndex
     } else if(aContext.domOrder - bContext.domOrder) {
       return aContext.domOrder - bContext.domOrder
     }
   }

  
   
	 /* 
    * 이 아래부터는 a,b는 동일한 stacking context 있음을 의미한다.
    * a와 b의 관계는 아래 둘 중 하나이다. 
    *   1. 부모 - 자식
    *   2. 형제
   */
   
   // 1. 부모-자식인 경우
   if(limit > 0) {
     const lastContext = a.contexts[limit-1] // 값이 같기 때문에 a,b 둘 중 어느것이여도 상관없다.
     //둘 중 하나가 부모임
     if(lastContext.domOrder === a.domOrder || lastContext.domOrder === b.domOrder {
       return a.domOrder - b.domOrder
     }
   }
  
   // 2. 형제인 경우 
   if(a.contexts.length !== b.contexts.length) {
     const aContext = a.contexts[limit] || { zIndex:0, domOrder: a.domOrder }
     const bContext = b.contexts[limit] || { zIndex:0, domOrder: b.domOrder }
     
     /*
      * stacking context가 서로 다른 경우의 노드는 context의 
			* zIndex를 먼저 비교하고, 그 다음의 dom 순서를 비교합니다.
     */
     if(aContext.zIndex !== bContext.zIndex) {
			 return aContext.zIndex - bContext.zIndex
     } else if(aContext.domOrder - bContext.domOrder) {
       return aContext.domOrder - bContext.domOrder
     }

   }
   
   // 그 외는 돔 순서로 결정한다.
   return a.domOrder - b.domOrder
}
```

The above code expresses the logic for sorting by z-index. First, it checks if the stacking contexts are different. If they are different, it prioritizes comparing the zIndex, followed by comparing the DOM order. The length of the stacking contexts can be different. It compares them sequentially based on the shortest context. If the compared stacking contexts are all the same, it proceeds to the next step.

If the compared stacking contexts have the same value, it checks if the two DOMs have a `parent-child relationship`. If they are in the same stacking context, regardless of the child's z-index value, the child takes precedence over the parent in the vertical layer.


![image-7](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mqppd4vxd8d3alif1mu4.png)


In the diagram, despite the red circle having a lower z-index value of -1 than its parent, it's clear that it has a higher priority in the vertical layering. If the parent's **`z-index: 0`** were to be removed, then the red circle would be positioned under the blue box.

The stacking context for the blue box is **`[{zIndex: 0, domOrder: 2}]`**, and for the red circle, it is **`[{zIndex: 0, domOrder: 2}, {zIndex: -1, domOrder: 3}]`**. Let's consider adding a purple circle as a child of the blue box.


![image-8](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xa1yajrdi7gx7pyn2bji.png)

The stacking context for the purple circle, similar to the blue box, is **`[{zIndex: 0, domOrder: 2}]`**. However, because its own domOrder is 3, it does not create a stacking context. As can be seen in the diagram above, the purple circle has a higher priority in the vertical layering than the red circle. Since an unspecified z-index is considered as 0, the red circle with a **`z-index of -1`** is consequently lower in priority.

## **Implementing in SVG**

SVG does not support the z-index property. Instead, elements positioned later in the DOM are given higher priority in the vertical stack. To apply z-index in SVG, one must manually manipulate the order of DOM elements. This is similar in canvas.

Using the **flutterjs** library, which creates an SVG tree analogous to the DOM tree, I leveraged the Visitor pattern and Stacking Contexts to implement z-index.

![image-9](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b477j56gdn68xdxmybww.png)

# **Conclusion**

This article has provided insights into the z-index property and its practical application within SVG. Writing this piece also allowed me to correct misunderstandings about the stacking context's operational rules and improve drag behavior in an ERD diagram I'm developing.


![drag](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vwmzf5ndd0mq72ykq1sj.gif)


# **References**

- [https://flitter.dev/](https://flitter.dev/)
- [MDN on z-index](https://developer.mozilla.org/en-US/docs/Web/CSS/z-index)