<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ Mahesh Patidar - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ Mahesh Patidar - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Sun, 14 Jun 2026 10:14:41 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/author/ali6nx404/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How the z-index Property Works Under the Hood ]]>
                </title>
                <description>
                    <![CDATA[ The z-index property might seem simple, but it's not just about using a positive or negative integer. There's a lot more going on under the hood, and some quirks can cause problems when you're working with it. In this article, we're going to dive dee... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-the-z-index-property-works-under-the-hood/</link>
                <guid isPermaLink="false">66d45d987df3a1f32ee7f7d3</guid>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Mahesh Patidar ]]>
                </dc:creator>
                <pubDate>Fri, 14 Jan 2022 11:06:00 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2022/01/freecodecamp.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>The z-index property might seem simple, but it's not just about using a positive or negative integer.</p>
<p>There's a lot more going on under the hood, and some quirks can cause problems when you're working with it.</p>
<p>In this article, we're going to dive deep into the z-index property and learn how it really works.</p>
<p>So grab a cup of coffee or tea, and be ready with a pen and paper 😉</p>
<p>z-index controls the stacking order and creates the stacking context. So first let's understand what these terms mean.</p>
<h2 id="heading-stacking-order">Stacking Order</h2>
<p>When a browser is rendering your web page elements along the z-axis it has to choose which element to draw on the canvas first.</p>
<p>And the order of those elements is called stacking order.</p>
<p>Before we move further and talk about stacking order with the z-index property, let's understand what default stacking looks like:</p>
<ol>
<li><p>backgrounds and borders of the root element.</p>
</li>
<li><p>non-positioned block elements (in order of appearance)</p>
</li>
<li><p>non-positioned floating elements (in order of appearance)</p>
</li>
<li><p>inline elements (in order of appearance)</p>
</li>
<li><p>positioned elements (in order of appearance)</p>
</li>
</ol>
<p>Root element Background and borders represent the lowest level of the stack. Positioned elements represent the highest level of the stack.</p>
<blockquote>
<p>Note: a positioned element is an element that's either relative, fixed, absolute, or sticky – anything other than static.</p>
</blockquote>
<div class="embed-wrapper"><iframe height="487" style="width:100%" src="https://codepen.io/ali6nx404/embed/gOGdJEY?default-tab=result&amp;theme-id=dark" title="Embedded content" loading="lazy">
  See the Pen <a href="https://codepen.io/ali6nx404/pen/gOGdJEY">
  Untitled</a> by Mahesh Patidar (<a href="https://codepen.io/ali6nx404">@ali6nx404</a>)
  on <a href="https://codepen.io">CodePen</a>.
</iframe></div>

<p>A non-positioned element is below the inline element and positioned element. If we change the code and add relative position into the second div then it will hide the inline element.</p>
<h2 id="heading-how-the-z-index-property-works">How the z-index property works</h2>
<p>The z-index property is used to change the default stacking order of elements.</p>
<p>The z-index can take one of 3 values:</p>
<ol>
<li><p><strong>auto</strong>: The stack order of the element is the same as that of its parent element. This is the default value.</p>
</li>
<li><p><strong>integer</strong>: it can be a positive or negative number.</p>
</li>
<li><p><strong>inherit</strong>: sets the value the same as the parent element's value.</p>
</li>
</ol>
<blockquote>
<p>z-index only works with a positioned element. (Well, this is not completely true because there are some edge cases too – but we will discuss those below.)</p>
</blockquote>
<p>The highest positive z-index value means the one nearest to the user, and the lowest negative z-index value means the one farthest from the user.</p>
<div class="embed-wrapper"><iframe height="497" style="width:100%" src="https://codepen.io/ali6nx404/embed/GRMXVwd?default-tab=result&amp;theme-id=dark" title="Embedded content" loading="lazy">
  See the Pen <a href="https://codepen.io/ali6nx404/pen/GRMXVwd">
  Negative z-index</a> by Mahesh Patidar (<a href="https://codepen.io/ali6nx404">@ali6nx404</a>)
  on <a href="https://codepen.io">CodePen</a>.
</iframe></div>

<p>The above codepen example has the same code as the default stacking order example, but I just changed a single line in the 3rd element code.</p>
<p><code>z-index: -1;</code></p>
<p>I added a negative z-index, so it hides positioned elements below the non-positioned element.</p>
<p>When we specify the z-index on the positioned element, it creates a stacking context.</p>
<p>The simple z-index property gets a bit complicated with the introduction of the stacking context.</p>
<h2 id="heading-stacking-context">Stacking context</h2>
<p>Groups of elements with a common parent that move forward or backward together in the stacking order make up what is called a stacking context. (Official Definition from <a target="_blank" href="https://drafts.csswg.org/css2/#propdef-z-index">w3.org</a>.)</p>
<blockquote>
<p>The root element (HTML) forms the root stacking context. Other stacking contexts are generated by any positioned element (including relatively positioned elements) having a computed value of ‘z-index’ other than ‘auto’. Stacking contexts are not necessarily related to containing blocks.</p>
</blockquote>
<p>I hope this definition makes sense.</p>
<p>The HTML tag forms the root stacking context and by default, all elements belong to this stacking context.</p>
<p>But by using positioned elements and a z-index other than the "auto" value, we can create a local stacking context. That element is known as the root element in the local stacking context.</p>
<p>The parent element's children belong to that local stacking context and they move backward and forward together in the stacking order.</p>
<h3 id="heading-why-we-need-to-know-stacking-context">Why we need to know stacking context</h3>
<p>So why should you learn about all of these things if you can simply use the z-index by giving positive or negative values?</p>
<p>Let me give you a simple problem:</p>
<div class="embed-wrapper"><iframe height="424" style="width:100%" src="https://codepen.io/ali6nx404/embed/gOGBovo?default-tab=result&amp;theme-id=dark" title="Embedded content" loading="lazy">
  See the Pen <a href="https://codepen.io/ali6nx404/pen/gOGBovo">
  stacking context example</a> by Mahesh Patidar (<a href="https://codepen.io/ali6nx404">@ali6nx404</a>)
  on <a href="https://codepen.io">CodePen</a>.
</iframe></div>

<p>We have 3 divs with z-index values of 1, 999, and 2. But the confusing part is the second div which has a 999 z-index value (the highest value) – and it is still not able to come in front of the third div.</p>
<p>This is where our understanding comes in handy: this all happens because of stacking context.</p>
<p>Let's find out why.</p>
<p>Taka a look at the HTML:</p>
<pre><code class="lang-html">   <span class="hljs-tag">&lt;<span class="hljs-name">main</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"first"</span>&gt;</span>1<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"second"</span>&gt;</span>999<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
   <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span>
   <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"third"</span>&gt;</span>2<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>We have three divs with class names of first, second, and third. The first and second div are wrapped into the main tag.</p>
<p>Here's the CSS:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.first</span> {
  <span class="hljs-attribute">z-index</span>: <span class="hljs-number">1</span>;
}

<span class="hljs-selector-class">.second</span> {
  <span class="hljs-attribute">z-index</span>: <span class="hljs-number">999</span>;
}

<span class="hljs-selector-class">.third</span> {
  <span class="hljs-attribute">z-index</span>: <span class="hljs-number">2</span>;
}
</code></pre>
<p>You can see that the second div has the highest z-index value. still, it's not able to come in front of the third div.</p>
<p>All this is happening because of this line of code👇</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">main</span> {
  <span class="hljs-attribute">position</span>: relative;
  <span class="hljs-attribute">z-index</span>: <span class="hljs-number">1</span>;
}
</code></pre>
<p>The main element is creating a local stacking context and the first and second div belong to the same local stacking context – but the third div doesn't.</p>
<p>That's why the second div is able to come in front of the first: because it has a z-index value of 999 (higher than the first div).</p>
<p>The main tag and third div are the children of the root element, both having the z-index values.</p>
<p>The main element has a z-index value of 1 and the third has a z-index value of 2. The third has the higher z-index value which is why the third div is able to come in front of the main tag.</p>
<p>I hope you now understand how stacking context works and why you need to know about it.</p>
<h2 id="heading-how-to-create-stacking-contexts">How to Create Stacking Contexts</h2>
<p>We've seen how we can combine positioning (especially relative and absolute) with the z-index to create a stacking context – but that's not the only way! Here are some others:</p>
<ol>
<li><p>using position fixed or sticky (No z-index needed for these values!)</p>
</li>
<li><p>Adding a z-index value to a child within a display: flex or display: grid container</p>
</li>
<li><p>adding an opacity value of less than 1</p>
</li>
</ol>
<p>These are some of the common ways to do this, but you can check out the full list <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Positioning/Understanding_z_index/The_stacking_context">here</a>.</p>
<p>I told you earlier that the z-index actually doesn't just work with positioned elements – there are some edge cases.</p>
<p>The z-index property works well with grid and flex container children without specifying positioning.</p>
<p>Check out the below example and code:</p>
<div class="embed-wrapper"><iframe height="453" style="width:100%" src="https://codepen.io/ali6nx404/embed/JjrmvNd?default-tab=result&amp;theme-id=dark" title="Embedded content" loading="lazy">
  See the Pen <a href="https://codepen.io/ali6nx404/pen/JjrmvNd">
  z-index with grid</a> by Mahesh Patidar (<a href="https://codepen.io/ali6nx404">@ali6nx404</a>)
  on <a href="https://codepen.io">CodePen</a>.
</iframe></div>

<p>This works because adding a z-index value to a child within a grid or flex container forms a stacking context.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>I know these things are confusing and a little bit complex.</p>
<p>I hope after reading this article and practicing with the z-index property you understood how things work behind the scenes.</p>
<p>But if you are still confused then you can contact me <a target="_blank" href="https://twitter.com/Ali6nX404">here</a>.</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
