Export (0) Print
Expand All
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

z-index property

Sets or retrieves the stacking order of positioned objects.

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).

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:
© 2015 Microsoft