Style zIndex attribute

Definition and usage

zIndex Sets or returns the stacking order of a positioned element.

The element with a higher stacking order (1) is always in front of another element with a lower stacking order (0).

Tip:An element that is positioned is position attribute Set to:relative,absolute or fixed elements.

Tip:This property is very useful if you want to create overlapping elements.

See also:

CSS Tutorial:CSS-positionering

CSS Reference Manual:z-index attribute

Example

Example 1

Change the stacking order of the <img> element:

document.getElementById("img1").style.zIndex = "1";

Try it yourself

Example 2

Change the z-index attribute value of the <div> element:

document.getElementById("myDIV").style.zIndex = "-1";

Try it yourself

Example 3

Returns the z-index attribute value of the <img> element:

alert(document.getElementById("img1").style.zIndex);

Try it yourself

Syntax

Return zIndex attribute:

object.style.zIndex

Set zIndex attribute:

object.style.zIndex = "auto|number|initial|inherit"

Attribute value

Value Description
auto The browser determines the stacking order of an element (based on its order in the document). Default.
number An integer that defines the stacking order of an element. Negative values are allowed.
initial Sets this property to its default value. See also initial.
inherit Inherits this property from its parent element. See also inherit.

Technical details

Default value: auto
Return value: A string representing the stacking order of an element.
CSS version: CSS2

Webbläsarstöd

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
Stöd Stöd Stöd Stöd Stöd