Maximum Z-Index Value in CSS
The calculator for this page is switched off while it is checked. The formula is below, and the worked example in the article shows how to use it by hand.
This page explains the maximum z-index value you can use in CSS. The short answer: there is no hard maximum in the CSS specification—browsers accept any integer, but practical limits come from the browser’s internal number handling and your own code’s readability. You can set z-index to 2147483647 (the largest 32-bit signed integer) and it will work, but you rarely need to go that high.
Formula
There is no formula for z-index—it’s a CSS property that takes an integer value. The only “formula” that matters is the browser’s internal limit. Most browsers store z-index as a 32-bit signed integer, so the maximum is 231 − 1 = 2,147,483,647. Values above that may be clamped, ignored, or cause unexpected behavior depending on the browser.
Worked Example
Using the default value of 10 from the calculator above: 10 is less than 2,147,483,647, so it’s valid. If you entered 2,147,483,648, it would exceed the 32-bit limit and the calculator would show 0 (invalid). In practice, browsers treat such a value as if it were the maximum or wrap around, but the CSS spec says any integer is allowed.
How Browsers Handle Z-Index Internally
CSS itself doesn’t define a maximum—it just says z-index accepts an integer. But browsers are software with finite memory. Most use a 32-bit signed integer to store the computed z-index, matching the C++ int type. That means the largest value you can reliably use is 2,147,483,647. If you write a larger number, the browser may:
- Clamp it to 2,147,483,647 (most common)
- Ignore the declaration entirely
- Wrap around to a negative number (rare, but possible in older engines)
For example, Chrome and Firefox both clamp values above the 32-bit limit. Safari historically had bugs with very large values, so it’s safest to stay below the limit.
Why You Shouldn’t Use the Maximum
Even though you can set z-index to 2,147,483,647, doing so is almost always a bad idea. Here’s why:
- Maintenance nightmare: When you need to stack something above it later, you’re stuck. You’d have to change the original value, which could break other layers.
- Readability: A value like 2147483647 tells a future developer nothing. A value like 100 or 1000 is self-explanatory.
- Browser quirks: Very large values can trigger rounding errors in some rendering engines, especially when combined with transforms or animations.
Instead, use small, logical numbers. A common pattern is to use increments of 10 or 100, leaving room for future layers. For example, a modal might be z-index: 1000, a dropdown 900, and a header 800.
How to Choose a Z-Index Value
Start with the lowest value that works. If you only have a few layers, use 1, 2, 3. If you have many, use a scale like 10, 20, 30. Reserve the highest values for things that must always be on top, like modals, tooltips, and notifications.
Here’s a practical system:
- Base content: auto or 0
- Sticky headers: 100
- Dropdowns: 200
- Modals: 1000
- Tooltips: 2000
This leaves plenty of headroom. If you ever need to exceed 2000, you can, without touching the lower layers.
Common Problems and Misconceptions
“Z-index only works with positioned elements.” True—z-index has no effect on static elements (position: static). You need position: relative, absolute, fixed, or sticky.
“Higher z-index always means on top.” Not always. Stacking contexts override z-index. If a parent has its own stacking context (e.g., opacity < 1, transform, filter), its children's z-index values are compared only within that parent. So a child with z-index: 9999 can still appear below a sibling's child with z-index: 1 if the parent's stacking order is lower.
“Negative z-index hides an element.” It can, but only if the parent doesn’t create a stacking context. A negative z-index puts an element behind its parent’s background, which may make it invisible—but it’s still in the document flow.
Z-Index vs. Other Stacking Methods
Z-index isn’t the only way to control stacking. Other CSS properties can also affect order:
- DOM order: Without z-index, later elements in the HTML appear on top of earlier ones.
- Opacity: An element with opacity less than 1 creates a stacking context, which can change how z-index behaves.
- Transform, filter, perspective: These also create stacking contexts, which can trap z-index values.
- Position: Positioned elements (with z-index auto) stack above non-positioned ones.
Understanding stacking contexts is more important than memorizing the maximum z-index. A z-index of 1 inside a stacking context that’s lower in the global order will never beat a z-index of 0 in a higher context.
Frequently Asked Questions
What is the highest z-index I can use? Theoretically, any integer. Practically, 2147483647 is the safe ceiling in all major browsers. Values above that are clamped or ignored.
Is z-index: 9999 enough? For almost every real-world site, yes. 9999 is far above typical UI elements. The only time you’d need more is if you’re generating z-index values programmatically (e.g., in a game or complex app) and need a scale that doesn’t collide.
Can z-index be negative? Yes, negative values are allowed. They place an element behind its parent’s background, which can be useful for decorative elements. But be careful—negative z-index can make an element unclickable if it goes behind another element.
Does z-index work on flex items? Yes, flex items respect z-index even if they are not positioned, because flex items are always considered positioned for stacking purposes.
Why is my z-index not working? Usually because the element isn’t positioned, or a parent creates a stacking context that traps the value. Check the element’s position property and look for any opacity, transform, or filter on ancestors.
What’s the difference between z-index: auto and z-index: 0? Both are similar, but z-index: 0 creates a stacking context, while auto does not. That means z-index: 0 can affect how the element stacks relative to other stacking contexts, even though it doesn’t change its own order.
Related
Formula used
in z | Desired z-index | - | 10 | 0 out is_valid | Valid? | - | if(z
Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.