Z-Index Explained: How to Stack Elements Using CSS (2025)

/ #CSS
Z-Index Explained: How to Stack Elements Using CSS (1)

by Veronika Ivhed

Z-Index Explained: How to Stack Elements Using CSS (2)

I have always struggled with the CSS property z-index. It sounds so easy at first. Elements with a higher z-index value are displayed in front of those with a lower z-index value. Still, a lot of times I have ended up in situations where it seems like the z-index value didn’t have any effect at all.

I decided that I’d had enough of trial and error with z-index and that I wanted to get a better understanding. I hope this article can help you so you will never wonder why z-index is not doing what you expect it to do.

Default stacking order

Let’s first mention the default order the browser stacks elements in, when no z-index is applied:

  1. Root element (the <html> element)
  2. Non-positioned elements in the order they are defined
  3. Positioned elements in the order they are defined

A non-positioned element is an element with the default position value static. A positioned element is an element with any other position value. Examples of other values are: absolute, relative, sticky or fixed.

HTML:

<div class=”pink”> <div class=”orange”></div></div><div class=”blue”></div><div class=”green”></div>

CSS:

/* This is only the CSS that is relevant for the example. For the complete CSS check the links below the pictures. */.blue, .pink, .orange { position: absolute;}
Z-Index Explained: How to Stack Elements Using CSS (3)

We defined the green box last in the document. Still, it appears behind the others because it is non-positioned.

Stacking with z-index

If we now want to change the stacking order of these elements, we can use the property z-index. An element with a higher z-index will be displayed in front of an element with a lower z-index. One thing to note is that z-index only works with positioned elements.

.blue, .pink, .orange { position: absolute;}.blue { z-index: 2;}.orange { z-index: 3;}.green { z-index: 100; // has no effect since the green box is non- positioned}
Z-Index Explained: How to Stack Elements Using CSS (4)

The orange box with a higher z-index is displayed in front of the blue box.

Stacking Context

Let’s say that we add another positioned box to the layout which we want to position behind the pink box. We update our code to the following:

HTML:

<div class=”pink”> <div class=”orange”></div></div><div class=”blue”></div><div class=”purple”></div><div class=”green”></div>

CSS:

.blue, .pink, .orange, .purple { position: absolute;}.purple { z-index: 0;}.pink { z-index: 1;}.blue { z-index: 2;}.orange { z-index: 3;}.green { z-index: 100;}
Z-Index Explained: How to Stack Elements Using CSS (5)

Our pink box is displayed in front of the purple box as expected, but what happened to the orange box? Why is it all of a sudden behind the blue one even though it has a higher z-index? This is because adding a z-index value to an element forms what is called a stacking context.

The pink box has a z-index value other than auto, which forms a new stacking context. The fact that it forms a stacking context affects how its child elements are being displayed.

It is possible to change the stacking order of the pink box child elements. However, their z-index only has a meaning within that stacking context. This means that, we won’t be able to move the orange box in front of the blue box, because they are not within the same stacking context anymore.

If we want the blue box and the orange box to be part of the same stacking context, we can define the blue box as a child element of the pink box. This will make the blue box appear behind the orange one.

<div class=”pink”> <div class=”orange”></div> <div class=”blue”></div></div><div class=”purple”></div><div class=”green”></div>
Z-Index Explained: How to Stack Elements Using CSS (6)

Stacking contexts are not only formed when applying z-index to an element. There are several other properties that cause elements to form stacking contexts. Some examples are: filter, opacity, and transform.

Let’s go back to our previous example. The blue box is again a sibling to the pink box. This time, instead of adding z-index to the pink box, we apply a filter to it.

HTML:

<div class=”pink”> <div class=”orange”></div></div><div class=”blue”></div><div class=”green”></div>

CSS:

.blue, .pink, .orange { position: absolute;}.pink { filter: hue-rotate(20deg);}.blue { z-index: 2;}.orange { z-index: 3;}.green { z-index: 100;}
Z-Index Explained: How to Stack Elements Using CSS (7)

The orange box still has a higher z-index than the blue one, but is still displayed behind it. This is because the filter value caused the pink box to form a new stacking context.

Summary

By using z-index on positioned elements, we can change the default stacking order.

When applying certain CSS properties, an element can form a stacking context. Z-index values only have a meaning within the same stacking context.

For more information on z-index, I recommend this article. I got a lot of inspiration from it when writing this.

Thanks for reading! :)

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

If this article was helpful, .

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

ADVERTISEMENT

Z-Index Explained: How to Stack Elements Using CSS (2025)

FAQs

What is stacking elements with z-index? ›

z-index is the CSS property that controls the stacking order of overlapping elements on a page. An element with a higher z-index value will appear in front of an element with a lower z-index value. The property is called “z-index” because it sets the order of elements along the z-axis.

How do you stack elements in CSS? ›

Using CSS position property: The position: absolute; property is used to position any element at the absolute position and this property can be used to stack elements on top of each other. Using this, any element can be positioned anywhere regardless of the position of other elements.

What is the rule of z-index in CSS? ›

Definition and Usage

The z-index property specifies the stack order of an element. An element with greater stack order is always in front of an element with a lower stack order.

What is the correct stack order of elements in CSS? ›

When the z-index property is not specified on any element, elements are stacked in the following order (from bottom to top): The background and borders of the root element. Descendant non-positioned elements, in order of appearance in the HTML. Descendant positioned elements, in order of appearance in the HTML.

What is the Z stacking process? ›

Z-stacking (also known as focus stacking) is a digital image-processing method which combines multiple images taken at different focal distances to provide a composite image with a greater depth of field (e.g., the thickness of the plane of focus) than any of the individual source images.

What is the stacking context in CSS? ›

Stacking context is a three-dimensional conceptualization of HTML elements along an imaginary z-axis relative to the user, who is assumed to be facing the viewport or the webpage. HTML elements occupy this space in priority order based on element attributes.

What is the best practice of using z-index in CSS? ›

If you want to create a custom stacking order, you can use the z-index property on a positioned element. Note: When no z-index property is specified, elements are rendered on the default rendering layer (Layer 0).

What is the most top z-index in CSS? ›

In CSS code bases, you'll often see z-index values of 999, 9999 or 99999. This is a perhaps lazy way to ensure that the element is always on top. It can lead to problems down the road when multiple elements need to be on top. Most of the time you'll find that a z-index of 1 or 2 will suffice for your needs.

What is z-index 1000 in CSS? ›

z-index defines which positioned element appears on top (Sort of like layers). So z-index: 1000 would appear on top of a z-index 999 . As mentioned above it is used for stacking elements.

How do you stack two items in CSS? ›

Use CSS Grid instead of position absolute to stack items on top of each other. Using the grid-area CSS shorthand property the grid item starts at the first grid line of the grid row ( grid-row-start: 1 ) and the first grid line of the grid column ( grid-column-start: 1 ).

Why is the z-index not working? ›

TL;DR: the most common cause for z-index not working is not explicitly declaring a CSS position value (i.e. position: relative, absolute, fixed or stick) on the element.

How do you change the order of stacked elements in CSS? ›

We can change the stacking order by adding the position property. Any positioned elements (and their children) are displayed in front of any non-positioned elements. Positioned elements have a position value relative, absolute, sticky or fixed. Non-positioned elements are the ones with default position (static).

What is a stack element? ›

In computer science, a stack is an abstract data type that serves as a collection of elements with two main operations: Push, which adds an element to the collection, and. Pop, which removes the most recently added element.

What is stack index? ›

indexOf(Object element) method is used to check and find the occurrence of a particular element in the Stack. If the element is present then the index of the first occurrence of the element is returned otherwise -1 is returned if the Stack does not contain the element. Syntax: Stack.indexOf(Object element)

What is stacking in data structure? ›

Stacks in Data Structures is a linear type of data structure that follows the LIFO (Last-In-First-Out) principle and allows insertion and deletion operations from one end of the stack data structure, that is top.

When should I use z-index? ›

If you want to create a custom stacking order, you can use the z-index property on a positioned element. Note: When no z-index property is specified, elements are rendered on the default rendering layer (Layer 0).

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Mrs. Angelic Larkin

Last Updated:

Views: 6236

Rating: 4.7 / 5 (47 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Mrs. Angelic Larkin

Birthday: 1992-06-28

Address: Apt. 413 8275 Mueller Overpass, South Magnolia, IA 99527-6023

Phone: +6824704719725

Job: District Real-Estate Facilitator

Hobby: Letterboxing, Vacation, Poi, Homebrewing, Mountain biking, Slacklining, Cabaret

Introduction: My name is Mrs. Angelic Larkin, I am a cute, charming, funny, determined, inexpensive, joyous, cheerful person who loves writing and wants to share my knowledge and understanding with you.