Thuta Learning
CSS
IntermediateWeb Developmentအခြေခံ

Z-Index

CSSသင်ခန်းစာ 1630 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • z-index အလုပ်လုပ်ဖို့ element က positioned ဒါမှမဟုတ် flex/grid item ဖြစ်ရမယ်ဆိုတာ စစ်နိုင်ဖို့
  • Stacking context ကို ဖန်တီးတဲ့ property တွေ (z-index ပါတဲ့ position, opacity, transform, isolation...) ကို မှတ်မိဖို့
  • Child ရဲ့ z-index က parent context ရဲ့ အပြင်ကို ဘာကြောင့် မကျော်နိုင်လဲ ရှင်းပြနိုင်ဖို့
  • CSS custom property နဲ့ layer token scale တစ်ခု ဆောက်ပြီး isolation: isolate ကို သုံးနိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

ရိုးရိုးလေး ရှင်းရရင်

Element တွေ ထပ်နေတဲ့အခါ browser က စားပွဲပေါ်မှာ စက္ကူတွေ ထပ်ထားသလို အောက်ကနေ အပေါ်ကို paint လုပ်ပါတယ်။ z-index က စက္ကူတစ်ရွက်ကို "ငါ့ကို ပိုအပေါ်မှာ ထား" လို့ နံပါတ်ပေးတာပါ — နံပါတ် ကြီးလေ အပေါ်ရောက်လေပါ။ Modal၊ dropdown၊ tooltip နဲ့ sticky header တွေမှာ ဒီ property ကို အမြဲ လိုအပ်ပါတယ်။

ဒါပေမယ့် စက္ကူတချို့က folder ထဲမှာ ရှိနေပါတယ်။ Folder ထဲက စက္ကူကို 9999 ပေးလည်း folder ကိုယ်တိုင်က တခြား folder ရဲ့ အောက်မှာ ရှိနေရင် folder နဲ့အတူ အောက်မှာပဲ ကျန်နေပါတယ်။ အဲဒီ folder ကို stacking context လို့ ခေါ်ပါတယ်။

Problem → Why → Solution

ပြဿနာ: dropdown က content အောက်ကို ဝင်သွားတယ်

Header ထဲက dropdown menu ကို ဖွင့်လိုက်တော့ အောက်က hero section ရဲ့ အောက်ကို ဝင်သွားပါတယ်။ Dropdown ကို z-index: 9999 ပေးကြည့်ပါတယ်၊ ပြီးတော့ 999999 ပေးကြည့်ပါတယ်၊ ဘာမှ မပြောင်းပါဘူး။ Hero မှာ z-index: 2 ပဲ ရှိတာကို တွေ့ရပေမယ့် 9999 က 2 ထက် ကြီးတာ သေချာနေပါတယ်။

layers.css
.header   { position: relative; z-index: 1; }
.dropdown { position: absolute; top: 100%; z-index: 9999; }
.hero     { position: relative; z-index: 2; }

/* <header class="header"><nav class="dropdown">...</nav></header>
   <section class="hero">...</section> */

ဘာကြောင့် 9999 က ရှုံးတာလဲ

.header က position: relative နဲ့ z-index: 1 ရှိလို့ stacking context တစ်ခု ဖြစ်နေပါတယ်။ .dropdown ရဲ့ 9999 က header ရဲ့ အထဲမှာပဲ နှိုင်းယှဉ်ခံရပြီး root အဆင့်မှာတော့ header တစ်ခုလုံးကို 1 အဖြစ်ပဲ မြင်ပါတယ်။ Root မှာ 1 နဲ့ 2 ကို နှိုင်းယှဉ်တော့ hero (2) က အနိုင်ရပါတယ်။ ဖြေရှင်းချက်က dropdown ကို ပိုကြီးတဲ့ နံပါတ် ပေးတာ မဟုတ်ဘဲ header ရဲ့ z-index ကို hero ထက် မြင့်အောင် လုပ်တာ (ဒါမှမဟုတ် hero ရဲ့ z-index ကို ဖယ်တာ) ပါ။

Core concept: stacking context ဆိုတဲ့ folder

z-index မပေးထားရင် browser က အစီအစဉ်တစ်ခုအတိုင်း paint လုပ်ပါတယ်။ အောက်ဆုံးမှာ context ရဲ့ background၊ ပြီးရင် ပုံမှန် flow ထဲက block တွေ၊ float တွေ၊ inline content၊ အပေါ်ဆုံးမှာ positioned element တွေကို HTML အစဉ်အတိုင်း paint ပါတယ်။ ဒါကြောင့် ထပ်နေတဲ့ positioned sibling နှစ်ခုမှာ HTML ထဲ နောက်မှ ရေးထားတဲ့ ဟာက အပေါ်မှာ ပေါ်ပါတယ်။ ဒါကြောင့် HTML အစဉ်ကို ပြောင်းတာကိုယ်တိုင်က မှန်ကန်တဲ့ ဖြေရှင်းနည်း တစ်ခု ဖြစ်ပြီး z-index လုံးဝ မလိုတော့အောင် လုပ်ပေးတတ်ပါတယ်။

z-index က ဒီ အစဉ်ကို ပြောင်းပေးပေမယ့် position: static မဟုတ်တဲ့ element (relative, absolute, fixed, sticky) နဲ့ flex/grid item တွေမှာပဲ အလုပ်လုပ်ပါတယ်။ Static block ကို z-index: 5 ပေးရင် ဘာမှ မဖြစ်ပါဘူး။ Negative z-index ကလည်း ရပါတယ် — element ကို သူ့ context ရဲ့ ပုံမှန် content နောက်ကို ပို့ပေမယ့် context ရဲ့ background အောက်ကိုတော့ မရောက်ပါဘူး။

Stacking context ဆိုတာ ကိုယ်ပိုင် z-index နံပါတ်စဉ် ရှိတဲ့ folder တစ်ခုပါ။ Folder အထဲက child တွေကို သူတို့ချင်း နှိုင်းယှဉ်ပြီးတဲ့နောက် folder တစ်ခုလုံးကို parent context ထဲမှာ layer တစ်ခုတည်းအဖြစ် ထည့်ပါတယ်။ Child က folder အပြင်ဘက်က element တွေနဲ့ ဘယ်တော့မှ တိုက်ရိုက် မနှိုင်းယှဉ်ရပါဘူး။

Context အသစ် ဖန်တီးတဲ့ အဖြစ်များဆုံး property တွေကတော့ အောက်ပါအတိုင်းပါ။ ဒီထဲက opacity နဲ့ transform က animation တွေထဲမှာ မသိလိုက်ဘဲ ပါလာတတ်လို့ အန္တရာယ် အများဆုံးပါ။

PropertyContext ဖန်တီးတဲ့ အခြေအနေ
position: relative / absolutez-index က auto မဟုတ်ရင်
position: fixed / stickyအမြဲ
opacity1 ထက် ငယ်ရင် (0.99 ပါ)
transform / filternone မဟုတ်ရင်
isolationisolate ဖြစ်ရင်

Stacking context က folder တွေ အထပ်ထပ် ဖြစ်နိုင်ပါတယ်၊ folder ထဲမှာ folder ထပ်ရှိနိုင်ပါတယ်။ ဒါကြောင့် element နှစ်ခုကို နှိုင်းယှဉ်ချင်ရင် နှစ်ခုလုံးရဲ့ ancestor တွေကို အပေါ်ဘက် လိုက်ကြည့်ပြီး သူတို့ ပထမဆုံး ဆုံတဲ့ context ကို ရှာရပါတယ်။ အဲဒီ context ထဲမှာ ယှဉ်ပြိုင်နေတာက element နှစ်ခု ကိုယ်တိုင် မဟုတ်ဘဲ သူတို့ကို ကိုယ်စားပြုတဲ့ ancestor နှစ်ခု ဖြစ်တတ်ပါတယ်။

ဘယ်သူ အပေါ်ရောက်လဲ စစ်နည်း
  1. နှစ်ခုလုံး positioned လား စစ်
  2. Context တစ်ခုစီကို အပေါ်ဘက် လိုက်ရှာ
  3. တူညီတဲ့ parent context ကို ရှာ
  4. အဲဒီအဆင့်က z-index ကိုပဲ နှိုင်းယှဉ်

z-index နံပါတ်ကို မတိုးခင် DevTools မှာ element နှစ်ခုရဲ့ ancestor တွေကို စစ်ပြီး ဘယ် folder တွေထဲ ရှိနေလဲ ကြည့်ပါ။

အထွေထွေ ပုံစံ

css
:root {
  --z-layer-name: number;  /* one token per layer */
}

element {
  position: relative | absolute | fixed | sticky;
  z-index: var(--z-layer-name);
}

component {
  isolation: isolate;  /* new context, no z-index needed */
}

Layer တစ်ခုစီကို custom property token နဲ့ နာမည်ပေးပြီး z-index မှာ var() နဲ့ သုံးပါ။ Component တစ်ခုရဲ့ အတွင်းပိုင်း z-index တွေကို အပြင်ကို မယိုစိမ့်စေချင်ရင် isolation: isolate ပေးပါ။

Example: modal နဲ့ backdrop

Modal ဖွင့်တဲ့အခါ page ကို မှိန်စေတဲ့ backdrop နဲ့ modal box ကို layer နှစ်ခုအဖြစ် ထားပါတယ်။ နှစ်ခုလုံး fixed ဖြစ်ပြီး body ရဲ့ တိုက်ရိုက် child တွေဖြစ်လို့ root context ထဲမှာ အတူတူ နှိုင်းယှဉ်ခံရပါတယ်။

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 100;
}

.modal {
  position: fixed;
  z-index: 101;
}

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. Line 2position: fixed;

    Backdrop ကို viewport မှာ ကပ်ပြီး fixed ဖြစ်တာနဲ့ stacking context တစ်ခုလည်း ဖြစ်သွားပါတယ်။

  2. Line 3inset: 0;

    top, right, bottom, left လေးခုလုံးကို 0 ပေးလို့ backdrop က viewport တစ်ခုလုံးကို ဖုံးပြီး အောက်က page ကို click နှိပ်လို့ မရတော့ပါဘူး။

  3. Line 4z-index: 100;

    Page content တွေရဲ့ အပေါ်မှာ ရှိဖို့ ကြီးတဲ့ layer တန်ဖိုး ပေးထားပါတယ်။ Page ထဲက တခြား positioned element တွေက 100 ထက် ငယ်ရပါမယ်။

  4. Line 9z-index: 101;

    Modal နဲ့ backdrop က တူညီတဲ့ root context ထဲမှာ ရှိလို့ 101 > 100 ဆိုတဲ့ ရိုးရှင်းတဲ့ နှိုင်းယှဉ်မှုက တကယ် အလုပ်လုပ်ပါတယ်။ Real modal မှာ top/left (ဒါမှမဟုတ် inset) နဲ့ နေရာချပေးဖို့ လိုသေးပါတယ်၊ ဒီမှာ layer ကိုပဲ ပြထားတာပါ။

You should see
Backdrop က page တစ်ခုလုံးကိုဖုံးပြီး modal box က backdrop အပေါ်မှာပေါ်ပါမယ်။

DevTools နဲ့ stacking ပြဿနာ ရှာနည်း

1. ဖုံးခံရတဲ့ element ကို ရွေးပါ

Elements panel မှာ dropdown ကို ရွေးပြီး Computed tab မှာ position နဲ့ z-index ကို စစ်ပါ။ position က static ဖြစ်နေရင် ပြဿနာ အဲဒီမှာပဲ ပြီးပါပြီ။

2. Ancestor တွေကို အပေါ်ကို လိုက်စစ်ပါ

Parent တစ်ခုချင်းစီကို ရွေးပြီး z-index ပါတဲ့ position၊ opacity, transform, filter, isolation ရှိမရှိ ကြည့်ပါ။ ပထမဆုံး တွေ့တဲ့ ancestor က dropdown ရဲ့ folder ပါ။

3. Folder အဆင့်မှာ ပြင်ပါ

အဲဒီ folder ရဲ့ z-index ကို ဖုံးနေတဲ့ element ရဲ့ folder ထက် မြင့်အောင် ပြင်ပါ၊ ဒါမှမဟုတ် မလိုအပ်တဲ့ context ကို ဖယ်ပါ။ Chromium ရဲ့ Layers panel ကလည်း layer တွေကို 3D နဲ့ ပြပေးနိုင်ပါတယ်။

လက်တွေ့ project တွေမှာ

Real project တွေမှာ ဘယ်လို သုံးလဲ

Project ကြီးတွေမှာ developer တစ်ယောက်ချင်းစီက z-index: 999၊ 9999၊ 99999 လို နံပါတ်တွေ ထပ်တိုးသွားတာကို "z-index arms race" လို့ ခေါ်ကြပါတယ်။ ဒါကို ကာကွယ်ဖို့ team တွေက --z-dropdown: 100; --z-sticky: 200; --z-modal: 300; --z-toast: 400; လို token scale တစ်ခုကို တစ်နေရာတည်းမှာ သတ်မှတ်ပြီး ကျန်တဲ့ code တွေက token ကိုပဲ သုံးကြပါတယ်။ Layer အသစ် လိုရင် scale ထဲ ထည့်ပြီး review လုပ်ရလွယ်ပါတယ်။ .menu { z-index: var(--z-dropdown); } ကို ဖတ်တဲ့ ဘယ်သူမဆို ဒီ element က layer အစီအစဉ်ထဲ ဘယ်နေရာမှာ ရှိရမလဲ ချက်ချင်း သိပါတယ်။

React, Vue လို framework တွေရဲ့ modal နဲ့ tooltip library တွေက content ကို portal နဲ့ body ရဲ့ အဆုံးကို ရွှေ့ပြီး render လုပ်လေ့ ရှိပါတယ်။ ဒါက transform ဒါမှမဟုတ် z-index ပါတဲ့ parent context ထဲမှာ ပိတ်မိမနေအောင် လုပ်တာပါ။ Card ထဲမှာ decoration layer တွေ ပါတဲ့ component တွေမှာတော့ root element ကို isolation: isolate ပေးပြီး အတွင်းက negative z-index တွေကို component အတွင်းမှာပဲ ထိန်းထားပါတယ်။

အမှားများ

အဖြစ်များတဲ့ အမှားတွေ

1. Static element ကို z-index ပေးတာ

.tooltip { z-index: 10; } လို့ ရေးပြီး position မပေးထားရင် (flex/grid item လည်း မဟုတ်ရင်) z-index ကို လုံးဝ လျစ်လျူရှုပါတယ်။ DevTools က ဒီ declaration ကို မှိန်ပြပြီး သတိပေးတတ်ပါတယ်၊ position: relative ထည့်ပါ။

2. Context ထဲက child ကို နံပါတ် တိုးပေးနေတာ

Dropdown 9999 က ရှုံးနေရင် နံပါတ် ထပ်တိုးတာ အကျိုးမရှိပါဘူး။ Dropdown ရဲ့ ancestor တွေထဲက context ဖန်တီးထားတဲ့ element ကို ရှာပြီး အဲဒီ element ရဲ့ z-index ကို ပြင်ပါ ဒါမှမဟုတ် မလိုအပ်တဲ့ context ကို ဖယ်ပါ။

3. Animation ကနေ ဝင်လာတဲ့ context ကို မမြင်တာ

Fade-in animation ရဲ့ opacity: 0.99 ဒါမှမဟုတ် slide-in ရဲ့ transform က animation ပြီးတဲ့အထိ (တခါတလေ အမြဲ) context အသစ် ဖန်တီးထားပါတယ်။ Animation run နေချိန်မှာပဲ tooltip က ဖုံးခံရတယ်ဆိုရင် ဒါကို သံသယ ဝင်ပါ။

Best Practices

အကောင်းဆုံး အလေ့အကျင့်များ

  • Token scale ရဲ့ တန်ဖိုးတွေကို 100 စီ ခြားထားပါ၊ နောက်မှ layer အသစ် ကြားထဲ ထည့်ဖို့ နေရာ ကျန်ပါတယ်။
  • Component အတွင်းက z-index တွေကို 1, 2, 3 လို သေးသေးလေးပဲ သုံးပြီး component root ကို isolation: isolate ပေးပါ။
  • Modal, toast လို app-wide overlay တွေကို body ရဲ့ အဆုံးမှာ render လုပ်ပါ။

Mini challenge

Mini challenge: layer scale နဲ့ dropdown ကို ကယ်ပါ

Starter code မှာ dropdown က hero အောက် ဝင်နေပါတယ်။ Layer token scale တစ်ခု ဆောက်ပြီး ပြင်ပါ၊ hero ရဲ့ decoration ကိုလည်း ဆက် မြင်ရအောင် ထားပါ။

  • :root မှာ --z-header, --z-modal, --z-toast token သုံးခုကို အစဉ်လိုက် ကြီးလာအောင် သတ်မှတ်ရမယ်
  • Dropdown က hero ရဲ့ အပေါ်မှာ ပေါ်ရမယ်၊ dropdown ကို 9999 မပေးရ
  • Hero ရဲ့ ::before decoration (z-index: -1) က hero background ရဲ့ အပေါ်မှာ ဆက်မြင်ရရမယ်
  • Toast က modal ရဲ့ အပေါ်မှာ ရှိရမယ်
layers.html
<style>
  :root {
    /* TODO: --z-header, --z-modal, --z-toast */
  }
  .header { position: relative; z-index: 1; }       /* TODO: use a token */
  .dropdown { position: absolute; top: 100%; z-index: 9999; }
  .hero { position: relative; z-index: 2; }  /* TODO: no z-index, still a context */
  .hero::before { content: ""; position: absolute; z-index: -1; }
  .modal { position: fixed; }                       /* TODO: token */
  .toast { position: fixed; }                       /* TODO: token, above modal */
</style>

<header class="header">Shop <nav class="dropdown">Menu</nav></header>
<section class="hero">Big sale</section>
<div class="modal">Sign in</div>
<div class="toast">Saved!</div>

ရလဒ်: Header ရဲ့ dropdown (အဝါရောင်) က hero အပေါ်မှာ ပေါ်ပါတယ်။ Hero ထဲမှာ အပြာရင့် decoration box က hero background အပေါ်၊ "Big sale" စာသားရဲ့ နောက်မှာ မြင်ရပါတယ်။ Toast က modal နဲ့ ထပ်နေတဲ့ နေရာမှာ modal ရဲ့ အပေါ်က ပေါ်ပါတယ်။

အရိပ်အမြွက် ကြည့်ရန်

Hero ရဲ့ z-index ကို ဖယ်လိုက်ရင် context ပျောက်သွားပြီး ::before က hero background နောက်ကို ကျသွားပါမယ်။ z-index မပါဘဲ context ဖန်တီးပေးတဲ့ property ကို သုံးပါ။

အဖြေ ကြည့်ရန်
layers.html
<style>
  :root {
    --z-header: 100;
    --z-modal: 300;
    --z-toast: 400;
  }
  body { margin: 0; }
  .header { position: relative; z-index: var(--z-header); height: 50px; }
  .dropdown {
    position: absolute;
    top: 100%;
    left: 0;
    width: 200px;
    height: 120px;
    background: #fde68a;
  }
  .hero { position: relative; isolation: isolate; height: 200px; background: #e0f2fe; }
  .hero::before {
    content: "";
    position: absolute;
    inset: 20px;
    z-index: -1;
    background: #1e3a8a;
  }
  .modal { position: fixed; inset: 30% 25%; z-index: var(--z-modal); background: white; }
  .toast {
    position: fixed;
    right: 24%;
    bottom: 28%;
    z-index: var(--z-toast);
    background: #16a34a;
  }
</style>

<header class="header">Shop <nav class="dropdown">Menu</nav></header>
<section class="hero">Big sale</section>
<div class="modal">Sign in</div>
<div class="toast">Saved!</div>

Quick Quiz 1 / 3

.card { opacity: 0.95; } ထဲမှာ .tip { position: absolute; z-index: 9999; } ရှိပြီး card ရဲ့ နောက်က sibling .banner { position: relative; z-index: 1; } က card နဲ့ ထပ်နေပါတယ်။ ထပ်နေတဲ့နေရာမှာ ဘာမြင်ရမလဲ။

Quick Quiz 2 / 3

ဘယ် element မှာ z-index: 5 က အကျိုးမသက်ရောက်ပါသလဲ။

Quick Quiz 3 / 3

Card ထဲက ::before decoration ကို z-index: -1 ပေးတော့ card background အောက်ကို ပျောက်သွားပါတယ်။ အကောင်းဆုံး ပြင်နည်းက ဘာလဲ။

မှတ်ထားရမယ့် အချက်များ

  • z-index က positioned element နဲ့ flex/grid item တွေမှာပဲ အလုပ်လုပ်ပါတယ်။
  • z-index က stacking context တစ်ခုအတွင်းမှာပဲ နှိုင်းယှဉ်ပြီး context တစ်ခုလုံးက parent ထဲမှာ layer တစ်ခုအဖြစ် ရွေ့ပါတယ်။
  • z-index ပါတဲ့ positioned element၊ fixed/sticky၊ opacity < 1၊ transform၊ filter၊ isolation: isolate တို့က context အသစ် ဖန်တီးပါတယ်။
  • နံပါတ်တွေ ပြိုင်တိုးမယ့်အစား custom property token scale တစ်ခုကို သုံးပါ။

CSS reference — MDN Web Docs

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Z-Index | Thuta Learning