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 1065952 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: 1065952
  • Created:
  • Creator: javichito
  • Is current revision? No
  • Comment

Revision Content

« CSS « UNDERSTANDING CSS Z-INDEX

Stacking without z-index

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

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

En the following example, the blocks with absolute and relative positions are properly sized and positioned to illustrate the stacking rules.

Notes:

  • Given homogeneous group of elements with no z-index property, such as the posiitoned blocks (DIV #1 to #4) in the example, the stacking order of the elements is its order in the HTML hierarchy, regardless its position.
  • Standar blocks (DIV #5) in the normal flow, with no position property, are always redered before the positioned elements and appear below of 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 doesn't appear in CodePen, click in the Tidy button from 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="Apilando_sin_z-index">Stacking without z-index</h3>

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

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

<p>En the following example, the blocks with absolute and relative positions are properly sized and positioned to illustrate the stacking rules.</p>

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

<ul>
 <li>Given homogeneous group of elements with no z-index property, such as the posiitoned blocks&nbsp;(DIV #1 to&nbsp;#4) in the example, the stacking order of the elements is its order in the HTML hierarchy, regardless its position.</li>
 <li>
  <p>Standar blocks&nbsp;(DIV #5) in the normal flow, with no position property, are always redered before the positioned elements and appear below of them, even if they come later in the HTML hierarchy.</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="Resultado">Result</h3>

<p>(If the image doesn't appear in CodePen, click in the Tidy button from the CSS section)</p>

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

<h3 id="También_puedes_ver">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&nbsp;<a href="https://www.yappy.it/">YappY</a>. I grant the right to share all the content under&nbsp;<a href="https://creativecommons.org/licenses/by-sa/2.0/">Creative Commons: Attribution-Sharealike license</a></li>
 <li>Last Updated Date:&nbsp;November 3rd, 2014</li>
</ul>
</div>

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