<?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: Marwan Ahmed</title>
    <description>The latest articles on DEV Community by Marwan Ahmed (@marwanahmed).</description>
    <link>https://dev.to/marwanahmed</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%2F1246419%2F1448eee3-f629-4432-b4c6-e4e3cbbc79c1.jpeg</url>
      <title>DEV Community: Marwan Ahmed</title>
      <link>https://dev.to/marwanahmed</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/marwanahmed"/>
    <language>en</language>
    <item>
      <title>Ending z-index confusion, once and for all.</title>
      <dc:creator>Marwan Ahmed</dc:creator>
      <pubDate>Tue, 02 Sep 2025 19:32:31 +0000</pubDate>
      <link>https://dev.to/marwanahmed/ending-z-index-confusion-once-and-for-all-5gje</link>
      <guid>https://dev.to/marwanahmed/ending-z-index-confusion-once-and-for-all-5gje</guid>
      <description>&lt;p&gt;Yes, we've all been there... writing &lt;code&gt;z-index: 99999;&lt;/code&gt; and almost smashing the screen because one element isn't being put on top of another.&lt;/p&gt;

&lt;p&gt;Before you start reading, if your goal is to skim through the article in order to find a quick fix for your bug, I'm sorry to disappoint you (I'm not actually), you won't find what you're looking for. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The goal of this article is to make you super confident the next time you're adding random numbers in a &lt;code&gt;z-index&lt;/code&gt;... that your solution won't work!&lt;/strong&gt;&lt;/p&gt;




&lt;p&gt;First, let's mention two basic but very important rules when it comes to stacking element on top of each other:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;If an element does not have a defined &lt;code&gt;z-index&lt;/code&gt;, it will have a value of &lt;code&gt;z-index: auto&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In order for &lt;code&gt;z-index&lt;/code&gt; to work, we must define a &lt;code&gt;position&lt;/code&gt; value other than &lt;code&gt;static&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;z-index&lt;/code&gt; will work with flex and grid without defining a &lt;code&gt;position&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Elements further down the document sit on top of elements that appear before them.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In order to understand the problems with &lt;code&gt;z-index&lt;/code&gt;, it's crucial that we grasp a very important concept called &lt;strong&gt;Stacking Contexts&lt;/strong&gt;.&lt;/p&gt;




&lt;h4&gt;
  
  
  Stacking Contexts
&lt;/h4&gt;

&lt;p&gt;Stacking contexts is the reason why your &lt;code&gt;99999&lt;/code&gt; doesn't work. So, what in the world is that thing that is causing developers pain?&lt;/p&gt;

&lt;p&gt;Let's make this simple using a dumb analogy. Let's assume that we're comparing the ages of parents and children. We have 2 fathers and each father has 2 sons. Ahmed has Omar (yes, it's my brother's name) and Marwan. Mark has John and Jack.&lt;/p&gt;

&lt;p&gt;The rules are that we cannot compare the age of a child to the age of a father, and we cannot compare the age of a child to the age of a child of another father. Meaning we can only compare either the ages of the two fathers (Ahmed and Mark), or the ages of two brothers.&lt;/p&gt;

&lt;p&gt;When a father has his first child, he starts creating a family (stacking context). It's logically impossible for one of his children to be older than him. His children cannot outgrow him no matter how tall they are or how much weight they put on (&lt;code&gt;z-index: 99999;&lt;/code&gt;).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="position-relative z-index-2" id="Ahmed"&amp;gt;
   &amp;lt;p class="position-relative z-index-10" id="Marwan"&amp;gt;&amp;lt;/p&amp;gt;
   &amp;lt;p class="position-relative z-index-1000" id="Omar"&amp;gt;&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;

&amp;lt;div class="position-relative z-index-1" id="Mark"&amp;gt;
   &amp;lt;p class="position-relative z-index-10" id="John"&amp;gt;&amp;lt;/p&amp;gt;
   &amp;lt;p class="position-relative z-index-1000" id="Jack"&amp;gt;&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;code&gt;div&lt;/code&gt; with &lt;code&gt;id&lt;/code&gt; &lt;code&gt;Ahmed&lt;/code&gt; creates a stacking context when it has &lt;code&gt;position: relative&lt;/code&gt; and &lt;code&gt;z-index: 2&lt;/code&gt;, so it starts having its own family. &lt;/p&gt;

&lt;p&gt;So, the child with &lt;code&gt;id&lt;/code&gt; &lt;code&gt;Omar&lt;/code&gt;, will never grow above its parent, although he has a much higher &lt;code&gt;z-index&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In the same family, we can compare both brothers, so &lt;code&gt;Omar&lt;/code&gt; is above &lt;code&gt;Marwan&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now, what if we want to compare &lt;code&gt;Ahmed&lt;/code&gt; with &lt;code&gt;Mark&lt;/code&gt;? Since they both created their own families (stacking contexts), they're both fathers, so we can compare them. In this case, &lt;code&gt;Ahmed&lt;/code&gt; will be above &lt;code&gt;Mark&lt;/code&gt;, since &lt;code&gt;Ahmed&lt;/code&gt; has a higher &lt;code&gt;z-index&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;How about comparing &lt;code&gt;Mark&lt;/code&gt; with &lt;code&gt;Omar&lt;/code&gt;? As mentioned above, we cannot compare fathers to children of other fathers. In this case, we will have to compare &lt;code&gt;Mark&lt;/code&gt; to the &lt;code&gt;Omar&lt;/code&gt;'s father &lt;code&gt;Ahmed&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is the gist of stacking contexts and why sometimes higher &lt;code&gt;z-index&lt;/code&gt; doesn't make any difference.&lt;/p&gt;

&lt;p&gt;There are scenarios where children can be above their parents. This is when the parent doesn't have a &lt;code&gt;position&lt;/code&gt; other than &lt;code&gt;static&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;In this case, the parent did not create a new stacking context, so the child is being compared in the &lt;strong&gt;root stacking context&lt;/strong&gt;, even if the parent has a &lt;code&gt;z-index&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Referring to our analogy, it's as if the child &lt;code&gt;Omar&lt;/code&gt; has grown to be a father and is now being compared to other fathers.&lt;/p&gt;




&lt;h4&gt;
  
  
  Solutions
&lt;/h4&gt;

&lt;p&gt;Knowing the problem is half the solution. Since the problem is now clear, you might think "Well, if the problem is that the child is trapped inside it's parent's stacking context, maybe the solution is to find a way to move the child outside". You'd be exactly right, and that's what Vue and React teams have done when they created &lt;code&gt;Portal&lt;/code&gt; and &lt;code&gt;Teleport&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Portal&lt;/code&gt; and &lt;code&gt;Teleport&lt;/code&gt; are used to teleport a certain element (dropdown, modals, popovers) to the &lt;code&gt;body&lt;/code&gt;, in order to free it from it's parent's stacking context.&lt;/p&gt;

&lt;p&gt;Other solutions might include restructuring your html, playing with elements' &lt;code&gt;position&lt;/code&gt; property, you get the idea.&lt;/p&gt;




&lt;h4&gt;
  
  
  Summary
&lt;/h4&gt;

&lt;p&gt;Here's a flowchart that summarizes the above, and helps you grasp the concept clearly. (I asked ChatGPT to do it but it failed miserably so I did it. The disappointments of AI...)&lt;/p&gt;

&lt;p&gt;Hopefully that's the end of random &lt;code&gt;9&lt;/code&gt;'s thrown around in your code.&lt;/p&gt;

&lt;p&gt;Maybe you can even show-off to your team and explain to them what you've understood 😉.&lt;/p&gt;

&lt;p&gt;If you have any questions or any feedback, drop them below 👇. I'd love to hear from you.&lt;/p&gt;

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

</description>
      <category>css</category>
      <category>ui</category>
      <category>frontend</category>
      <category>webdev</category>
    </item>
    <item>
      <title>NPM Packages: To install or not to install</title>
      <dc:creator>Marwan Ahmed</dc:creator>
      <pubDate>Tue, 02 Jan 2024 08:27:35 +0000</pubDate>
      <link>https://dev.to/marwanahmed/npm-packages-to-install-or-not-to-install-26kg</link>
      <guid>https://dev.to/marwanahmed/npm-packages-to-install-or-not-to-install-26kg</guid>
      <description>&lt;br&gt;
  







&lt;p&gt;&lt;em&gt;Are you a developer who installs A LOT of packages in your projects? &lt;br&gt;
Are you a developer who occasionally installs some packages?&lt;br&gt;
Are you a developer who never installs packages and builds everything from scratch?&lt;br&gt;
Well, either way, this is for you! This article is an attempt to strike the fine balance of when and when not to install an npm package.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;First off, whether you own a business and your app contains a lot of third-party libraries, or you’re a developer whose job involves a lot of npm installing, your future doesn’t look particularly bright.&lt;/p&gt;

&lt;p&gt;In order to discover how to decide on installing a package, let’s first breakdown why making it a habit to install packages is bad for you.&lt;/p&gt;

&lt;p&gt;Suppose you’re a business-owner whose business is booming. Your e-commerce website is doing great and gaining a lot of traffic. One day,  you open the website and you find it completely broken. &lt;/p&gt;

&lt;p&gt;After some investigation, you find out that the app’s code, which is built-up from numerous packages, is not working anymore. &lt;/p&gt;

&lt;p&gt;You later find out that the person who built one of your packages that the app’s dependent on has decided to shut it down. Basically your entire business was resting on the whims of some developer.&lt;/p&gt;

&lt;p&gt;If you’re thinking that this is a bit far-fetched, this has actually happened before, &lt;a href="https://www.revenera.com/blog/software-composition-analysis/the-story-behind-colors-js-and-faker-js/"&gt;check this&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now let’s look at some more probable disadvantages of npm packages:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.Cost&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;While it may seem so, installing packages is not actually for free.&lt;br&gt;&lt;br&gt;
The thing you pay the most is performance. The performance of your app is directly affected by the size of the package. By performance I mean, the loading time and the responsiveness of app (how fast the app responds to user interaction)&lt;/p&gt;

&lt;p&gt;This is by no means a cheap cost. The app performance can be the difference between a business's success and failure.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2.Security Risk&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you have a habit of installing a lot of packages, chances are your app will be prone to security issues. Because you’re basically adding a lot of code to your app that you don’t really know a lot about. Some of the code might have security vulnerabilities which can be exploited by hackers.&lt;br&gt;
So if you’re a business-owner that’s a very dangerous thing to have.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3.Learning Curve&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As a passionate-learner, this is for me by far the biggest disadvantage. Regularly installing packages can turn you into a lazy, incompetent developer. After all, it’s not very hard to type &lt;code&gt;npm install&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;When I first knew about npm packages, it was like magic for me. I thought people who built packages were like developers who only worked for mega companies. I could picture them wearing these hoodies, putting on their nerdy glasses while working with the lights off. &lt;br&gt;
Later, I found a package that’s whole purpose is to check whether a number was even or odd, and that was pretty much it. &lt;/p&gt;

&lt;p&gt;The moment I realized that developers who build packages are actually normal people, and that I have the capability to build any package I want, shifted my mindset hugely. I’m actually now building my own package which is something that, at some time, I thought I would be able to do when I’m close to retirement.&lt;/p&gt;

&lt;p&gt;Instead of installing a package, why not make it? &lt;br&gt;
Instead of just using a package from its documentation, why not read its code? Actually, I would argue now it’s a must to perform thorough code review for the package you’re using.&lt;/p&gt;

&lt;p&gt;You will learn a lot, whether by taking in different ideas and being encouraged to make your own package, or by being trained to look for things that you can improve on in a piece of code. And of course, you will have full knowledge of the ins and outs of your application.&lt;/p&gt;

&lt;p&gt;So, if you’re used to installing packages in order just to get things done, as time will pass you will find yourself way behind the pack.&lt;/p&gt;




&lt;p&gt;You might be thinking now, ‘wow packages are really bad, and if they’re so bad why are people making them?!’. Well, that’s not actually the case.&lt;/p&gt;

&lt;p&gt;Packages are a way of developers helping each other. You can learn a lot from other people’s code as well as saving time while building your app.&lt;/p&gt;

&lt;p&gt;But the number one benefit from npm packages is &lt;strong&gt;Time&lt;/strong&gt;. Packages save developers hours and hours of time building everything from scratch.&lt;/p&gt;

&lt;p&gt;But I’ve just mentioned above that it’s better to build your own code for learning’s sake so how does that work?!&lt;/p&gt;

&lt;p&gt;Actually, while working in a company, most of the time you won’t have the luxury of time to build everything from scratch, as companies always want fast development in order to generate fast revenue. So, you will find yourself thinking a lot; should I install a package for this feature or should I do it from scratch?&lt;/p&gt;

&lt;p&gt;Let’s talk about the factors that can help us decide on the matter:&lt;/p&gt;

&lt;p&gt;The most important factor is &lt;strong&gt;time&lt;/strong&gt;. Time is basically how urgent the business request is vs how much time it would take to build it. If you find yourself in a very tight time-frame and the feature is somehow complex, then the reasonable decision would be to install a package (but choose wisely!).&lt;/p&gt;

&lt;p&gt;Another situation where it would be more reasonable to install a package is if the feature you’re building requires a huge learning curve. I mean let’s not kid ourselves here you’re probably not gonna build your charts from scratch!&lt;/p&gt;

&lt;p&gt;But let’s not stop here. If you find yourself having to install a package, after you finish the feature, make a plan to build it from scratch yourself, whether to replace the one you installed, or even if you build it on your own time for learning’s sake. &lt;/p&gt;




&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;The next time you’re not sure whether or not to install a package, think of the factors we mentioned; &lt;strong&gt;Cost&lt;/strong&gt;, &lt;strong&gt;Security Risk&lt;/strong&gt;, &lt;strong&gt;Learning Curve&lt;/strong&gt; and &lt;strong&gt;Time&lt;/strong&gt;, and make a decision. &lt;br&gt;
If you decided on installing the package, remember to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Choose the package wisely&lt;/li&gt;
&lt;li&gt;Review the package’s code&lt;/li&gt;
&lt;li&gt;Make a plan to build it from scratch &lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>npm</category>
      <category>programming</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
