z-index

  • Revision slug: Web/CSS/z-index
  • Revision title: z-index
  • Revision id: 508177
  • Created:
  • Creator: tregagnon
  • Is current revision? No
  • Comment Highlight lines in code sample

Revision Content

{{CSSRef}}

Summary

The z-index CSS property specifies the z-order of an element and its descendants. When elements overlap, z-order determines which one covers the other. An element with a larger z-index generally covers an element with a lower one.

For a positioned box, the z-index property specifies:

  1. The stack level of the box in the current stacking context.
  2. Whether the box establishes a local stacking context.

{{cssbox("z-index")}}

Syntax

Formal syntax: {{csssyntax("z-index")}}
z-index: auto     /* Keyword value */
z-index: 0        /* <integer> value */
z-index: 3
z-index: 289

z-index: inherit

Values

auto
The box does not establish a new local stacking context. The stack level of the generated box in the current stacking context is the same as its parent's box.
<integer>
This integer is the stack level of the generated box in the current stacking context. The box also establishes a local stacking context in which its stack level is 0. This means that the z-indexes of descendants are not compared to the z-indexes of elements outside this element.

Examples

HTML

<div class="dashed-box">Dashed box
  <span class="gold-box">Gold box</span>
  <span class="green-box">Green box</span>
</div>

CSS

.dashed-box { 
  position: relative;
  z-index: 1;
  border: dashed;
  height: 8em;
  margin-bottom: 1em;
  margin-top: 2em;
}
.gold-box { 
  position: absolute;
  z-index: 2;
  background: gold;
  width: 80%;
  left: 60px;
  top: 3em;
}
.green-box { 
  position: absolute;
  z-index: 3;
  background: lightgreen;
  width: 20%;
  left: 20em;
  top: -25px;
  height: 7em;
  opacity: 0.9
}

Result

{{ EmbedLiveSample('Examples', '550', '200', '') }}

 

Specifications

Specification Status Comment
{{SpecName('CSS3 Transitions', '#animatable-css', 'visibility')}} {{Spec2('CSS3 Transitions')}} Defines visibility as animatable.
{{SpecName('CSS2.1', 'visuren.html#z-index', 'z-index')}} {{Spec2('CSS2.1')}} Initial specification.

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 1.0 {{CompatGeckoDesktop("1.0")}} 4.0 4.0 1.0
Negative values (CSS2.1 behavior, not allowed in the obsolete CSS2 spec) 1.0 {{CompatGeckoDesktop("1.9")}} 4.0 4.0 1.0
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}

See also

Revision Source

<div>
 {{CSSRef}}</div>
<h2 id="Summary">Summary</h2>
<p>The <code>z-index</code> CSS property specifies the z-order of an element and its descendants. When elements overlap, z-order determines which one covers the other. An element with a larger z-index generally covers an element with a lower one.</p>
<p>For a positioned box, the <code>z-index</code> property specifies:</p>
<ol>
 <li>The stack level of the box in the current stacking context.</li>
 <li>Whether the box establishes a local stacking context.</li>
</ol>
<p>{{cssbox("z-index")}}</p>
<h2 id="Syntax">Syntax</h2>
<pre class="twopartsyntaxbox">
<a href="/en-US/docs/CSS/Value_definition_syntax" title="CSS/Value_definition_syntax">Formal syntax</a>: {{csssyntax("z-index")}}
</pre>
<pre>
z-index: auto     /* Keyword value */
z-index: 0        /* &lt;integer&gt; value */
z-index: 3
z-index: 289

z-index: inherit
</pre>
<h3 id="Values">Values</h3>
<dl>
 <dt>
  <code>auto</code></dt>
 <dd>
  The box does not establish a new local stacking context. The stack level of the generated box in the current stacking context is the same as its parent's box.</dd>
 <dt>
  <code>&lt;integer&gt;</code></dt>
 <dd>
  This integer is the stack level of the generated box in the current stacking context. The box also establishes a local stacking context in which its stack level is <code>0</code>. This means that the z-indexes of descendants are not compared to the z-indexes of elements outside this element.</dd>
</dl>
<h2 id="Examples">Examples</h2>
<h4>HTML</h4>
<pre class="brush: html">
&lt;div class="dashed-box"&gt;Dashed box
  &lt;span class="gold-box"&gt;Gold box&lt;/span&gt;
  &lt;span class="green-box"&gt;Green box&lt;/span&gt;
&lt;/div&gt;
</pre>
<h4>CSS</h4>
<pre class="brush: css; highlight:[3,11,19]">
.dashed-box { 
  position: relative;
  z-index: 1;
  border: dashed;
  height: 8em;
  margin-bottom: 1em;
  margin-top: 2em;
}
.gold-box { 
  position: absolute;
  z-index: 2;
  background: gold;
  width: 80%;
  left: 60px;
  top: 3em;
}
.green-box { 
  position: absolute;
  z-index: 3;
  background: lightgreen;
  width: 20%;
  left: 20em;
  top: -25px;
  height: 7em;
  opacity: 0.9
}
</pre>
<h4>Result</h4>
<p>{{ EmbedLiveSample('Examples', '550', '200', '') }}</p>
<p>&nbsp;</p>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('CSS3 Transitions', '#animatable-css', 'visibility')}}</td>
   <td>{{Spec2('CSS3 Transitions')}}</td>
   <td>Defines <code>visibility</code> as animatable.</td>
  </tr>
  <tr>
   <td>{{SpecName('CSS2.1', 'visuren.html#z-index', 'z-index')}}</td>
   <td>{{Spec2('CSS2.1')}}</td>
   <td>Initial specification.</td>
  </tr>
 </tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{CompatibilityTable}}</p>
<div id="compat-desktop">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Chrome</th>
    <th>Firefox (Gecko)</th>
    <th>Internet Explorer</th>
    <th>Opera</th>
    <th>Safari</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>1.0</td>
    <td>{{CompatGeckoDesktop("1.0")}}</td>
    <td>4.0</td>
    <td>4.0</td>
    <td>1.0</td>
   </tr>
   <tr>
    <td>Negative values (CSS2.1 behavior, not allowed in the obsolete CSS2 spec)</td>
    <td>1.0</td>
    <td>{{CompatGeckoDesktop("1.9")}}</td>
    <td>4.0</td>
    <td>4.0</td>
    <td>1.0</td>
   </tr>
  </tbody>
 </table>
</div>
<div id="compat-mobile">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Android</th>
    <th>Firefox Mobile (Gecko)</th>
    <th>IE Mobile</th>
    <th>Opera Mobile</th>
    <th>Safari Mobile</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{CompatUnknown}}</td>
    <td>{{CompatUnknown}}</td>
    <td>{{CompatUnknown}}</td>
    <td>{{CompatUnknown}}</td>
    <td>{{CompatUnknown}}</td>
   </tr>
  </tbody>
 </table>
</div>
<h2 id="See_also">See also</h2>
<ul>
 <li>CSS {{Cssxref("position")}} property</li>
 <li><a href="/en-US/docs/CSS/Understanding_z-index" title="CSS/Understanding_z-index">Understanding CSS z-indexes</a></li>
</ul>
Revert to this revision