33

So normally we can get z-Index value of a div element using, e.g:

var zindex = document.getElementById('id').style.zIndex;

I am using this value in my code to do something. Now the problem is with default values where there is nothing defined in HTML code (or even in cases where z-Index is defined in external css), the same java script command returns nothing.

I understand that in default case, normally the browser decides the rendering based upon element stack. But is there any specific value or way to realize this in JavaScript that there is no value defined??

I mean, normally nothing returned would mean there is no value defined. But it also happend with external css, so how can i distinguish the both??

Gringo Suave
  • 29,931
  • 6
  • 88
  • 75
Johnydep
  • 6,027
  • 20
  • 57
  • 74
  • I don't really have an answer other than their isn't quite a default z-index value. It's not 0. Mozilla has a good writeup on the default stacking rules https://developer.mozilla.org/en/Understanding_CSS_z-index/Stacking_without_z-index – Doozer Blake Oct 27 '11 at 03:21
  • Isn't the value 3? as FrontPage Editor also shows 3, in div properties where there is nothing set. – Johnydep Oct 27 '11 at 03:38
  • 7
    No, it is not 3. FrontPage should definitely not be considered to be authoritative on *anything* – Andrew Barber Nov 20 '11 at 11:45
  • 1
    Link is now https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Positioning/Understanding_z_index/Stacking_without_z-index – josaphatv Feb 20 '22 at 16:35

6 Answers6

42

Default z-index of any element is 'auto' with exception of <html> which has default z-index:0. 'Auto' means that element gets z-index from its parent.

You can see this by using Developer Tools (in Chrome) or any similar tool in other browser. Also you can get this in your code by window.getComputedStyle() as others proposed here.

Konstantin Smolyanin
  • 17,579
  • 12
  • 56
  • 56
  • 9
    **Check using Chrome** : `Click On the Element --> Computed --> Filter --> Show All`. Hope helps someone. – Shaiju T Mar 19 '16 at 12:49
15

You might try window.getComputedStyle() on the element:

https://developer.mozilla.org/en/DOM/window.getComputedStyle

var e = document.getElementById('mydiv');
var value = window.getComputedStyle(e[0], null)['zIndex']

I got these values: "auto", "10". After changing: z-index: 10, position: relative.

Gringo Suave
  • 29,931
  • 6
  • 88
  • 75
5

I believe everything is z-indexed as 0. Really z-index is only valid if you set it for all the elements you care about. You can set the z-index of one div to 100000, but it won't matter if you don't set the z-index of the other element you are trying to overlap.

So I guess what I'm trying to say is that the default z-index of the div doesn't matter, it's only computed if you set it.

A div inside of a div doesn't have a specific z-index. Ad div inside of a div inside of the body inside of an iframe, inside of 3 more divs and 1 table, doesn't have a z-index(or a z-index that's computed.).

I can't see that there would be any practical reason for trying to find a z-index of an item, if it's irrelevant anyways.

Kelly Elton
  • 4,373
  • 10
  • 53
  • 97
  • hi, well actually i need the z-Index, to compute overlapping elements as i am making a plugin where i have to parse the whole page and decide which elements are visisble and if they are overlapping then decide which have highest z-index, or if default value, then are last in the stack (defined by browser). With this info, i check if there is some onClick event registerd with these elements, and if so, i can generate Clicks from plugin using external C++ application. – Johnydep Oct 27 '11 at 03:58
  • Ah I see. I just wanted to make sure that you understood that you can't only use the z-index to determine what's visible and what isn't. – Kelly Elton Oct 27 '11 at 10:23
  • oh thanks, i did not thought about child nodes, so thakns for informing. – Johnydep Oct 28 '11 at 14:15
  • +1 for noting that child nodes are removed from the z-index system. – Andrew Barber Nov 21 '11 at 11:33
3

@Konstantin-Smolyanin

Auto' means that element gets z-index from its parent.

No it doesn't - "inherit" means the element gets its' z-index from its' parent.

When no z-index property is specified, elements are rendered on the default rendering layer 0 (zero).

Neil
  • 1,928
  • 19
  • 14
2

Have a look at this question: Getting the z-index of a DIV in JavaScript?:


Since z index is mentioned in the CSS part you won't be able to get it directly through the code that you have mentioned. You can use the following example.

function getStyle(el,styleProp)
{
    var x = document.getElementById(el);

    if (window.getComputedStyle)
    {
        var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp); 
    }  
    else if (x.currentStyle)
    {
        var y = x.currentStyle[styleProp];
    }                     

    return y;
}

pass your element id and style attribute to get to the function.

Eg:

var zInd = getStyle ( "normaldiv1" , "zIndex" );
alert ( zInd );

For firefox you have to pass z-index instead of zIndex

var zInd = getStyle ( "normaldiv1" , "z-index" );
 alert ( zInd );

Reference


You must use z-index for Chrome as well.

Community
  • 1
  • 1
Korvin Szanto
  • 4,531
  • 4
  • 19
  • 49
0

You misunderstand the meaning of elements "style" property. By referring element.style what you actually retrieving is elements inline style. For example:

var el = document.getElementById('myEl');

console.log(el.style.zIndex); //you are asking for <div id="myEl" style="z-index: 1;"></div>

and to get the value of your css style(it could be a code goes here tag or any external stylesheet) you have to check is standard method (window.getComputedStyle) supported or not, if not you should check for internet explorer's specific version. You could do so by referring to el.currentStyle.

Summary:

to get inline style: var zIndex = el.style.zIndex

to get stylesheet value or style tag value:

    standard method: var zIndex = window.getComputedStyle(el,null).getPropertyValue('z-index');

    ie method: var zIndex = el.currentStyle['z-index'];
orustammanapov
  • 1,792
  • 5
  • 25
  • 44