306

So if I understand z-index correctly, it would be perfect in this situation:

enter image description here

I want to place the bottom image (the tag/card) below the div above it. So you can't see the sharp edges. How do I do this?

z-index:-1 // on the image tag/card

or

z-index:100 // on the div above

doesn't work either. Neither does a combination of anything like this. How come?

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
Linkjuice57
  • 3,473
  • 6
  • 24
  • 23
  • 4
    Sometime we make mistake of appying position value other than `static` only to one element. We should ensure that we're applying the position property to all the elements being stacked on the basis of z-index else it doesn't work. – RBT Jun 05 '21 at 03:53
  • Some useful hints on this linked other question, which could be of use. You would have to share the elements and all their actual styles for somebody to advice about if this is the case more specifically than RBT did https://stackoverflow.com/questions/33217407/css-negative-z-index-what-does-it-mean – T. Nielsen Aug 04 '22 at 08:06

10 Answers10

657

The z-index property only works on elements with a position value other than static (e.g. position: absolute;, position: relative;, or position: fixed).

There is also position: sticky; that is supported in Firefox, is prefixed in Safari, worked for a time in older versions of Chrome under a custom flag, and is under consideration by Microsoft to add to their Edge browser.

Temani Afif
  • 245,468
  • 26
  • 309
  • 415
Evert
  • 8,161
  • 1
  • 15
  • 17
  • 18
    [It is also depending on other factors to create `Stacking Context`.](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Positioning/Understanding_z_index/The_stacking_context) – deEr. May 29 '18 at 18:39
  • 7
    adding to this answer, chrome and likely other browsers as of this writing require that you explicitly state `position: relative` for z-index to work, despite elements behaving in other relative ways by default – benipsen Jan 24 '19 at 08:17
  • `static` elements are *static* and can't move around in `x, y or z planes`. They can't move in `x plane (left or right)` or `y plane (top or bottom)` like when we work with `position: absolute`. And nor can they move in the `z plane i.e. with z-index`. – Aakash Mar 06 '19 at 04:17
  • 1
    are you aware of any issues with using `z-index` in a grid element?? – oldboy Dec 31 '19 at 00:45
  • 4
    `sticky` is now supported by all modern browsers: https://caniuse.com/css-sticky – guettli Oct 14 '21 at 09:46
  • 2
    In my case, it was due to the fixed element not having a background color, it seemed like it was not overlaying, but in reality it was, but only with the foreground colors. – Zied Hamdi Apr 26 '22 at 08:05
136

If you set position to other value than static but your element's z-index still doesn't seem to work, it may be that some parent element has z-index set.

The stacking contexts have hierarchy, and each stacking context is considered in the stacking order of the parent's stacking context.

So with following html

div { border: 2px solid #000; width: 100px; height: 30px; margin: 10px; position: relative; background-color: #FFF; }
#el3 { background-color: #F0F; width: 100px; height: 60px; top: -50px; }
<div id="el1" style="z-index: 5"></div>
<div id="el2" style="z-index: 3">
  <div id="el3" style="z-index: 8"></div>
</div>

no matter how big the z-index of el3 will be set, it will always be under el1 because it's parent has lower stacking context. You can imagine stacking order as levels where stacking order of el3 is actually 3.8 which is lower than 5.

If you want to check stacking contexts of parent elements, you can use this:

var el = document.getElementById("#yourElement"); // or use $0 in chrome;
do {
    var styles = window.getComputedStyle(el);
    console.log(styles.zIndex, el);
} while(el.parentElement && (el = el.parentElement));

There is a great article about stacking contexts on MDN

Buksy
  • 11,571
  • 9
  • 62
  • 69
  • 2
    how to fix that? – S.M. Pat May 04 '18 at 06:18
  • 7
    set proper z-index of parent elements. – Buksy May 04 '18 at 11:12
  • 2
    Just for note: when your parent element has `position: sticky` and it does not have z-index then whatever z-index you set to its child elements it will not work, you have to set some z-index for parent element, maybe it's true for all position types: `sticky, relative, absolute` etc but it was `sticky` in my case. – Haritsinh Gohil Mar 09 '22 at 14:13
  • All around great answer. Snippet was perfect for identifying the problem or quickly ruling out this solution. – Akaisteph7 May 04 '23 at 15:07
45

Your elements need to have a position attribute. (e.g. absolute, relative, fixed) or z-index won't work.

Sunil Garg
  • 14,608
  • 25
  • 132
  • 189
clem
  • 3,345
  • 1
  • 22
  • 33
30

In many cases an element must be positioned for z-index to work.

Indeed, applying position: relative to the elements in the question would likely solve the problem (but there's not enough code provided to know for sure).

Actually, position: fixed, position: absolute and position: sticky will also enable z-index, but those values also change the layout. With position: relative the layout isn't disturbed.

Essentially, as long as the element isn't position: static (the default setting) it is considered positioned and z-index will work.


Many answers to "Why isn't z-index working?" questions assert that z-index only works on positioned elements. As of CSS3, this is no longer true.

Elements that are flex items or grid items can use z-index even when position is static.

From the specs:

4.3. Flex Item Z-Ordering

Flex items paint exactly the same as inline blocks, except that order-modified document order is used in place of raw document order, and z-index values other than auto create a stacking context even if position is static.

5.4. Z-axis Ordering: the z-index property

The painting order of grid items is exactly the same as inline blocks, except that order-modified document order is used in place of raw document order, and z-index values other than auto create a stacking context even if position is static.

Here's a demonstration of z-index working on non-positioned flex items: https://jsfiddle.net/m0wddwxs/

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
  • 3
    "Elements that are flex items or grid items can use z-index even when position is static." Yep, the spec essentially says to treat `position: static` the exact same as it would treat `position: relative` for flex and grid items due to the dynamic nature of those methods of layout. So I would perhaps say it's not that it's no longer true in CSS3 that an element needs to be positioned, but that CSS3 treats flex and grid items as if they were positioned, regardless. Just two ways of looking at the same thing, though, probably. – TylerH Oct 25 '17 at 19:14
  • 1
    @TylerH we should pay attention to that part of the spec because we should *treat static the same as relative* only when dealing with z-index and not in general. You cannot for example shift a flex item using left/top/bottom/right unless you explicitely speficy `position:relative`. Descendants of flex items with absolute position will not consider the flex item as their containing block because the flex item is not positionned. (https://jsfiddle.net/0yo7utfL/2/) – Temani Afif May 24 '19 at 00:07
  • i found a wierd "bug" with css. for some reason `overflow-y: scroll` prevents `overflow-x: visible` from working... [can you please take a look?](https://stackoverflow.com/q/59538547/7543162) – oldboy Dec 31 '19 at 01:19
15

Make sure that this element you would like to control with z-index does not have a parent with z-index property, because element is in a lower stacking context due to its parent’s z-index level.

Here's an example:

<section class="content">            
    <div class="modal"></div>
</section>

<div class="side-tab"></div>

// CSS //
.content {
    position: relative;
    z-index: 1;
}

.modal {
    position: fixed;
    z-index: 100;
}

.side-tab {
    position: fixed;
    z-index: 5;
}

In the example above, the modal has a higher z-index than the content, although the content will appear on top of the modal because "content" is the parent with a z-index property.

Here's an article that explains 4 reasons why z-index might not work: https://coder-coder.com/z-index-isnt-working/

Abdelsalam Megahed
  • 1,281
  • 12
  • 13
11

Z-index needs these to work:

  1. Position: relative, absolute, fixed, ..
  2. Make sure that the parent element hasn't overflow: hidden;
Mohamad Hamouday
  • 2,070
  • 23
  • 20
2

I had the same problem with z-index and fixed it by setting the background color like this:

 background-color: white;
jonny
  • 4,264
  • 4
  • 22
  • 29
erfan
  • 21
  • 2
1

If all else fails, look for syntax errors in your HTML. It's not intuitive, but I've seen it be the reason why z-index doesn't work.

The following code has invalid HTML syntax:

<div class="over"/>
<div class="under"/>

...(it's is invalid syntax because a div isn't a self closing tag).

CSS properties that were applied to these rogue HTML elements, such as background-color: black, position: fixed, width: 150px, and top:150px, were all working as expected. However, the z-index: 2 property wasn't working under the exact same conditions.

Only when the invalid HTML was fixed did the z-index work correctly.

I'm not sure why z-index was pickier than the other CSS attributes, but maybe this answer can help someone.

JCollier
  • 1,102
  • 2
  • 19
  • 31
0

I got my answer from codercoder.com: In my code I have set my Navbar's opacity to 0.9.

Once you remove the opacity property from the Navbar's CSS, the z-index works as expected.

jonny
  • 4,264
  • 4
  • 22
  • 29
-1

in my case the setting upper element to z-index: -1 helps.

S.M. Pat
  • 308
  • 3
  • 12