<?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" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ayham Al Attar on Medium]]></title>
        <description><![CDATA[Stories by Ayham Al Attar on Medium]]></description>
        <link>https://medium.com/@ayham.alattar?source=rss-77d0ca348158------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*lxq_KTzD_jtUAvKxfz6pjQ.jpeg</url>
            <title>Stories by Ayham Al Attar on Medium</title>
            <link>https://medium.com/@ayham.alattar?source=rss-77d0ca348158------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 03:48:07 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ayham.alattar/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Four shapes of async Javascript]]></title>
            <link>https://medium.com/@ayham.alattar/four-shapes-of-async-javascript-fc89a8c2a40e?source=rss-77d0ca348158------2</link>
            <guid isPermaLink="false">https://medium.com/p/fc89a8c2a40e</guid>
            <category><![CDATA[behaviorsubject]]></category>
            <category><![CDATA[promises]]></category>
            <category><![CDATA[asynchronous]]></category>
            <category><![CDATA[async]]></category>
            <category><![CDATA[observables]]></category>
            <dc:creator><![CDATA[Ayham Al Attar]]></dc:creator>
            <pubDate>Sat, 13 Jun 2026 07:24:12 GMT</pubDate>
            <atom:updated>2026-06-13T07:41:35.026Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rDTjuTOdHFV8aWp9OqBnRw.jpeg" /></figure><p>A huge amount of frontend complexity comes from some objects that all seem to do the same thing: Promise, Observable, Subject, and BehaviorSubject. They all deal with asynchronous data. They all deliver values. They all appear everywhere in modern frontend applications and answer the same question: <em>“How do I handle data that isn’t ready yet?”</em> The difference is how many values, when they start, and who’s listening.</p><h3>Mental Model</h3><p>Imagine a pizza shop. Everything you need to know about these four concepts happens inside it:</p><p><strong>Promise — one pizza, cooked the moment you order</strong></p><blockquote><em>The oven fires up the instant you order. You get exactly one pizza, once. Even if you never answer the door, it was still made (it’s </em>eager<em> — it runs whether or not you’re listening).</em></blockquote><p><strong>Observable — a meal-plan subscription</strong></p><blockquote><em>Nothing is cooked until you sign up (</em><em>subscribe). Then pizzas arrive over time, each subscriber gets their own batch, and you can cancel anytime (it&#39;s </em>lazy<em> — no subscriber, no cooking).</em></blockquote><p><strong>Subject — the counter loudspeaker</strong></p><blockquote><em>Staff shout “Fresh slices ready!” and everyone in the room hears it at the same moment. Walk in late? You missed that announcement — it’s gone forever.</em></blockquote><p><strong>BehaviorSubject — the menu board at the entrance</strong></p><blockquote><em>Always shows the current special of the day. Anyone who walks in — even hours late — instantly sees the latest special, and it updates live for everyone when it changes.</em></blockquote><p>Keep this pizza shop in mind, every section below maps straight back to it.</p><h3>Deep Dive</h3><h4>1. Promise — ordering one pizza (eager, single-shot)</h4><p>The moment you create a Promise, its work begins (it’s eager, the oven is already on). It settles exactly once (resolved or rejected) and then it&#39;s frozen forever. You can&#39;t cancel it, and you can&#39;t get a second value out of it.</p><p><strong>Quick note:</strong> Promises are built into JavaScript itself. The other three come from RxJS, a reactive programming library used across the frontend ecosystem.</p><p><em>“A Promise delivers one value, once, and starts working immediately.”</em></p><pre>const pizza = new Promise((resolve) =&gt; {<br> console.log(‘Oven is ON immediately!’); // runs right away<br> setTimeout(() =&gt; resolve(‘🍕’), 2000);<br>});<br><br>pizza.then(p =&gt; console.log(&#39;Delivered:&#39;, p));</pre><h4>2. Observable — the meal-plan subscription (lazy, multi-value, cancellable)</h4><p>An Observable is just a blueprint, a flyer for the meal plan. Nothing is cooked until .subscribe() is called (it&#39;s lazy). It can emit zero, one, or infinite values, and you can stop it anytime with unsubscribe(). Crucially, each subscriber triggers a fresh execution, two subscribers, two separate batches of pizzas (this is called <em>unicast</em>).</p><p><em>“An Observable delivers many values over time, and only starts when someone subscribes.”</em></p><pre>import { interval } from ‘rxjs’;<br><br>const mealPlan$ = interval(1000); // just a flyer - nothing cooks yet!<br>const sub = mealPlan$.subscribe(n =&gt; console.log(&#39;Pizza #&#39;, n)); // NOW the kitchen starts<br><br>setTimeout(() =&gt; sub.unsubscribe(), 5000); // cancel the plan anytime</pre><h4>3. Subject — the counter loudspeaker (manual push, multicast)</h4><p>A Subject is both an Observable (you can listen to it) and an Observer (you can call .next() to make an announcement). All subscribers share one execution (<em>multicast</em>), one shout, everyone hears it. But there&#39;s a catch: walk in late, and past announcements are gone.</p><p><em>“A Subject is an Observable you can push values into manually — and broadcast to many listeners”</em></p><pre>import { Subject } from ‘rxjs’;<br><br>const loudspeaker$ = new Subject&lt;string&gt;();<br><br>loudspeaker$.subscribe(msg =&gt; console.log(&#39;Alice hears:&#39;, msg));<br>loudspeaker$.next(&#39;Fresh slices ready!&#39;); // Alice hears it<br><br>loudspeaker$.subscribe(msg =&gt; console.log(&#39;Bob hears:&#39;, msg)); // Bob walks in now<br>loudspeaker$.next(&#39;Last call for garlic bread!&#39;); // both hear this - but Bob missed the first one</pre><h4>4. BehaviorSubject — the menu board (memory included)</h4><p>A BehaviorSubject requires an initial value (the board is never blank) and always stores the most recent one. Every new subscriber immediately receives the current value, then all future updates. You can even read it synchronously via .getValue().</p><p><em>“A BehaviorSubject is a Subject that remembers its latest value and hands it to anyone who joins late.”</em></p><pre>import { BehaviorSubject } from ‘rxjs’;<br><br>const menuBoard$ = new BehaviorSubject&lt;string&gt;(&#39;Margherita&#39;); // initial special required<br><br>menuBoard$.subscribe(s =&gt; console.log(&#39;Alice sees:&#39;, s)); // Alice sees: Margherita (instantly!)<br>menuBoard$.next(&#39;Pepperoni&#39;); // Alice sees: Pepperoni<br><br>menuBoard$.subscribe(s =&gt; console.log(&#39;Bob sees:&#39;, s)); // Bob sees: Pepperoni (got the latest!)</pre><h3>📊 Quick Comparison</h3><p><strong>Promise</strong></p><ul><li>Pizza-shop role: one pizza order</li><li>Number of values: exactly 1</li><li>Starts: immediately (eager)</li><li>Cancellable: ❌</li><li>Push values manually: ❌</li><li>Late subscribers get: the result (always)</li><li>Initial value: none</li><li>Cast type: none</li></ul><p><strong>Observable:</strong></p><ul><li>Pizza-shop role: meal-plan subscription</li><li>Number of values: 0 → ∞</li><li>Starts: on subscribe (lazy)</li><li>Cancellable: ✅</li><li>Push values manually: ❌</li><li>Late subscribers get: a fresh execution</li><li>Initial value: none</li><li>Cast type: unicast</li></ul><p><strong>Subject:</strong></p><ul><li>Pizza-shop role: loudspeaker</li><li>Number of values: 0 → ∞</li><li>Starts: when you call .next()</li><li>Cancellable: ✅</li><li>Push values manually: ✅ .next()</li><li>Late subscribers get: nothing past</li><li>Initial value: none</li><li>Cast type: multicast</li></ul><p><strong>BehaviorSubject:</strong></p><ul><li>Pizza-shop role: menu board</li><li>Number of values: 0 → ∞</li><li>Starts: when you call .next()</li><li>Cancellable: ✅</li><li>Push values manually: ✅ .next()</li><li>Late subscribers get: the latest value</li><li>Initial value: required</li><li>Cast type: multicast</li></ul><h3>Common Mistakes &amp; Pitfalls</h3><p><strong>1. Forgetting to unsubscribe</strong> <strong>→ memory leaks.</strong> Cancel your meal plan when you move out! Long-lived Observables keep running after your component or page is gone. Store the subscription and unsubscribe in your framework’s teardown hook (useEffect cleanup in React, onUnmounted in Vue, ngOnDestroy in Angular):</p><pre>const sub = mealPlan$.subscribe(render);<br><br>// later, when the component unmounts:<br>sub.unsubscribe(); // ✅ no leak</pre><p><strong>2. Exposing the Subject itself from a store or service.</strong> If you hand out the raw Subject, anyone can grab the loudspeaker and shout nonsense (.next()), corrupting your state. Expose it as a read-only Observable instead:</p><pre>const _menuBoard = new BehaviorSubject&lt;string&gt;(‘Margherita’); // private - staff only<br>export const menuBoard$ = _menuBoard.asObservable(); // public - read-only ✅</pre><p><strong>3. Expecting a Subject to replay old values.</strong> The loudspeaker has no memory. If late subscribers need history, use BehaviorSubject (last value) or ReplaySubject (last N values).</p><p><strong>4. Thinking Observables run without a subscriber.</strong> A meal-plan flyer feeds no one. Creating an Observable does nothing on its own, no subscribe, no execution. This trips up almost every beginner.</p><h3>When to Use Which?</h3><ul><li><strong>Promise </strong>→ A truly one-time async operation, especially with async/await. <em>Examples: a </em><em>fetch() call, reading from a browser API, loading a config file once.</em></li><li><strong>Observable </strong>→ Anything that emits multiple values over time, or where you want operators like map, debounceTime, switchMap. <em>Examples: search-box input, WebSocket messages, scroll/resize events.</em></li><li><strong>Subject </strong>→ You need to manually broadcast events to multiple listeners, and history doesn’t matter. <em>Example: an app-wide event bus (“notification fired”, “modal closed”).</em></li><li><strong>BehaviorSubject </strong>→ You’re modeling shared state that components need <em>right now</em>, not just future changes. <em>Examples: current logged-in user, theme, cart contents.</em> This pattern is the backbone of many lightweight state-management setups.</li></ul><h3>Wrapping Up</h3><p>Next time you’re confused, just step back into the pizza shop: a Promise is one pizza order, an Observable is a meal-plan subscription, a Subject is the loudspeaker, and a BehaviorSubject is the menu board at the door.<br>Use Promises for genuine one-shot tasks, Observables for data that flows, Subjects to broadcast events, and BehaviorSubjects for shared state. Master these four, and most reactive frontend code (whatever the framework) will suddenly read like plain English.</p><p>Happy streaming! 🚀</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fc89a8c2a40e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Angular’s Journey From Chaos to Precision]]></title>
            <link>https://medium.com/@ayham.alattar/angulars-journey-from-chaos-to-precision-6370cc5dd719?source=rss-77d0ca348158------2</link>
            <guid isPermaLink="false">https://medium.com/p/6370cc5dd719</guid>
            <category><![CDATA[change-detection]]></category>
            <category><![CDATA[zoneless]]></category>
            <category><![CDATA[angular-signals]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[signal]]></category>
            <dc:creator><![CDATA[Ayham Al Attar]]></dc:creator>
            <pubDate>Sun, 10 May 2026 15:32:26 GMT</pubDate>
            <atom:updated>2026-05-10T15:32:26.848Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kE9Adj-Ww3W-T9VKlaYwfA.png" /></figure><p>Your Angular app feels fast, until one day it doesn’t, pages become heavier, interactions start lagging, the dashboard that once felt instant suddenly feels unpredictable, and most of the time, the issue isn’t your business logic, it’s change detection strategy.</p><h3>Core Concept</h3><p>What Is Change Detection? Angular needs to answer one question constantly: <em>“Did anything change? Should I update the UI?”</em>.<br>How it answers that question, and how much work it does to find out is what separates the three strategies we will cover.</p><h4>⚙️ Strategy 1: Zone.js — The Classic Approach:</h4><p>For most of Angular’s lifetime, change detection revolved around Zone.js, it quietly wraps all browser async operations (clicks, HTTP calls, timers) and taps Angular on the shoulder every time one completes. Angular’s response: <em>Check every component. All of them. Every time</em>.</p><p>User clicks a button<br>↓<br>Zone.js intercepts it<br>↓<br>Angular walks the entire component tree<br>↓<br>UI updates</p><p>Why it’s good: Zero configuration. You write your logic, Angular handles the rest.<br>Why it’s a problem at scale: Even if only one component changed, Angular still inspects all of them. In large apps, this becomes a real performance cost.</p><p><strong>The system is reactive, but not yet precise.</strong></p><h4>⚡ Strategy 2: Signals + Default Strategy — A Step Forward</h4><p>Angular Signals (stable since v17) introduced a smarter primitive: a value that knows when it changes and tells its consumers (the signal tracks who reads its value and notifies consumers when that value changes).</p><pre>count = signal(0);<br><br>increment() {<br>  this.count.update(v =&gt; v + 1);<br>}</pre><p>Instead of relying purely on intercepted async operations, Angular now understands: <em>what state changed ? who depends on it ? and where updates are needed</em>.<br>Here’s the important nuance: with the default strategy, Zone.js is still active underneath, so async events can still trigger a global change detection cycle, but signals make that cycle smarter, Angular now knows exactly which components read a changed signal, so it can focus its work rather than treating every component equally.</p><p>What the signals improve:</p><ul><li>Mark only the affected components as dirty</li><li>Skip components that don’t depend on the changed signal</li><li>Re-render only what’s needed</li></ul><p><strong>Signals already improve developer ergonomics and reactive clarity, even before full optimization arrives</strong>.</p><h4>🚀 Strategy 3: Signals + OnPush — The Modern Standard</h4><p>This is where performance and reactivity come together.<br>With ChangeDetectionStrategy.OnPush, Angular skips a component entirely, unless something relevant actually changed, paired with signals, &quot;something relevant&quot; means exactly one thing: <em>a signal this component reads has a new value.</em></p><pre>@Component({<br>  changeDetection: ChangeDetectionStrategy.OnPush,<br>  template: `&lt;p&gt;{{ count() }}&lt;/p&gt;`<br>})<br>export class CounterComponent {<br>  count = signal(0);<br>}</pre><p>Angular no longer needs to guess, it checks only the components that need updating.</p><p>Signal value changes<br>↓<br>Only affected components re-check<br>↓<br>UI updates</p><p>This also unlocks zoneless mode, where you remove Zone.js entirely. No monkey-patching, no global interception, just signals driving your UI directly. Angular 20 made this stable.</p><h3>📊 Quick Comparison</h3><p><strong>Zone.js<br></strong>- Detects changes through async interception<br>- Checks the full component tree<br>- Traverses all components and evaluates all bindings<br>- Fine for small applications<br>- Being phased out gradually<br>- Not zoneless-ready</p><p><strong>Signals + Default<br></strong>- Uses signals with Zone.js underneath<br>- Still traverses the full component tree<br>- Visits all components but focuses work on dirty views<br>- Provides marginal performance improvements<br>- Good transitional architecture<br>- Not zoneless-ready</p><p><strong>Signals + OnPush<br></strong>- Uses explicit signal-driven updates<br>- Re-checks only affected component paths<br>- Skips clean subtrees and unnecessary work<br>- Significantly better scalability and performance<br>- Best aligned with Angular’s future direction<br>- Fully compatible with zoneless Angular</p><h3>Wrap Up</h3><p>Angular’s change detection has come a long way, from checking everything, to checking smarter, to only checking what truly matters.<br>Zone.js served the ecosystem well, but Signals + OnPush is the Angular team’s bet on the future, and with zoneless mode now stable, that future is already here.</p><p>If you’re not using signals yet, now is the right time to start, your app’s performance will feel the difference.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6370cc5dd719" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Divide and Conquer with Git Bisect]]></title>
            <link>https://medium.com/@ayham.alattar/divide-and-conquer-with-git-bisect-4a035524cd8c?source=rss-77d0ca348158------2</link>
            <guid isPermaLink="false">https://medium.com/p/4a035524cd8c</guid>
            <category><![CDATA[bugs]]></category>
            <category><![CDATA[debugging]]></category>
            <category><![CDATA[git-bisect]]></category>
            <category><![CDATA[git]]></category>
            <dc:creator><![CDATA[Ayham Al Attar]]></dc:creator>
            <pubDate>Tue, 02 Dec 2025 20:41:23 GMT</pubDate>
            <atom:updated>2025-12-02T20:49:27.045Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9JIt9Rcoo7O8N79giOjnEg.jpeg" /></figure><p>Bugs rarely announce their arrival, they slip quietly into our codebase, hidden somewhere between yesterday’s commit and today’s, Git Bisect lets us rewind time, walk through every change, and discover the exact moment the code began to break.</p><h3>Definition</h3><p>Git bisect is a Git command used to identify the exact commit where a bug was introduced (or where an issue was fixed). By performing a binary search across our commit history.<br>Instead of manually checking dozens or hundreds of commits, Git bisect reduces the process to a handful of tests.</p><h3>Usage</h3><p>We use `git bisect` when:</p><ul><li>A bug appears, but we don’t know which commit caused it.</li><li>We want to locate a commit that introduced, removed, or changed specific behavior.</li><li>We need to pinpoint performance regressions or functional issues.</li></ul><p>Git bisect requires two pieces of information:</p><ol><li>A <strong>good </strong>commit: where the code behaved correctly.</li><li>A <strong>bad </strong>commit: where the problem is observable.</li></ol><p>Once started, Git automatically checks out the <strong>middle commit</strong> between these two commits, then we test that commit manually (or automatically) and mark it as <strong>good </strong>or <strong>bad</strong>, Git continues halving the commit range until it finds the <strong>first bad commit</strong> (the commit where the bug started). This binary search approach drastically reduces debugging time.</p><h3>Implementation</h3><p>Let’s consider the following timeline of commits:<br>A → B → C → D → E → F → G → H → I</p><ul><li>At commit <strong>C</strong>, the code works (good).</li><li>At commit <strong>I</strong>, the code is broken (bad).</li><li>We want to find the <strong>exact commit</strong> where the bug was introduced.</li></ul><ol><li>Start bisect</li></ol><pre>git bisect start<br><br># This command begins a bisect session and switches Git into bisect mode.</pre><p>2. Mark the known bad commit</p><pre>git bisect bad<br><br># Typically, the current commit is where the bug is visible.</pre><p>3. Mark the last known good commit</p><pre># Since commit C is known to be working:<br><br>git bisect good C<br><br># Git now automatically checks out the middle commit between C (good) and I (bad). <br># In this example, the midpoint is F.</pre><p>4. Test the checked-out commit</p><pre># At this point, we verify the behavior of commit F.<br># If the commit functions correctly:<br><br>git bisect good<br><br># If the bug is present:<br><br>git bisect bad<br><br># Git will then narrow the search interval and move to the next midpoint.</pre><p>5. Repeat until Git identifies the bad commit</p><pre># After a few iterations, Git will output something like:<br><br>abcd123 is the first bad commit<br><br># This commit is the exact point where the behavior changed.</pre><p>6. Finalize</p><pre># Once finished, we exit bisect mode and return to our original branch:<br><br>git bisect reset</pre><h3>Final takeaway</h3><p>The ideal situation to use `git bisect` is when a bug appears somewhere in the codebase that used to work fine before, or when some logic or function changed at some point. `git bisect` won’t help with newly built components and functions because its main mission is to compare with existing previous situation.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4a035524cd8c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Hidden Battle Between `sticky position` and `z-index`]]></title>
            <link>https://medium.com/@ayham.alattar/the-hidden-battle-between-sticky-position-and-z-index-78097175c3b2?source=rss-77d0ca348158------2</link>
            <guid isPermaLink="false">https://medium.com/p/78097175c3b2</guid>
            <category><![CDATA[stacking-context]]></category>
            <category><![CDATA[css]]></category>
            <category><![CDATA[z-index]]></category>
            <category><![CDATA[layout]]></category>
            <category><![CDATA[frontend]]></category>
            <dc:creator><![CDATA[Ayham Al Attar]]></dc:creator>
            <pubDate>Mon, 10 Nov 2025 21:02:25 GMT</pubDate>
            <atom:updated>2025-12-02T20:04:10.624Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pzOGGzMvJnSsX8Y0l-_TkQ.jpeg" /></figure><p>Many developers face a confusing situation where elements inside a sticky container appear under overlays, even when a very high `z-index` is set. <br>This happens not because of the number itself, but because of <strong>CSS stacking contexts</strong>.</p><h3>Problem</h3><p>A sticky element with `z-index` creates its own stacking context. Any child inside that context cannot visually rise above elements in a _parent or sibling_ stacking context, no matter how large its `z-index` is. <br>This means that dropdowns, menus, or popups inside a sticky header might be trapped beneath overlays or modals elsewhere on the page.</p><p><em>example</em>:</p><pre>&lt;!-- Overlay that appears on the page --&gt;<br>&lt;div class=&quot;overlay&quot;&gt;Overlay (z-index: 200)&lt;/div&gt;<br><br>&lt;!-- Sticky header with dropdown --&gt;<br>&lt;header class=&quot;sticky-header&quot;&gt;<br>  &lt;nav&gt;<br>    &lt;div class=&quot;dropdown-container&quot;&gt;<br>      &lt;button class=&quot;dropdown-btn&quot;&gt;Menu ▼&lt;/button&gt;<br>      &lt;div class=&quot;dropdown&quot;&gt;<br>        &lt;a&gt;I&#39;m a Dropdown!&lt;/a&gt;<br>      &lt;/div&gt;<br>    &lt;/div&gt;<br>  &lt;/nav&gt;<br>&lt;/header&gt;<br><br>&lt;main&gt;<br>  &lt;h1&gt;Content&lt;/h1&gt;<br>&lt;/main&gt;</pre><pre>/* Overlay that blocks the dropdown */<br>.overlay {<br>  position: fixed;<br>  top: 12vh;<br>  left: 0;<br>  width: 100%;<br>  height: 80vh;<br>  background: rgba(255, 0, 0, 0.5);<br>  z-index: 200;<br>  display: flex;<br>  align-items: center;<br>  justify-content: center;<br>  color: white;<br>}<br><br>/* Sticky header - Creates a stacking context */<br>.sticky-header {<br>  position: sticky;<br>  top: 0;<br>  background: #2c3e50;<br>  z-index: 100; /* Lower than overlay */<br>  padding: 20px;<br>}<br><br>.sticky-header nav {<br>  display: flex;<br>  justify-content: center;<br>  align-items: center;<br>}<br><br>/* Dropdown container */<br>.dropdown-container {<br>  position: relative;<br>}<br><br>.dropdown-btn {<br>  background: #3498db;<br>  color: white;<br>  border: none;<br>  padding: 10px 20px;<br>  font-size: 16px;<br>  cursor: pointer;<br>}<br><br><br>/* The dropdown menu - TRAPPED in stacking context */<br>.dropdown {<br>  position: absolute;<br>  top: 100%;<br>  right: 0;<br>  background: white;<br>  min-width: 200px;<br>  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);<br>  border-radius: 4px;<br>  margin-top: 8px;<br>  opacity: 0;<br>  visibility: hidden;<br>  transform: translateY(-10px);<br>  transition: all 0.3s ease;<br>  z-index: 9999; /* Doesn&#39;t help! Still under overlay! */<br>}<br><br>/* Show dropdown on hover */<br>.dropdown-container:hover .dropdown {<br>  opacity: 1;<br>  visibility: visible;<br>  transform: translateY(0);<br>}<br><br>.dropdown a {<br>  display: block;<br>  padding: 12px 20px;<br>  color: #2c3e50;<br>  text-decoration: none;<br>}<br><br>.dropdown a:hover {<br>  background: #ecf0f1;<br>}</pre><h3>Context</h3><p>Each stacking context acts like a sealed layer in an image editor. <br>You can reorder elements <strong>within </strong>that layer, but you cannot place part of it <strong>between </strong>other layers. <br>If a child element needs to appear above something outside its parent’s stacking context, it must exist in a context above that external element.</p><h4>Stacking Context Hierarchy:</h4><p>┌─────────────────────────────────┐<br>│ Document (root context) │<br>│ ┌──────────────────────────┐ │<br>│ │ Overlay (z-index: 200) │ │<br>│ └──────────────────────────┘ │<br>│ ┌──────────────────────────┐ │<br>│ │ Sticky Header Context │ │<br>│ │ (z-index: 100) │ │<br>│ │ ┌──────────────────┐ │ │<br>│ │ │ Dropdown │ │ │<br>│ │ │ (z-index: 9999) │◄───┼───┼─ Trapped!<br>│ │ │ Can’t escape! │ │ │<br>│ │ └──────────────────┘ │ │<br>│ └──────────────────────────┘ │<br>└─────────────────────────────────┘</p><h3>Solution</h3><p>The simplest approach is to increase the z-index of the sticky parent so the entire element, along with its children, appears above other content.</p><pre>.sticky-header {<br>  position: sticky;<br>  top: 0;<br>  background: #2c3e50;<br>  z-index: 300; /* Higher than overlay (200) */<br>  padding: 20px;<br>}</pre><p>This works best when the full header should remain visible above overlays.<br>However, this solution may not fit when only one part of the sticky element (like a dropdown) should overlap other layers.</p><p>If only part of the sticky element should appear above overlays, divide the UI into two sibling sections with different stacking behaviors. <br>The first part stays under the overlay, while the second part (like buttons or filters) can be given a higher z-index, this solution requires minor restructuring of the layout but gives precise control.</p><pre>&lt;!-- Overlay that appears on the page --&gt;<br>&lt;div class=&quot;overlay&quot;&gt;Overlay (z-index: 200)&lt;/div&gt;<br><br>&lt;!-- Sticky header WITHOUT creating stacking context for dropdown --&gt;<br>&lt;header class=&quot;sticky-header&quot;&gt;<br>  &lt;nav&gt;<br>    &lt;button class=&quot;dropdown-btn&quot;&gt;Menu ▼&lt;/button&gt;<br>  &lt;/nav&gt;<br>&lt;/header&gt;<br><br>&lt;div class=&quot;dropdown-layer&quot;&gt;<br>  &lt;div class=&quot;dropdown-container&quot;&gt;<br>    &lt;div class=&quot;dropdown&quot;&gt;<br>      &lt;a&gt;I&#39;m a Dropdown!&lt;/a&gt;<br>    &lt;/div&gt;<br>  &lt;/div&gt;<br>&lt;/div&gt;<br><br>&lt;main&gt;<br>  &lt;h1&gt;Content&lt;/h1&gt;<br>&lt;/main&gt;</pre><pre>/* Overlay that blocks the dropdown */<br>.overlay {<br>  position: fixed;<br>  top: 12vh;<br>  left: 0;<br>  width: 100%;<br>  height: 80vh;<br>  background: rgba(255, 0, 0, 0.5);<br>  z-index: 200;<br>  display: flex;<br>  align-items: center;<br>  justify-content: center;<br>  color: white;<br>}<br><br>/* Sticky header - Creates a stacking context */<br>.sticky-header {<br>  position: sticky;<br>  top: 0;<br>  background: #2c3e50;<br>  z-index: 100; /* Lower than overlay */<br>  padding: 20px;<br>}<br><br>.sticky-header nav {<br>  display: flex;<br>  justify-content: center;<br>  align-items: center;<br>}<br><br>/* Dropdown container */<br>.dropdown-container {<br>  position: relative;<br>}<br><br>.dropdown-btn {<br>  background: #3498db;<br>  color: white;<br>  border: none;<br>  padding: 10px 20px;<br>  font-size: 16px;<br>  cursor: pointer;<br>}<br><br>.dropdown {<br>  background: white;<br>  min-width: 200px;<br>  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);<br>  border-radius: 4px;<br>  opacity: 0;<br>  visibility: hidden;<br>}<br><br>/* Show dropdown on hover */<br>.dropdown-container:hover .dropdown {<br>  opacity: 1;<br>  visibility: visible;<br>  transform: translateY(0);<br>}<br><br>.dropdown a {<br>  display: block;<br>  padding: 12px 20px;<br>  color: #2c3e50;<br>  text-decoration: none;<br>}<br><br>/* Separate dropdown layer with higher z-index */<br>.dropdown-layer {<br>  position: fixed;<br>  top: 0;<br>  left: 0;<br>  width: 100%;<br>  height: 100%;<br>  pointer-events: none; /* Don&#39;t block clicks */<br>  z-index: 300; /* Above overlay */<br>}<br><br>.dropdown-container {<br>  position: absolute;<br>  top: 60px; /* Align with header height */<br>  left: 50%;<br>  transform: translateX(-50%);<br>  pointer-events: auto; /* Re-enable clicks for dropdown */<br>}<br><br>/* Show dropdown ONLY on button hover */<br>.sticky-header:has(.dropdown-btn:hover) ~ .dropdown-layer .dropdown {<br>  opacity: 1;<br>  visibility: visible;<br>  transform: translateY(0);<br>}</pre><h3>Debugging tips</h3><ul><li>Use browser developer tools to inspect the <strong>stacking context</strong> of each element.</li><li>Check for transforms, opacity, or z-index on ancestor elements.</li><li>Use colored outlines or backgrounds temporarily to visualize stacking layers.</li></ul><h3>Final takeaway</h3><p>`z-index` alone doesn’t guarantee layering control — context does. <br>If increasing the z-index doesn’t solve your problem, the element is likely stuck inside the wrong stacking context.</p><p>Move floating elements to a higher context, or restructure your layout to control which parts should appear above others. <br>Once you understand stacking contexts, `z-index` issues become predictable and easy to fix.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=78097175c3b2" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>