Thuta Learning
CSS
IntermediateWeb Developmentအခြေခံ

Position Property

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

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

  • Position value တစ်ခုစီက normal flow ထဲမှာ နေရာ ချန်ထားလား မထားလား ခွဲပြောနိုင်ဖို့
  • Absolute element ရဲ့ containing block ကို ရှာပြီး badge ဘာကြောင့် page ထောင့်ကို ပျံသွားလဲ ရှင်းပြနိုင်ဖို့
  • transform ပါတဲ့ ancestor က fixed element ကို ဘယ်လို ဖမ်းထားတတ်လဲ သိဖို့
  • Sticky header မကပ်ရတဲ့ အကြောင်းရင်း သုံးခု (top မရှိ၊ overflow ancestor၊ parent အမြင့်) ကို debug လုပ်နိုင်ဖို့

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

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

Normal flow ကို ရုပ်ရှင်ရုံထဲက ထိုင်ခုံတန်းလို့ မြင်ကြည့်ပါ — လူတိုင်း ကိုယ့်ခုံမှာ အစဉ်လိုက် ထိုင်ပါတယ်။ relative က ကိုယ့်ခုံမှာ ထိုင်ရင်း ခန္ဓာကိုယ်ကို နည်းနည်း ဘေးစောင်းလိုက်တာပါ၊ ခုံကတော့ သူ့အတွက် ရှိနေဆဲပါ။ absolute က ခုံကနေ ထပြီး အခန်းထဲက နံရံတစ်ခုခုကို တိုင်းတာပြီး ရပ်နေတာပါ၊ သူ့ခုံကို တခြားလူတွေ ဝင်ယူသွားပါတယ်။

fixed က screen ပေါ်မှာ ကပ်ထားတဲ့ sticker လိုပါ — ရုပ်ရှင်ဘယ်လောက် ရွေ့ရွေ့ အဲဒီနေရာမှာပဲ ရှိနေပါတယ်။ sticky ကတော့ ပုံမှန် ထိုင်နေပြီး scroll လုပ်လို့ ထိပ်ကို ရောက်တဲ့အခါ အပေါ်မှာ ကပ်သွားတာပါ၊ ဒါပေမယ့် သူ့အုပ်စု (parent) ကုန်သွားရင် အတူတူ ထွက်သွားပါတယ်။

Problem → Why → Solution

ပြဿနာ: badge က page ထောင့်ကို ပျံသွားတယ်

Product card ရဲ့ ညာဘက်အပေါ်ထောင့်မှာ "-20%" badge ကပ်ချင်လို့ position: absolute; top: 8px; right: 8px; ပေးလိုက်ပါတယ်။ Badge က card ပေါ်မှာ မပေါ်ဘဲ page တစ်ခုလုံးရဲ့ ညာဘက်အပေါ်ထောင့်ကို ရောက်သွားပြီး card အားလုံးရဲ့ badge တွေက အဲဒီနေရာမှာ တစ်ခုပေါ်တစ်ခု ထပ်နေပါတယ်။ Scroll ဆင်းလိုက်တော့ card တွေနဲ့ အတူ မပါဘဲ page ထိပ်မှာပဲ ကျန်ခဲ့ပါတယ်။

.card {
  width: 300px;
  /* no position here */
}

.badge {
  position: absolute;
  top: 8px;
  right: 8px;
}

ဘာကြောင့် ဒီလို ဖြစ်တာလဲ

Absolute element က top/right ကို သူ့ရဲ့ containing block ကနေ တိုင်းပါတယ်၊ အဲဒါက position: static မဟုတ်တဲ့ အနီးဆုံး ancestor ဖြစ်ပါတယ်။ .card က static ဖြစ်နေတော့ browser က အပေါ်ကို ဆက်ရှာပြီး ဘယ်သူမှ မတွေ့လို့ initial containing block (page ရဲ့ ထိပ်ဆုံးက viewport အရွယ် ထောင့်မှန်စတုဂံ) ကို သုံးလိုက်ပါတယ်။ .card မှာ position: relative; တစ်ကြောင်း ထည့်လိုက်ရုံနဲ့ card က badge ရဲ့ containing block ဖြစ်လာပါတယ်။

Core concept: flow ထဲမှာ နေမလား နဲ့ ဘယ်ကနေ တိုင်းမလဲ

Position value တစ်ခုစီကို မေးခွန်း နှစ်ခုနဲ့ ဖတ်ရင် ရှင်းသွားပါတယ်။ ပထမ — element က normal flow ထဲမှာ သူ့နေရာကို ချန်ထားသေးလား။ ဒုတိယ — top, right, bottom, left (offset property တွေ) ကို ဘယ် box ကနေ တိုင်းလဲ။ static က default ပါ၊ flow ထဲမှာ ပုံမှန် နေပြီး offset property တွေနဲ့ z-index ကို လျစ်လျူရှုပါတယ်။

ValueFlow ထဲ နေရာOffset ကို တိုင်းတဲ့ box
relativeချန်ထားသူ့ မူလနေရာ
absoluteဖယ်ထုတ်Static မဟုတ်တဲ့ အနီးဆုံး ancestor
fixedဖယ်ထုတ်Viewport (ခြွင်းချက်ရှိ)
stickyချန်ထားအနီးဆုံး scroll container

relative element ကို top: 10px ပေးရင် မျက်စိနဲ့ မြင်ရတာ 10px ဆင်းသွားပေမယ့် အောက်က element တွေက မူလနေရာကို ကြည့်ပြီး စီနေဆဲမို့ ထပ်နေနိုင်ပါတယ်။ ဒါကြောင့် relative ကို ရွှေ့ဖို့ထက် absolute child တွေအတွက် containing block ဖြစ်ဖို့ ပိုသုံးကြပါတယ်။ Absolute element ကို left: 0; right: 0; နှစ်ခုလုံး ပေးပြီး width ကို auto ထားရင် containing block အပြည့် ဆန့်ထွက်ပါတယ်၊ inset: 0 က offset လေးခုလုံးကို တစ်ခါတည်း 0 ပေးတဲ့ shorthand ပါ။

fixed ရဲ့ ခြွင်းချက်က ancestor တစ်ခုခုမှာ transform, filter ဒါမှမဟုတ် perspective ရှိနေရင် အဲဒီ ancestor က fixed element ရဲ့ containing block ဖြစ်သွားပြီး element က viewport မှာ မကပ်တော့ဘဲ ancestor နဲ့အတူ scroll ဖြစ်သွားတာပါ။ sticky ကတော့ threshold (ဥပမာ top: 0) မရောက်ခင် relative လို နေပြီး ရောက်တာနဲ့ scroll container ရဲ့ အစွန်းမှာ ကပ်ပါတယ်၊ ဒါပေမယ့် parent box ရဲ့ အပြင်ကို ဘယ်တော့မှ မထွက်ပါဘူး။

Flow ကနေ ထွက်သွားတဲ့ element (absolute နဲ့ fixed) တွေမှာ နောက်ထပ် အကျိုးဆက် နှစ်ခု ရှိပါတယ်။ ပထမ — parent က သူတို့ကို အမြင့်တွက်တဲ့အခါ ထည့်မတွက်တော့လို့ child အားလုံး absolute ဖြစ်နေတဲ့ parent က အမြင့် 0 ဖြစ်သွားပါတယ်။ ဒုတိယ — width: auto ဖြစ်နေရင် block element လို line အပြည့် မဆန့်တော့ဘဲ content အရွယ်အထိ ကျုံ့သွားပါတယ်။

Position ကို ဖတ်တဲ့ အစီအစဉ်
  1. Flow ထဲမှာ နေရာ ချန်လား?
  2. Containing block ကို ရှာ
  3. Offset တွေကို အဲဒီ box ကနေ တိုင်း
  4. Scroll လုပ်ရင် ဘယ်သူနဲ့ ရွေ့လဲ စစ်

Element က မထင်မှတ်တဲ့နေရာ ရောက်နေရင် အမြဲတမ်း "containing block က ဘယ်သူလဲ" ကို အရင်မေးပါ။

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

css
parent {
  position: relative;  /* becomes the containing block */
}

child {
  position: absolute | fixed | sticky;
  top: length;     /* and/or right, bottom, left */
  inset: length;   /* shorthand for all four */
}

Parent ကို relative ပေးပြီး containing block လုပ်ပါ၊ ပြီးမှ child ကို position value တစ်ခုနဲ့ offset တွေ ပေးပါ။ Offset မပါတဲ့ absolute element က သူ မူလ ရှိရမယ့်နေရာ (static position) မှာပဲ ကျန်နေပါတယ်။

Example: chat button နဲ့ card badge

ဒီ CSS မှာ position အမျိုးအစား နှစ်ခု ပါပါတယ်။ Chat button က viewport ထောင့်မှာ အမြဲ ကပ်နေပြီး badge က card ထောင့်မှာ ကပ်ပါတယ်။ Badge အလုပ်လုပ်ဖို့ HTML မှာ badge က position: relative ရှိတဲ့ card တစ်ခုထဲမှာ ရှိရပါမယ်။

.chat-button {
  position: fixed;
  right: 20px;
  bottom: 20px;
  z-index: 50;
}

.badge {
  position: absolute;
  top: 8px;
  right: 8px;
}

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

  1. Line 2position: fixed;

    Button ကို flow ထဲကနေ ထုတ်ပြီး viewport ကို containing block အဖြစ် သုံးပါတယ်၊ ဒါကြောင့် page ကို scroll လုပ်လည်း မရွေ့ပါဘူး။

  2. Lines 3-4right: 20px;

    right နဲ့ bottom ကို viewport ရဲ့ ညာဘက်နဲ့ အောက်ဘက် အစွန်းကနေ တိုင်းလို့ button က ညာဘက်အောက်ထောင့်ကနေ 20px စီ ခွာနေပါတယ်။

  3. Line 5z-index: 50;

    Positioned element ဖြစ်လို့ z-index အလုပ်လုပ်ပြီး button ကို page content အပေါ်မှာ ထားပါတယ်။ အသေးစိတ်ကို နောက် lesson မှာ လေ့လာပါမယ်။

  4. Line 9position: absolute;

    Badge က flow ထဲကနေ ထွက်သွားလို့ card ထဲက စာသားတွေကို မတွန်းပါဘူး။ Containing block က static မဟုတ်တဲ့ အနီးဆုံး ancestor ဖြစ်တဲ့ card ပါ။

  5. Lines 10-11top: 8px;

    top နဲ့ right က card ရဲ့ padding box အစွန်းကနေ တိုင်းလို့ badge က card ရဲ့ ညာဘက်အပေါ်ထောင့်ကနေ 8px စီ အတွင်းဘက်မှာ ရှိပါတယ်။

You should see
Chat button က browser ညာဘက်အောက်မှာအမြဲပေါ်နေမယ်။ Badge က parent card ရဲ့ ညာဘက်အပေါ်မှာကပ်နေပါမယ်။

Sticky header မကပ်ရတဲ့ အကြောင်းရင်း သုံးခု

Threshold မပေးထား

position: sticky တစ်ခုတည်းနဲ့ ဘာမှ မဖြစ်ပါဘူး၊ top: 0 လို ဘယ်နေရာမှာ ကပ်ရမလဲ ပြောပေးရပါတယ်။

Overflow ပါတဲ့ ancestor

ကြားထဲက ancestor တစ်ခုမှာ overflow: hidden ဒါမှမဟုတ် auto ရှိရင် အဲဒါက scroll container အသစ် ဖြစ်သွားပြီး sticky က အဲဒီ box ကိုပဲ ကြည့်ပါတယ်။ အဲဒီ box ကိုယ်တိုင် scroll မဖြစ်ရင် ကပ်တာကို ဘယ်တော့မှ မမြင်ရပါဘူး။

Parent က တိုလွန်း

Sticky element က parent box အတွင်းမှာပဲ ရွေ့နိုင်ပါတယ်။ Parent က header လောက်ပဲ မြင့်ရင် ကပ်စရာ နေရာ မကျန်ပါဘူး။

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

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

absolute ကို overlay သေးသေးလေးတွေအတွက် သုံးပါတယ် — card ပေါ်က sale badge၊ avatar ပေါ်က online dot၊ input ထဲက clear button၊ dropdown menu တွေပါ။ Pattern က အမြဲ အတူတူပါ: parent ကို relative ပေး၊ child ကို absolute နဲ့ နေရာချ။ Page တစ်ခုလုံးရဲ့ column တွေ၊ header နဲ့ footer လို အဓိက layout ကိုတော့ absolute နဲ့ မဆောက်သင့်ပါဘူး၊ content အရှည် ပြောင်းတာနဲ့ ထပ်နေတတ်လို့ flex ဒါမှမဟုတ် grid ကို သုံးပါ။

fixed ကို chat widget၊ cookie banner၊ "back to top" button နဲ့ mobile မှာ အောက်ခြေ navigation bar တွေအတွက် သုံးပါတယ်။ sticky ကို ဆိုက်တွေရဲ့ header၊ table ရဲ့ column heading နဲ့ contact list ထဲက A, B, C အက္ခရာ heading တွေမှာ တွေ့ရပါတယ်။ Fixed bar တွေက content ကို ဖုံးထားလို့ body မှာ padding-bottom ထည့်ပေးဖို့ မမေ့ပါနဲ့၊ ဒါမှ နောက်ဆုံး စာကြောင်းတွေ bar အောက် မပျောက်မှာပါ။

အမှားများ

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

1. Parent ကို position: relative မပေးတာ

Absolute badge တွေ page ထောင့်မှာ စုနေရင် ဒီအမှားပါ။ DevTools မှာ badge ကို ရွေးပြီး $0.offsetParent ကို console မှာ စစ်ကြည့်ရင် ဘယ် element ကနေ တိုင်းနေလဲ သိနိုင်ပါတယ်။

2. relative နဲ့ ရွှေ့ပြီး ကွက်လပ် ကျန်ခဲ့တာ

position: relative; top: -40px; နဲ့ heading ကို အပေါ်ပို့ရင် သူ့ မူလနေရာမှာ 40px ကွက်လပ် ကျန်ခဲ့ပါတယ်၊ flow က အဲဒီနေရာကို ချန်ထားလို့ပါ။ Spacing ပြင်ချင်ရင် margin ကို ပြောင်းပါ။

3. Animation ထဲက transform က fixed modal ကို ဖမ်းထားတာ

Slide-in animation အတွက် transform ပါတဲ့ panel ထဲမှာ fixed modal ကို ထည့်ထားရင် modal က viewport အပြည့် မဖုံးတော့ဘဲ panel အရွယ်ပဲ ရှိပြီး panel နဲ့အတူ scroll ဖြစ်ပါတယ်။ Modal ကို body ရဲ့ တိုက်ရိုက် child အဖြစ် render လုပ်ပါ။

Best Practices

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

  • Absolute child ရှိတဲ့ component တိုင်းရဲ့ root element မှာ position: relative ကို ထည့်ထားပါ၊ ဒါမှ component ကို ဘယ်နေရာ ရွှေ့ရွှေ့ badge က လိုက်ပါလာမှာပါ။
  • Page column တွေ၊ header နဲ့ footer လို အဓိက layout ကို flex ဒါမှမဟုတ် grid နဲ့ ဆောက်ပြီး position ကို overlay တွေအတွက်ပဲ ချန်ထားပါ။
  • Sticky header ပါတဲ့ page မှာ anchor link တွေ header အောက် မပျောက်အောင် scroll-margin-top ကို header အမြင့်အတိုင်း ပေးပါ။

Mini challenge

Mini challenge: badge နဲ့ sticky heading

Product card ပေါ်မှာ discount badge ကပ်ပြီး scroll ဖြစ်တဲ့ list ထဲမှာ အက္ခရာ heading တွေကို sticky လုပ်ပါ။

  • .badge က card ရဲ့ ညာဘက်အပေါ်ထောင့်ကနေ 8px စီ အတွင်းမှာ ရှိရမယ်
  • Badge က card ထဲက စာသားကို မတွန်းရ (flow ထဲကနေ ထွက်ရမယ်)
  • .list ကို scroll လုပ်တဲ့အခါ h3 တစ်ခုစီက .list ရဲ့ ထိပ်မှာ ကပ်နေရမယ်
  • နောက် section ရောက်တဲ့အခါ အရင် heading က တွန်းထုတ်ခံရမယ်
position.html
<style>
  .card {
    /* TODO: make the card the badge's containing block */
  }
  .badge {
    /* TODO: 8px in from the card's top-right corner */
  }
  .list {
    /* TODO: 200px tall and scrollable */
  }
  .list h3 {
    /* TODO: hold at the top of .list while scrolling */
  }
</style>

<div class="card"><h2>Wireless Mouse</h2><span class="badge">-20%</span></div>
<div class="list">
  <section>
    <h3>A</h3><p>Apple</p><p>Apricot</p><p>Avocado</p><p>Acai</p><p>Almond</p>
  </section>
  <section>
    <h3>B</h3><p>Banana</p><p>Blueberry</p><p>Brazil nut</p><p>Bilberry</p>
  </section>
</div>

ရလဒ်: Badge က card ရဲ့ ညာဘက်အပေါ်ထောင့်နားမှာ ပေါ်ပြီး card ကို ရွှေ့ရင် အတူ ပါသွားပါတယ်။ List ကို 100px scroll လုပ်ရင် "A" heading က list ထိပ်မှာ ကပ်နေပြီး "B" section ရောက်လာတဲ့အခါ အပေါ်ကို တွန်းထုတ်ခံရပါတယ်။

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

Card ကို relative ပေးပါ။ Heading မှာ background ထည့်ပါ၊ မဟုတ်ရင် ကပ်နေတဲ့အခါ အောက်က စာတွေ ဖောက်မြင်ရပါမယ်။

အဖြေ ကြည့်ရန်
position.html
<style>
  .card {
    position: relative;
    width: 260px;
    padding: 16px;
    border: 1px solid #ccc;
  }
  .badge {
    position: absolute;
    top: 8px;
    right: 8px;
    padding: 2px 8px;
    background: crimson;
    color: white;
  }
  .list {
    height: 200px;
    overflow-y: auto;
    border: 1px solid #ccc;
  }
  .list h3 {
    position: sticky;
    top: 0;
    margin: 0;
    padding: 4px 8px;
    background: #f1f5f9;
  }
</style>

<div class="card"><h2>Wireless Mouse</h2><span class="badge">-20%</span></div>
<div class="list">
  <section>
    <h3>A</h3><p>Apple</p><p>Apricot</p><p>Avocado</p><p>Acai</p><p>Almond</p>
  </section>
  <section>
    <h3>B</h3><p>Banana</p><p>Blueberry</p><p>Brazil nut</p><p>Bilberry</p>
  </section>
</div>

Quick Quiz 1 / 3

<div class="a"><div class="b"><span class="c"></span></div></div> မှာ .a က relative၊ .b က static၊ .c က absolute ဖြစ်ရင် .c ရဲ့ top ကို ဘယ်ကနေ တိုင်းမလဲ။

Quick Quiz 2 / 3

position: relative; top: 30px; ပေးထားတဲ့ element ရဲ့ အောက်က sibling တွေ ဘာဖြစ်မလဲ။

Quick Quiz 3 / 3

h2 { position: sticky; } လို့ပဲ ရေးထားပြီး heading က လုံးဝ မကပ်ပါဘူး။ အဖြစ်နိုင်ဆုံး အကြောင်းရင်းက ဘာလဲ။

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

  • relative နဲ့ sticky က flow ထဲမှာ နေရာ ချန်ထားပြီး absolute နဲ့ fixed က flow ထဲကနေ ထွက်သွားပါတယ်။
  • Absolute element က static မဟုတ်တဲ့ အနီးဆုံး ancestor ကနေ တိုင်းတာမို့ parent ကို position: relative ပေးပါ။
  • fixed က viewport မှာ ကပ်ပေမယ့် transform/filter ပါတဲ့ ancestor ထဲမှာဆိုရင် အဲဒီ ancestor ကို ကပ်သွားပါတယ်။
  • sticky အတွက် threshold (top: 0)၊ scroll ဖြစ်တဲ့ container နဲ့ နေရာလုံလောက်တဲ့ parent လိုပါတယ်။

CSS reference — MDN Web Docs

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

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

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

Position Property | Thuta Learning