Why we never use z-index (2024)

Why we never use z-index (3)

Have you ever added a CSS z-index to a div just to find out (a few days later) that your div is showing up in unexpected places? This is exactly why we made it a priority to avoid z-indices.

z-indices are especially difficult to manage in large code bases where multiple developers are involved. Developers keep trying to “overpower” z-indices introduced by other developers. Before you know it, someone decides to add a z-index of 9,999 thinking that it will conquer all — that is until someone else decides to do a z-index of 10,001, because somehow, 10,001 is more effective than 10,000.

It is absolutely ridiculous.

Consider natural rendering order of HTML

Z-index is unnecessary the majority of the time if we simply take a minute to think about the natural rendering order of the DOM. Browsers render elements top-down, meaning that elements lower in the DOM will naturally appear on top of a element defined higher up if the two overlap.

Consider this example:

<div style="position: relative">
<div style="z-index: 1; background-color: yellow; width: 200px; height: 200px; position: absolute">
Yellow
</div>
<div style="background-color: red; width: 400px;height: 400px; position: absolute">
Red
</div>
</div>

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.

<div style="position: relative">
<div style="background-color:red;width: 400px;height: 400px; position: absolute">
Red
</div>
<div style="background-color:yellow;width: 200px;height: 200px; position: absolute;">
Yellow
</div>
</div>

Render elements that need to be on top in higher level components

If you are building an app with components, consider mounting components that need to appear above other components higher up in the component hierarchy.

It is best to illustrate with an example:

// pseudo code, don't try this at home
<container>
Some content...

<component1>
<modal z-index={100}>
This is a modal
</modal>
<button onClick={showModal()} />
</component1>

</container>

In this case you want the modal to appear on top of “some content” in the outer container. A lazy way to do this would be to add a high z-index on the modal hoping that no one else will introduce a higher z-index somewhere else…. we all know that’s wishful thinking.

A better way is to simply mount the modal in the container itself letting the natural DOM rendering order do its thing:

// pseudo code, don't try this at home
<container>
Some content...

<component1 onButtonClick={showModal()}>
<button onClick={onButtonClick()} />
</component1>

<modal>
This is a modal
</modal>

</container>

In the vast majority of cases, z-indices can be avoided. Avoid them if possible. If you do decide z-indices are needed, make sure every developer on your team understands CSS stacking context. A few key points:

  • A positioned element (i.e. position: absolute, relative) WITH a non-auto z-index has its own stacking context. This means z-indices of child elements do not take effect outside of the positioned element. There are other cases when elements has its own stacking context, but this is the most common one.
  • By default, a html element has a z-index of “auto”, which evaluates to 0. This is different from explicitly defining a z-index of 0 due to the above point regarding stacking context.
  • Stick with one, at most two z-index values across the entire app. (i.e. 1 and -1).
Why we never use z-index (2024)

FAQs

Why we never use z-index? ›

Z-index is unnecessary the majority of the time if we simply take a minute to think about the natural rendering order of the DOM. Browsers render elements top-down, meaning that elements lower in the DOM will naturally appear on top of a element defined higher up if the two overlap.

Why does z-index never work? ›

You set z-index on a static element

By default, every element has a position of static. z-index only works on positioned elements (relative, absolute, fixed, sticky) so if you set a z-index on an element with a static position, it won't work.

Is there a limit to z-index? ›

The maximum range is ±2147483647. In CSS code bases, you'll often see z-index values of 999, 9999 or 99999.

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

Why do we even need z-index where will you use it? ›

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.

Is z-index bad practice? ›

Z-index is unnecessary the majority of the time if we simply take a minute to think about the natural rendering order of the DOM. Browsers render elements top-down, meaning that elements lower in the DOM will naturally appear on top of a element defined higher up if the two overlap.

What does z-index only work with? ›

Note: z-index only works on positioned elements (position: absolute, position: relative, position: fixed, or position: sticky) and flex items (elements that are direct children of display:flex elements).

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 high can z-index be? ›

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 0? ›

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.

When should you not use z-score? ›

One misuse of Z-scores is to use the cut-offs of +2 and +3 to assess obesity - the body mass index is more appropriate for this. Another misuse may be to use the relatively sophisticated Z-score in a famine emergency, when mid upper arm circumference may be the more appropriate diagnostic tool.

What is the best practice for z-index? ›

Best Practices for Z-Index Usage: To ensure smooth and predictable layering in your web designs, consider the following best practices: Use positive and negative values wisely: Positive values raise an element above the default layer, while negative values place an element behind it.

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 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. But if this hasn't solved your z-index issue or just want to get a little more information about the CSS property, then let's go a little deeper.

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 everything about z-index? ›

Z Index ( z-index ) is a CSS property that defines the order of overlapping HTML elements. Elements with a higher index will be placed on top of elements with a lower index. Note: Z index only works on positioned elements ( position:absolute , position:relative , or position:fixed ).

How do you get the z-index to work? ›

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.

Does z-index work with position absolute? ›

z-index only works for positioned elements

If you want to control the stacking order of elements, you can do so with z-index . But z-index will only take affect if the element also has a position value of absolute , relative or fixed .

Top Articles
Stars Cinema Golden
Im Kendall Hunt
Hannaford Weekly Flyer Manchester Nh
Danatar Gym
Chatiw.ib
Santa Clara College Confidential
Kris Carolla Obituary
Parks in Wien gesperrt
REVIEW - Empire of Sin
Classroom 6x: A Game Changer In The Educational Landscape
Guilford County | NCpedia
Nj State Police Private Detective Unit
Immortal Ink Waxahachie
Busby, FM - Demu 1-3 - The Demu Trilogy - PDF Free Download
Craigslist In Flagstaff
Lawson Uhs
Craigslist Maui Garage Sale
Aaa Saugus Ma Appointment
Healthier Homes | Coronavirus Protocol | Stanley Steemer - Stanley Steemer | The Steem Team
Amazing deals for Abercrombie & Fitch Co. on Goodshop!
Jenna Ortega’s Height, Age, Net Worth & Biography
Violent Night Showtimes Near Century 14 Vallejo
Magic Seaweed Daytona
Southland Goldendoodles
Grave Digger Wynncraft
Jesus Calling Feb 13
How To Improve Your Pilates C-Curve
Darktide Terrifying Barrage
Die wichtigsten E-Nummern
Wasmo Link Telegram
Panchang 2022 Usa
Amici Pizza Los Alamitos
Robot or human?
Iban's staff
CARLY Thank You Notes
Kips Sunshine Kwik Lube
Montrose Colorado Sheriff's Department
Terrier Hockey Blog
Weapons Storehouse Nyt Crossword
Ticket To Paradise Showtimes Near Marshall 6 Theatre
Koninklijk Theater Tuschinski
Tillman Funeral Home Tallahassee
Reese Witherspoon Wiki
Umiami Sorority Rankings
US-amerikanisches Fernsehen 2023 in Deutschland schauen
13 Fun &amp; Best Things to Do in Hurricane, Utah
Blackwolf Run Pro Shop
Aurora Southeast Recreation Center And Fieldhouse Reviews
Haunted Mansion Showtimes Near Millstone 14
Tìm x , y , z :a, \(\frac{x+z+1}{x}=\frac{z+x+2}{y}=\frac{x+y-3}{z}=\)\(\frac{1}{x+y+z}\)b, 10x = 6y và \(2x^2\)\(-\) \(...
ats: MODIFIED PETERBILT 389 [1.31.X] v update auf 1.48 Trucks Mod für American Truck Simulator
Latest Posts
Article information

Author: Gov. Deandrea McKenzie

Last Updated:

Views: 6131

Rating: 4.6 / 5 (46 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Gov. Deandrea McKenzie

Birthday: 2001-01-17

Address: Suite 769 2454 Marsha Coves, Debbieton, MS 95002

Phone: +813077629322

Job: Real-Estate Executive

Hobby: Archery, Metal detecting, Kitesurfing, Genealogy, Kitesurfing, Calligraphy, Roller skating

Introduction: My name is Gov. Deandrea McKenzie, I am a spotless, clean, glamorous, sparkling, adventurous, nice, brainy person who loves writing and wants to share my knowledge and understanding with you.