Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1065956 of Stacking without z-index

  • Revision slug: Web/CSS/CSS_Positioning/Understanding_z_index/Stacking_without_z-index
  • Revision title: Stacking without z-index
  • Revision id: 1065956
  • Created:
  • Creator: javichito
  • Is current revision? No
  • Comment Revert to revision of 2016-02-09 19:29:11 by duncanmcdonald: "Accidentally translated the english version to spanish"

Revision Content

« CSS « Understanding CSS z-index

Stacking without z-index

When no element has a z-index, elements are stacked in this order (from bottom to top):

  1. Background and borders of the root element
  2. Descendant blocks in the normal flow, in order of appearance (in HTML)
  3. Descendant positioned elements, in order of appearance (in HTML)

In the following example, absolute and relatively positioned blocks are properly sized and positioned to illustrate the stacking rules.

Notes:

  • Given a homogeneous group of elements without any z-index property, such as the positioned blocks (DIV #1 to #4) in the example, the element's stacking order is their order in the HTML hierarchy, regardless of their position.
  • Standard blocks (DIV #5) in the normal flow, without any positioning property, are always rendered before positioned elements, and appear below them, even if they come later in the HTML hierarchy. 

understanding_zindex_01.png

 

Example

HTML

<div id="absdiv1">
    <br /><span class="bold">DIV #1</span>
    <br />position: absolute; </div>
<div id="reldiv1">
    <br /><span class="bold">DIV #2</span>
    <br />position: relative; </div>
<div id="reldiv2">
    <br /><span class="bold">DIV #3</span>
    <br />position: relative; </div>
<div id="absdiv2">
    <br /><span class="bold">DIV #4</span>
    <br />position: absolute; </div>
<div id="normdiv">
    <br /><span class="bold">DIV #5</span>
    <br />no positioning </div>

CSS

 .bold {
     font-weight: bold;
     font: 12px Arial;
 }
 #normdiv {
     height: 70px;
     border: 1px dashed #999966;
     background-color: #ffffcc;
     margin: 0px 50px 0px 50px;
     text-align: center;
 }
 #reldiv1 {
     opacity: 0.7;
     height: 100px;
     position: relative;
     top: 30px;
     border: 1px dashed #669966;
     background-color: #ccffcc;
     margin: 0px 50px 0px 50px;
     text-align: center;
 }
 #reldiv2 {
     opacity: 0.7;
     height: 100px;
     position: relative;
     top: 15px;
     left: 20px;
     border: 1px dashed #669966;
     background-color: #ccffcc;
     margin: 0px 50px 0px 50px;
     text-align: center;
 }
 #absdiv1 {
     opacity: 0.7;
     position: absolute;
     width: 150px;
     height: 350px;
     top: 10px;
     left: 10px;
     border: 1px dashed #990000;
     background-color: #ffdddd;
     text-align: center;
 }
 #absdiv2 {
     opacity: 0.7;
     position: absolute;
     width: 150px;
     height: 350px;
     top: 10px;
     right: 10px;
     border: 1px dashed #990000;
     background-color: #ffdddd;
     text-align: center;
 }

Result

(If the image does not display in CodePen, click the Tidy button in the CSS section)

{{ EmbedLiveSample('Example', '', '', '', 'Web/CSS/CSS_Positioning/Understanding_z_index/Stacking_without_z-index') }}

See also

 

Original Document Information

 

Revision Source

<p>« <a href="/en/CSS" title="CSS">CSS</a> « <a href="/en/CSS/Understanding_z-index" title="Understanding CSS z-index"><span class="title">Understanding CSS z-index</span></a></p>

<h3 id="Stacking_without_z-index">Stacking without z-index</h3>

<p>When no element has a z-index, elements are stacked in this order (from bottom to top):</p>

<ol>
 <li>Background and borders of the root element</li>
 <li>Descendant blocks in the normal flow, in order of appearance (in HTML)</li>
 <li>Descendant positioned elements, in order of appearance (in HTML)</li>
</ol>

<p>In the following example, absolute and relatively positioned blocks are properly sized and positioned to illustrate the stacking rules.</p>

<div class="note">
<p><strong>Notes:</strong></p>

<ul>
 <li>Given a homogeneous group of elements without any z-index property, such as the positioned blocks (DIV #1 to #4) in the example, the element's stacking order is their order in the HTML hierarchy, regardless of their position.</li>
 <li>
  <p>Standard blocks (DIV #5) in the normal flow, without any positioning property, are always rendered before positioned elements, and appear below them, even if they come later in the HTML hierarchy.&nbsp;</p>
 </li>
</ul>
</div>

<p><img alt="understanding_zindex_01.png" class="default internal" src="/@api/deki/files/910/=understanding_zindex_01.png" /></p>

<p>&nbsp;</p>

<h2 id="Example" name="Example">Example</h2>

<h3 id="HTML">HTML</h3>

<pre class="brush: html">
&lt;div id="absdiv1"&gt;
&nbsp;&nbsp;&nbsp; &lt;br /&gt;&lt;span class="bold"&gt;DIV #1&lt;/span&gt;
&nbsp;&nbsp;&nbsp; &lt;br /&gt;position: absolute; &lt;/div&gt;
&lt;div id="reldiv1"&gt;
&nbsp;&nbsp;&nbsp; &lt;br /&gt;&lt;span class="bold"&gt;DIV #2&lt;/span&gt;
&nbsp;&nbsp;&nbsp; &lt;br /&gt;position: relative; &lt;/div&gt;
&lt;div id="reldiv2"&gt;
&nbsp;&nbsp;&nbsp; &lt;br /&gt;&lt;span class="bold"&gt;DIV #3&lt;/span&gt;
&nbsp;&nbsp;&nbsp; &lt;br /&gt;position: relative; &lt;/div&gt;
&lt;div id="absdiv2"&gt;
&nbsp;&nbsp;&nbsp; &lt;br /&gt;&lt;span class="bold"&gt;DIV #4&lt;/span&gt;
&nbsp;&nbsp;&nbsp; &lt;br /&gt;position: absolute; &lt;/div&gt;
&lt;div id="normdiv"&gt;
&nbsp;&nbsp;&nbsp; &lt;br /&gt;&lt;span class="bold"&gt;DIV #5&lt;/span&gt;
&nbsp;&nbsp;&nbsp; &lt;br /&gt;no positioning &lt;/div&gt;
</pre>

<h3 id="CSS">CSS</h3>

<pre class="brush: css">
&nbsp;.bold {
&nbsp;&nbsp;&nbsp;&nbsp; font-weight: bold;
&nbsp;&nbsp;&nbsp;&nbsp; font: 12px Arial;
&nbsp;}
&nbsp;#normdiv {
&nbsp;&nbsp;&nbsp;&nbsp; height: 70px;
&nbsp;&nbsp;&nbsp;&nbsp; border: 1px dashed #999966;
&nbsp;&nbsp;&nbsp;&nbsp; background-color: #ffffcc;
&nbsp;&nbsp;&nbsp;&nbsp; margin: 0px 50px 0px 50px;
&nbsp;&nbsp;&nbsp;&nbsp; text-align: center;
&nbsp;}
&nbsp;#reldiv1 {
&nbsp;&nbsp;&nbsp;&nbsp; opacity: 0.7;
&nbsp;&nbsp;&nbsp;&nbsp; height: 100px;
&nbsp;&nbsp;&nbsp;&nbsp; position: relative;
&nbsp;&nbsp;&nbsp;&nbsp; top: 30px;
&nbsp;&nbsp;&nbsp;&nbsp; border: 1px dashed #669966;
&nbsp;&nbsp;&nbsp;&nbsp; background-color: #ccffcc;
&nbsp;&nbsp;&nbsp;&nbsp; margin: 0px 50px 0px 50px;
&nbsp;&nbsp;&nbsp;&nbsp; text-align: center;
&nbsp;}
&nbsp;#reldiv2 {
&nbsp;&nbsp;&nbsp;&nbsp; opacity: 0.7;
&nbsp;&nbsp;&nbsp;&nbsp; height: 100px;
&nbsp;&nbsp;&nbsp;&nbsp; position: relative;
&nbsp;&nbsp;&nbsp;&nbsp; top: 15px;
&nbsp;&nbsp;&nbsp;&nbsp; left: 20px;
&nbsp;&nbsp;&nbsp;&nbsp; border: 1px dashed #669966;
&nbsp;&nbsp;&nbsp;&nbsp; background-color: #ccffcc;
&nbsp;&nbsp;&nbsp;&nbsp; margin: 0px 50px 0px 50px;
&nbsp;&nbsp;&nbsp;&nbsp; text-align: center;
&nbsp;}
&nbsp;#absdiv1 {
&nbsp;&nbsp;&nbsp;&nbsp; opacity: 0.7;
&nbsp;&nbsp;&nbsp;&nbsp; position: absolute;
&nbsp;&nbsp;&nbsp;&nbsp; width: 150px;
&nbsp;&nbsp;&nbsp;&nbsp; height: 350px;
&nbsp;&nbsp;&nbsp;&nbsp; top: 10px;
&nbsp;&nbsp;&nbsp;&nbsp; left: 10px;
&nbsp;&nbsp;&nbsp;&nbsp; border: 1px dashed #990000;
&nbsp;&nbsp;&nbsp;&nbsp; background-color: #ffdddd;
&nbsp;&nbsp;&nbsp;&nbsp; text-align: center;
&nbsp;}
&nbsp;#absdiv2 {
&nbsp;&nbsp;&nbsp;&nbsp; opacity: 0.7;
&nbsp;&nbsp;&nbsp;&nbsp; position: absolute;
&nbsp;&nbsp;&nbsp;&nbsp; width: 150px;
&nbsp;&nbsp;&nbsp;&nbsp; height: 350px;
&nbsp;&nbsp;&nbsp;&nbsp; top: 10px;
&nbsp;&nbsp;&nbsp;&nbsp; right: 10px;
&nbsp;&nbsp;&nbsp;&nbsp; border: 1px dashed #990000;
&nbsp;&nbsp;&nbsp;&nbsp; background-color: #ffdddd;
&nbsp;&nbsp;&nbsp;&nbsp; text-align: center;
&nbsp;}
</pre>

<h3 id="Result">Result</h3>

<p>(If the image does not display in CodePen, click the Tidy button in the CSS section)</p>

<p>{{ EmbedLiveSample('Example', '', '', '', 'Web/CSS/CSS_Positioning/Understanding_z_index/Stacking_without_z-index') }}</p>

<h3 id="See_also">See also</h3>

<ul>
 <li><a href="/en/CSS/Understanding_z-index/Stacking_and_float" title="en/CSS/Understanding_z-index/Stacking_and_float">Stacking and float</a>&nbsp;: How floating elements are handled</li>
 <li><a href="/en/CSS/Understanding_z-index/Adding_z-index" title="en/CSS/Understanding_z-index/Adding_z-index">Adding z-index</a>&nbsp;: Using z-index to change default stacking</li>
 <li><a href="/en/CSS/Understanding_z-index/The_stacking_context" title="en/CSS/Understanding_z-index/The_stacking_context">The stacking context</a>&nbsp;: Notes on the stacking context</li>
 <li><a href="/en/CSS/Understanding_z-index/Stacking_context_example_1" title="en/CSS/Understanding_z-index/Stacking_context_example_1">Stacking context example 1</a>&nbsp;: 2-level HTML hierarchy, z-index on the last level</li>
 <li><a href="/en/CSS/Understanding_z-index/Stacking_context_example_2" title="en/CSS/Understanding_z-index/Stacking_context_example_2">Stacking context example 2</a>&nbsp;: 2-level HTML hierarchy, z-index on all levels</li>
 <li><a href="/en/CSS/Understanding_z-index/Stacking_context_example_3" title="en/CSS/Understanding_z-index/Stacking_context_example_3">Stacking context example 3</a>&nbsp;: 3-level HTML hierarchy, z-index on the second level<span id="cke_bm_94E" style="display:none">&nbsp;</span></li>
</ul>

<p>&nbsp;</p>

<div class="originaldocinfo">
<h3 id="Original_Document_Information" name="Original_Document_Information">Original Document Information</h3>

<ul>
 <li>Author(s): Paolo Lombardi</li>
 <li>This article is the english translation of an article I wrote in italian for <a class="external" href="https://www.yappy.it">YappY</a>. I grant the right to share all the content under <a class="external" href="https://creativecommons.org/licenses/by-sa/2.0/">Creative Commons: Attribution-Sharealike license</a></li>
 <li>Last Updated Date: November 3rd, 2014</li>
</ul>
</div>

<p>&nbsp;</p>
Revert to this revision