The CSS z-index Property: What You Need to Know (2024)

When building web pages, we often want to layer different elements on top of each other to create immersive, rich designs and interfaces. But, if two elements overlap, how do you control which one sits on top?

The CSS z-index Property: What You Need to Know (1)

As with many things in web design, the answer lies in CSS — specifically the z-index property. z-index is a key property in CSS, since it allows you to control the stacking order of elements on a web page.

In this post, we’ll discuss what the z-index property means in CSS, how to use it, and some tips for getting the most out of it in your projects. Let’s get started.

What is 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. If the x-axis goes left-to-right and the y-axis goes top-to-bottom, the z-axis adds the dimension of “toward” and “away from” the user. Elements with a higher z-index value appear closer to the user, and elements with a lower value look farther away.

The CSS z-index Property: What You Need to Know (3)

If two page elements overlap and no z-index value is given (or both elements have the same z-index value), the element placed last in the HTML code will appear on top of the element placed before it in the code. Also, elements that are given a position value (other than static, the default value) appear above elements without a position in the stacking.

How to Use z-index

The z-index property uses the following syntax:

z-index: [integer]<!-- or -->z-index: auto

The z-index property takes an integer value. The higher the integer, the higher the element will be in the stacking order. For example, if you have two elements with a z-index of 0 and 1, the element with a z-index of 1 will be in front of the element with a z-index of 0.

z-index can also take the value auto, which sets the stacking level of the element to 0, the default value.

Let’s look at an example. First, here’s a stack of four div elements without the z-index property:

See the Pen z-index: stacking example (no z-index) by HubSpot (@hubspot) on CodePen.

As you can see, the order of the stacking is determined by the order of the divs in the HTML code. Each div is placed on top of the div before it.

Now, let’s use z-index to flip this ordering:

See the Pen z-index: stacking example (with z-index) by HubSpot (@hubspot) on CodePen.

In this example, we use z-index to override the default stacking. Now, the first written div (“first”) is at the top of the stack.

Notice in these examples that divs are also given a position value. To control an element with the z-index property, the element must have a value for position that is not static (the default). z-index will apply to elements with a position of relative, fixed, absolute, or sticky. To learn more about how the CSS position property works, see our intro guide to CSS position.

z-index can also take negative integer values, like -1. Since the default z-index value for page elements is 0, elements with a negative z-index value appear behind elements without a set z-index value.

See the Pen z-index: stacking example (negative values) by HubSpot (@hubspot) on CodePen.

z-index Stacking Context

When using z-index, it’s also important to consider how stacking is affected in nested elements, since things get a bit more complex.

A z-index value positions an element on the z-axis relative to other elements in the same stacking context. A stacking context is a group of elements that share the same parent element and whose z-index values are compared with each other.

In the examples above, all divs were in the same stacking context, being direct child elements of <body>. Here’s another example that contains multiple stacking contexts:

See the Pen z-index: stacking context example 1 by HubSpot (@hubspot) on CodePen.

In this example, we have two container divs (marked with black borders), each with two child elements. Each of these containers has its ;own stacking context.

If we take container-2 and move it underneath container-1, we can see how these stacking contexts behave:

See the Pen z-index: stacking context example 2 by HubSpot (@hubspot) on CodePen.

Notice here that div 4 is in front of div 1, even though div 1 has a higher z-index value than div 4.

This is because container-2 itself has a higher z-index than container-1. Therefore, all child elements inside container-2 will sit in front of all child elements inside container-1, no matter their z-index values. The z-index values only determine position relative to other elements in the same container (i.e., the same stacking context).

Control your element stack with z-index.

When used correctly, the z-index property is a powerful but easy-to-understand tool for ordering your elements the right way, and is a must for anyone learning CSS. Just make sure to keep your z-index values tidy, so you’re not getting lost in the layers!

The CSS z-index Property: What You Need to Know (2024)

FAQs

The CSS z-index Property: What You Need to Know? ›

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.

What do you need to know about z-index? ›

The z-index CSS property sets the z-order of a positioned element and its descendants or flex and grid items. Overlapping elements with a larger z-index cover those with a smaller one.

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 are the requirements for z-index? ›

The css property z-index only works on positioned elements, meaning elements must be position absolute, fixed or relative in order for the z-index property to take effect. The higher the z-index the closer to the front it will appear. The values specified for the z-index property can be positive or negative.

What is z-index in CSS interview questions? ›

Define z-index.

This is one of the most frequently asked CSS interview questions. Z-index is used to specify the stack order of elements that overlap each other. Its default value is zero and can take both negative and positive values. A higher z-index value is stacked above the lower index element.

What does the z-index property indicate? ›

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.

Is higher z-index better? ›

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.

How to avoid z-index? ›

Z-index can be avoided in the vast majority of cases

We have a yellow square and a red square. We added a z-index of 1 so that the yellow square appears on top. However, we can achieve the exact same outcome by simply rearrange the order of the div's and getting rid of the z-index.

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

How high can z-index go in CSS? ›

The maximum and minimum value for the z-index in most browsers is limited to a signed 32-bit value i.e. from −2147483648 to +2147483647.

Can z-index be negative? ›

You can have negative z-index

This often means layering elements on top of each other, with ever-increasing values of z-index . To place an element on a layer below another one, it just has to have a lower value of z-index but that lower value can be negative.

Why is z-index 9999? ›

Often z-index is given arbitrary "high enough" number to be reasonably sure element would be really on top. It doesn't have to be this high, 9999 just makes sure if there is by a chance element with z-index 9998, 3000, 100 ...etc. (anything below 9999) - it will be above it.

How to know the z-index of an element? ›

When elements are positioned, they can overlap other elements. The z-index property specifies the stack order of an element (which element should be placed in front of, or behind, the others).

How to give the highest z-index? ›

If you apply a z-index to a non positioned element then it is ignored. This means for static elements you must add position:relative (without coordinates) for the element to partake in the stacking strategy. Browsers do have number limits and in Firefox the z-index limit is 2147483647.

How to reset z-index? ›

initial:
  1. This value sets the property back to its default, which for z-index is auto.
  2. Can be used to override any previously set z-index values and reset them back to their default behaviour.
Oct 27, 2023

How do you bring to the front of the z-index? ›

To bring an element to front of another element, you can use the CSS z-index property. The z-index property specifies the stack order of an element. An element with greater z-index value will be displayed in front of an element with a lower z-index value.

What do I need to know about Z scores? ›

Understanding Z-Score

It indicates how many standard deviations a data point is from the mean of the distribution. If a Z-score is 0, it indicates that the data point's score is identical to the mean score. A Z-score of 1.0 would indicate a value that is one standard deviation from the mean.

What is the highest possible z-index? ›

The maximum value of the z-index property is equal to 2147483647, and the minimum value is -2147483647.

What does z-index 1000 mean? ›

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 . Doing z-index: 1001 is an an attempt to appear "on top" of a item with z-index: 1000.

Top Articles
Who is Breckie Hill? Wiki, Bio, Age, Height, Boyfriend, Net Worth
Wer ist Breckie Hill? Wiki, Bio, Alter, Größe, Freund, Vermögen
Gfr Soccer
Episode 163 – Succession and Legacy • History of the Germans Podcast
Entegra Forum
Comparing Each Tacoma Generation, Which is Best?
Sand Castle Parents Guide
Pip Calculator | Myfxbook
Summoners War Update Notes
Sermon Collections, Sermons, Videos, PowerPoint Templates, Backgrounds
Unit 5 Lesson 6 Coding Activity
Praxis für Psychotherapie und Coaching Rhein-Neckar
Catholic Church Near Seatac Airport
Lighthouse Diner Taylorsville Menu
Craigslist Yamhill
Juanita Swink Hudson
630251.S - CCB-PWRIO-05 - Vision Systems - Vision Systems In-Sight, Cognex - InSight 2800 Series - Accessories Cables / Brackets IS28XX -
Metoprolol  (Kapspargo Sprinkle, Lopressor) | Davis’s Drug Guide
5 takeaways from Baylor’s historic comeback win vs. UCF: Bears find new energy in Orlando
Frontier Internet Outage Davenport Fl
Kagtwt
3 30 Mountain Time
Weather Arlington Radar
Pennys Department Store Near Me
2013 Freightliner Cascadia Fuse Box Diagram
Does Wanda Sykes Use A Cane
Refinery29 Horoscopes
Otter Bustr
What Are The Hours Of Chase Bank Today
Best Boxing Gyms Near Me
Sounder Mariners Schedule
Phunextra
charleston rooms & shares - craigslist
Section 212 Metlife Stadium
Nahant Magic Seaweed
Omari Lateef Mccree
What Does It Mean When Hulu Says Exp
Fuzz Bugs Factory Number Bonds
A1.35.3 Spanish short story: Tending the Garden
Cvs On 30Th And Fowler
Healthstream Mobile Infirmary
Payback Bato
Amariah Morales Snapchat
Ukg Dimensions Urmc
Function Calculator - eMathHelp
Uncc Class Schedule
Lucio Volleyball Code
Fapello.ckm
Raleigh Craigs List
Barber Gym Quantico Hours
Cardaras Logan Ohio
Latest Posts
Article information

Author: Trent Wehner

Last Updated:

Views: 6151

Rating: 4.6 / 5 (56 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Trent Wehner

Birthday: 1993-03-14

Address: 872 Kevin Squares, New Codyville, AK 01785-0416

Phone: +18698800304764

Job: Senior Farming Developer

Hobby: Paintball, Calligraphy, Hunting, Flying disc, Lapidary, Rafting, Inline skating

Introduction: My name is Trent Wehner, I am a talented, brainy, zealous, light, funny, gleaming, attractive person who loves writing and wants to share my knowledge and understanding with you.