12 out of 20 rated this helpful - Rate this topic

z-index Attribute | zIndex Property

Gets or sets the stacking order of positioned objects.

Syntax

CSS { z-index : vOrder }
Scripting [ vOrder = ] object.style.zIndex

Possible Values

vOrder String or Integer that specifies or receives one of the following 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.

The property is read/write for all objects except the following, for which it is read-only: currentStyle. The property has a default value of auto. The Cascading Style Sheets (CSS) attribute is not inherited.

DHTML expressions can be used in place of the preceding value(s). As of Internet Explorer 8, expressions are supported in IE7 Standards mode and IE5 (Quirks) mode only. For more information, see About Dynamic Properties and Defining Document Compatibility.

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. Set this parameter to null to remove the attribute.

The zIndex 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 zIndex property. In earlier versions of Windows Internet Explorer, the iframe object is windowed and, like all windowed controls, ignores the zIndex property. If you maintain Web pages that were designed for earlier versions of Internet Explorer that do not support the zIndex 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 Internet Explorer 7, the select property is windowless and can make use of the z-index attribute and the zIndex property.

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 zIndex 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 mode and an empty string ( "") in IE5 mode. Applications that rely on specific default values for the zIndex property of the currentStyle object should be modified to take the document compatibility mode into account.

Examples

The following examples use the z-index attribute and the zIndex property to change the stacking order of objects.

This example uses an inline style sheet to set the stacking order.


<img src="cone.jpg" 
    alt="cone"
    style="position: absolute; 
        top: 100px; 
        left: 100px; 
        z-index: 4">
<div style="position:absolute; 
    top: 100px; 
    left: 100px;
    color: red; 
    background-color: #999966; 
    font-weight:bold; 
    z-index: 1">
...
</div>

Code example: http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/z-index.htm

This example uses inline scripting to set the stacking order.


<img id="cone" 
    alt="cone"
    src="cone.jpeg"
    style="position: absolute;
        top: 10px;
        left: 10px;"
    onclick="cone.style.zIndex=1; 
        sphere.style.zIndex=2"/>
<img id="sphere" 
    alt="sphere"
    src="sphere.jpg"
    style="position: absolute;
        top: 1px;
        left: 1px;"
    onclick="cone.style.zIndex=2; 
        sphere.style.zIndex=1"/>

Code example: http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/zIndex.htm

Standards Information

This property is defined in Cascading Style Sheets (CSS), Level 2 (CSS2).

Applies To

A, ADDRESS, APPLET, B, BIG, BLOCKQUOTE, BUTTON, CAPTION, CENTER, CITE, CODE, COL, COLGROUP, CSSStyleDeclaration, currentStyle, CUSTOM, DD, defaults, DFN, DIR, DIV, DL, DT, EM, FIELDSET, FORM, I, IFRAME, INPUT type=button, INPUT type=checkbox, INPUT type=file, INPUT type=image, INPUT type=password, INPUT type=radio, INPUT type=reset, INPUT type=submit, INPUT type=text, ISINDEX, KBD, LABEL, LEGEND, LI, LISTING, MARQUEE, MENU, OL, P, PLAINTEXT, PRE, runtimeStyle, S, SAMP, SMALL, SPAN, STRIKE, STRONG, style, SUB, SUP, TABLE, TBODY, TD, TEXTAREA, TFOOT, TH, THEAD, TR, TT, U, UL, VAR, XMP, CSSCurrentStyleDeclaration Constructor, CSSRuleStyleDeclaration Constructor, CSSStyleDeclaration Constructor
Did you find this helpful?
(1500 characters remaining)

Community Additions

© 2013 Microsoft. All rights reserved.