z-index property

Sets or retrieves the stacking order of positioned objects.

Internet Explorer 9

 

Syntax

z-index: auto | order

Property values

auto

Default. String that specifies the stacking order of the positioned objects based on the order in which the objects appear in the HTML source.

order

Integer that specifies the position of the object in the stacking order.

CSS information

Applies ToAll elements
Mediavisual
Inheritedno
Initial Valueauto

Remarks

Positive z-index values are positioned above a negative (or lesser value) z-index. Two objects with the same z-index are stacked according to source order. A positive value positions the element above text that has no defined z-index, and a negative value positions it below.

The z-index property only applies to objects that have the position property set to relative or absolute.

The property does not apply to windowed controls, such as select objects.

Input from pointing devices, such as a mouse, does not penetrate through overlapping elements even if the elements are not visible. This is also true for positioned elements with a negative z-index unless:

  • The parent is a scrolling container (that is, its overflow property is set to auto or scroll).
  • The parent is positioned (that is, its position property is set to absolute, relative, or fixed).

As of Microsoft Internet Explorer 5.5, the iframe object is windowless and supports the z-index property. In earlier versions of Windows Internet Explorer, the iframe object is windowed and, like all windowed controls, ignores the z-index property. If you maintain Web pages that were designed for earlier versions of Internet Explorer that do not support the z-index property, you might want to redesign the pages, especially if the pages contain iframe objects that are stacked on top of windowed controls, such as select objects. You can use the visibility attribute to hide windowed controls that you want an iframe object to overlap. You can also position windowed controls so that iframe objects do not overlap them.

As of Windows Internet Explorer 7, the select property is windowless and can make use of the z-index attribute and the z-index property.

Windows Internet Explorer 8 and later. When you access a property that has not been explicitly set, Internet Explorer returns the default value of the property. The default value of the z-index property for the currentStyle object depends on the document compatibility mode used to display the Web page. When a page is displayed in IE8 Standards mode, the default value is auto. The default value is 0 in IE7 Standards mode and an empty string ("") in IE5 (Quirks) mode. Applications that rely on specific default values for the z-index property of the currentStyle object should be modified to take the document compatibility mode into account.

You can use the z-index property on Grid items. While the z-index property normally only applies to objects that have the position property not set to static, the z-index property applies to Grid items even when the position property is set to static.

Examples

The following example shows how to use the z-index property to change the display of overlapping objects. The example shows two pairs of overlapping boxes, one red box and one light blue box. In the first pair of boxes, the light blue box has a higher z-index value so it overlaps the red box. In the second pair of boxes, the red box has the higher z-index value so it overlaps the light blue box.


div {
  position: absolute;
  text-align: center;
  width: 120px;
  height: 100px;
  font-family: sans-serif;
}
.redbox1 {
  background-color: red;
  top: 20px;
  left: 20px;
  z-index: -1;
}
.bluebox1 {
  background-color: lightblue;
  top: 50px;
  left: 40px;
  z-index: 1;
}
.redbox2 {
  background-color: red;
  top: 20px;
  left: 180px;
  z-index: 1;
}
.bluebox2 {
  background-color: lightblue;
  top: 50px;
  left: 200px;
  z-index: -1;
}


<body>
  <div class="redbox1">Lorem ipsum dolor sit amet, consectetuer adipiscing elit...</div>
  <div class="bluebox1">Lorem ipsum dolor sit amet, consectetuer adipiscing elit...</div>
  <div class="redbox2">Lorem ipsum dolor sit amet, consectetuer adipiscing elit...</div>
  <div class="bluebox2">Lorem ipsum dolor sit amet, consectetuer adipiscing elit...</div>
</body>

The following image shows the result:

An image showing two pairs of overlapping boxes with different z-index settings.

See also

CSSStyleDeclaration
currentStyle
defaults
runtimeStyle
style

 

 

Show:
© 2014 Microsoft