<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Haris Shah</title>
    <description>The latest articles on DEV Community by Haris Shah (@hariscs).</description>
    <link>https://dev.to/hariscs</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F635694%2Ffde8966f-0a84-4b3c-b60a-5d6b4aff3646.jpg</url>
      <title>DEV Community: Haris Shah</title>
      <link>https://dev.to/hariscs</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hariscs"/>
    <language>en</language>
    <item>
      <title>Stacking issues with z-index: Solved with isolate.</title>
      <dc:creator>Haris Shah</dc:creator>
      <pubDate>Tue, 06 Sep 2022 18:41:48 +0000</pubDate>
      <link>https://dev.to/hariscs/stacking-issues-with-z-index-solved-with-isolate-5g39</link>
      <guid>https://dev.to/hariscs/stacking-issues-with-z-index-solved-with-isolate-5g39</guid>
      <description>&lt;p&gt;Recently, I was working on a project that has a card with a hover animation running from the bottom right side to the top left side.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F51xzsdp82un6h68uujjd.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F51xzsdp82un6h68uujjd.gif" alt="hover effect" width="600" height="338"&gt;&lt;/a&gt;&lt;br&gt;
At first I created this effect using the HTML &lt;code&gt;pseudo-element ::after&lt;/code&gt; and it worked with the &lt;code&gt;position absolute&lt;/code&gt;, however, it covered all the content inside the card, which was not the effect I was going for.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkywa82zdevp6fxts4c73.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkywa82zdevp6fxts4c73.png" alt="effectcover" width="800" height="239"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So I gave it the property of z-index: -1, but then the animation was behind the card and not visible.&lt;br&gt;
To get it to work I removed the &lt;code&gt;z-index&lt;/code&gt; value from &lt;code&gt;::after&lt;/code&gt; element and instead assigned &lt;code&gt;z-index: 1;&lt;/code&gt; to all of the elements inside the card. This time the &lt;code&gt;hover effect&lt;/code&gt; was showing up on the &lt;code&gt;heading&lt;/code&gt; and &lt;code&gt;img&lt;/code&gt; elements but not on the &lt;code&gt;p&lt;/code&gt; element. Upon searching, I discovered that we have to make the &lt;code&gt;p&lt;/code&gt; element &lt;code&gt;position:relative&lt;/code&gt;. After I changed the &lt;code&gt;p&lt;/code&gt; tag to &lt;code&gt;position:relative&lt;/code&gt;, the effect displays perfectly.&lt;br&gt;
But if we look at this approach, it is not good practice to change a lot of CSS to achieve this effect.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;code Screenshot&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh27l6coawyhyatyqk7uf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh27l6coawyhyatyqk7uf.png" alt="Code before using isolate" width="800" height="339"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Several days later, I was browsing YouTube when I saw a short &lt;a href="https://www.youtube.com/shorts/NXu2vN052xM"&gt;video&lt;/a&gt; from Kevin Powell explaining the CSS property &lt;code&gt;isolation:isolate&lt;/code&gt;. Watching that video, I was amazed at how easy it was to create this effect without writing much CSS.&lt;br&gt;
The &lt;code&gt;isolate&lt;/code&gt; property creates a new &lt;code&gt;stacking context&lt;/code&gt;. When we declare the parent &lt;code&gt;isolate&lt;/code&gt;, it locks the content inside it. Now if we make the &lt;code&gt;::after&lt;/code&gt; element &lt;code&gt;z-index of -1&lt;/code&gt; it will be only negative to its siblings not the entire container!&lt;br&gt;
I removed all &lt;code&gt;z-indexes&lt;/code&gt; from card elements and declared only one CSS property &lt;code&gt;isolate&lt;/code&gt; on the card.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp6e04el9pdjnqrx1f6m6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp6e04el9pdjnqrx1f6m6.png" alt="after using isolate" width="800" height="339"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1mp5ifonau0pnfn9u8hn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1mp5ifonau0pnfn9u8hn.png" alt="after" width="800" height="264"&gt;&lt;/a&gt;&lt;br&gt;
We can now see that the code is much cleaner and does not have any additional side effects.&lt;/p&gt;

&lt;p&gt;Thanks for reading this. Please do share your feedback &amp;amp; suggestions in the comments section.&lt;/p&gt;

</description>
      <category>css</category>
      <category>html</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
