Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between auto, 0, and no z-index?

Tags:

html

css

z-index

What is the difference between:

  1. z-index: auto
  2. z-index: 0
  3. no z-index at all

All the above scenarios are for a div that encloses two divs, div1 and div2 each having a z-index which is 9 and 10 respectively.

The enclosing div is in the stacking context of HTML.

like image 997
bluelurker Avatar asked Jan 01 '13 11:01

bluelurker


People also ask

What does Z Index auto mean?

z-index: auto. For a positioned box (that is, one with any position other than static ), the z-index property specifies: The stack level of the box in the current stacking context. Whether the box establishes a local stacking context.

Why do we need 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 ).

Can I use Z index without position?

Positioning and order In order for z-index to have any effect at all, it needs to be applied to a positioned element, that means, an element with position relative , absolute , fixed , or sticky . If you don't apply a z-index , the elements will be stacked in the order they are written in the HTML.

What do you do if Z index doesn't work?

To sum up, most issues with z-index can be solved by following these two guidelines: Check that the elements have their position set and z-index numbers in the correct order. Make sure that you don't have parent elements limiting the z-index level of their children.


1 Answers

Not specifying z-index is the same as z-index: auto; that is its initial value.

auto and 0 mean the same thing if your element doesn't create its own stacking context; e.g. it is not positioned as relative, absolute or fixed.

If your enclosing div isn't positioned, then whatever you set its z-index to doesn't matter; it and all its contents will participate in the stacking context of html, and its descendants will always be positioned in front of it.

like image 54
BoltClock Avatar answered Oct 06 '22 00:10

BoltClock