Thuta Learning
CSS
AdvancedWeb Developmentအခြေခံ

Flex Items

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

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

  • flex-basis ကနေ စပြီး free space ကို flex-grow အချိုးအတိုင်း ခွဲပေးတာကို pixel နဲ့ တွက်နိုင်ဖို့
  • flex-shrink က basis နဲ့ အလေးချိန်တွက်ပြီး ကျုံ့တာကို ရှင်းပြနိုင်ဖို့
  • flex: 1, flex: auto, flex: none နဲ့ flex: 0 0 200px ကို ရည်ရွယ်ချက်အလိုက် ရွေးနိုင်ဖို့
  • min-width: auto ကြောင့် item မကျုံ့တဲ့ ပြဿနာကို min-width: 0 နဲ့ ဖြေရှင်းနိုင်ဖို့
  • order နဲ့ align-self ကို သုံးပြီး order က accessibility ကို ဘယ်လို ထိခိုက်နိုင်လဲ သိဖို့

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

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

Container property တွေက ဘတ်စ်ကားရဲ့ စည်းကမ်းဆိုရင် item property တွေက ခရီးသည် တစ်ယောက်ချင်းစီရဲ့ ဆန္ဒပါ။ flex-basis က "ငါ ဒီလောက် နေရာ လိုတယ်" ဆိုတဲ့ အစ အရွယ်ပါ။ flex-grow က ခုံတွေ ပိုနေရင် "ပိုတဲ့ နေရာကို ငါ ဘယ်နှစ်ပုံ ယူမလဲ"၊ flex-shrink က ကျပ်နေရင် "ငါ ဘယ်လောက် ချုံ့ပေးမလဲ" ဆိုတာပါ။

order က ခရီးသည်ကို တန်းစီတဲ့ အစဉ်မှာ ရှေ့ကို ဒါမှမဟုတ် နောက်ကို ပြောင်းပေးပြီး align-self က တစ်ယောက်တည်းကို ကျန်လူတွေနဲ့ မတူတဲ့ ပုံစံ ထိုင်ခွင့် ပေးတာပါ။ ဒီ property တွေ အားလုံးကို container မှာ မဟုတ်ဘဲ child item ပေါ်မှာ ရေးပါတယ်။

Problem → Why → Solution

ပြဿနာ: content column က sidebar ကို ညှပ်ပြီး screen အပြင် ထွက်သွားတယ်

Sidebar ကို width: 200px၊ content ကို flex: 1 ပေးထားတဲ့ layout တစ်ခု ရှိပါတယ်။ Content ထဲကို ရှည်လျားတဲ့ URL ပါတဲ့ <pre> block တစ်ခု ထည့်လိုက်တော့ content column က container ထက် ကျယ်လာပြီး အပြင်ကို ဖိတ်ထွက်သွားပါတယ် (full-width layout ဆိုရင် page က ဘေးတိုက် scroll ဖြစ်လာပါတယ်)၊ sidebar ကလည်း 200px ထက် အများကြီး ငယ်သွားပြီး "Menu" ဆိုတဲ့ စာလုံး အကျယ်လောက်အထိ ညှပ်ခံရပါတယ်။ flex: 1 ဆိုတာ "ကျန်တဲ့ နေရာကိုပဲ ယူ" လို့ ထင်ထားတာ ဘာကြောင့် မမှန်တာလဲ။

layout.css
.layout  { display: flex; }
.sidebar { width: 200px; }   /* flex-shrink is still 1 */
.content { flex: 1; }        /* min-width is still auto */

/* <main class="content"><pre>https://example.com/very/long/url...</pre></main> */

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

Flex item တွေရဲ့ min-width ရဲ့ default က auto ဖြစ်ပြီး အဲဒါက "content ရဲ့ အသေးဆုံး အရွယ် (min-content) ထက် မကျုံ့နဲ့" လို့ ဆိုလိုပါတယ်။ ဖြတ်လို့မရတဲ့ URL ရဲ့ min-content က အရမ်း ကျယ်လို့ content column က မကျုံ့နိုင်ပါဘူး။ နေရာ မလောက်တော့ shrink ဖြစ်နိုင်တဲ့ တစ်ခုတည်းသော item ဖြစ်တဲ့ sidebar (flex-shrink: 1 default) က ညှပ်ခံရပါတယ်။ ဖြေရှင်းချက်က content မှာ min-width: 0၊ sidebar မှာ flex: 0 0 200px ပေးတာပါ။

Core concept: basis ကနေ စ၊ ပြီးမှ grow ဒါမှမဟုတ် shrink

Browser က flex item တွေရဲ့ main-axis အရွယ်ကို အဆင့်နှစ်ဆင့်နဲ့ တွက်ပါတယ်။ ပထမ — item တစ်ခုစီရဲ့ flex-basis ကို ယူပါတယ်။ basis က auto (default) ဆိုရင် item ရဲ့ width (column မှာ height) ကို သုံးပြီး အဲဒါလည်း မရှိရင် content အရွယ်ကို သုံးပါတယ်။ ဒုတိယ — container အရွယ်ထဲက basis တွေ (gap တွေပါ) ကို နုတ်ပြီး ကျန်တာ အပေါင်း ဖြစ်ရင် grow၊ အနုတ် ဖြစ်ရင် shrink လုပ်ပါတယ်။

Grow က ရိုးရှင်းပါတယ် — free space ကို flex-grow တန်ဖိုးတွေရဲ့ အချိုးအတိုင်း ခွဲပါတယ်။ 600px container ထဲမှာ basis 100px (grow 1) နဲ့ 200px (grow 2) ရှိရင် free space 300px ကို 1:2 နဲ့ ခွဲလို့ 100 + 100 = 200px နဲ့ 200 + 200 = 400px ရပါတယ်။ grow 2 ဆိုတာ "နှစ်ဆ ကျယ်" မဟုတ်ဘဲ "free space ကို နှစ်ပုံ ယူ" လို့ ဆိုလိုတာ သတိထားပါ။

Shrink ကတော့ basis နဲ့ အလေးချိန် တွက်ပါတယ်၊ ဒါမှ item ကြီးတွေက ပိုများများ ကျုံ့ပြီး item သေးတွေ ပျောက်မသွားမှာပါ။ 300px container ထဲမှာ basis 200px item နှစ်ခုက 100px ပိုနေပါတယ်။ shrink 1 နဲ့ 1 ဆိုရင် 50px စီ ကျုံ့လို့ 150px စီ ရပြီး shrink 1 နဲ့ 3 ဆိုရင် 25px နဲ့ 75px ကျုံ့လို့ 175px နဲ့ 125px ရပါတယ်။ flex-shrink: 0 ပေးထားတဲ့ item ကတော့ လုံးဝ မကျုံ့ပါဘူး။ နောက်ထပ် ကန့်သတ်ချက် တစ်ခုလည်း ရှိပါတယ် — flex item ရဲ့ min-width default က auto ဖြစ်လို့ min-width: 0 မပေးရင်၊ ဒါမှမဟုတ် overflow ကို visible မဟုတ်တဲ့ value မပေးရင် content ရဲ့ အသေးဆုံး ဖြစ်နိုင်တဲ့ အကျယ်ထက် မကျုံ့ပါဘူး။

Shorthandအပြည့်အဓိပ္ပာယ်
flex: initial (default)0 1 autoမကြီး၊ လိုရင် ကျုံ့
flex: 11 1 0%Content မကြည့်ဘဲ နေရာ တူတူ ခွဲ
flex: auto1 1 autoContent အရွယ် + ကျန်တာ တူတူ ခွဲ
flex: none0 0 autoအရွယ် မပြောင်း
Item အရွယ် တွက်နည်း
  1. flex-basis ကနေ စ
  2. Container − basis တွေ − gap = free space
  3. အပေါင်း: grow အချိုးနဲ့ ခွဲ
  4. အနုတ်: shrink × basis နဲ့ ကျုံ့
  5. min-width / max-width နဲ့ ကန့်သတ်

Item က မကျုံ့ရင် shrink ကို မစစ်ခင် min-width: auto ကို အရင် သံသယ ဝင်ပါ။

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

css
item {
  flex: grow shrink basis;   /* e.g. 1 1 240px */
  min-width: 0;              /* allow shrinking below content */
  order: integer;            /* visual order only, default 0 */
  align-self: auto | flex-start | center | flex-end | stretch;
}

flex shorthand ကို value သုံးခုနဲ့ ရေးတာ အရှင်းဆုံးပါ။ order က အငယ်ဆုံး နံပါတ်ကို ရှေ့ဆုံး ပြပြီး align-self က container ရဲ့ align-items ကို item တစ်ခုတည်းအတွက် override လုပ်ပါတယ်။

Example: pricing card နဲ့ featured card

HTML ထဲမှာ card သုံးခု (Basic, Pro, Team) ရှိပြီး အားလုံးမှာ pricing-card class ပါကာ Pro မှာ featured-card class ပါ ထပ်ပါတယ်လို့ ယူဆပါ။ Container က 1000px ကျယ်တဲ့ display: flex row ပါ။

.pricing-card {
  flex: 1 1 280px;
}

.featured-card {
  order: -1;
  flex-grow: 1.3;
}

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

  1. Line 2flex: 1 1 280px;

    Card တိုင်း 280px basis ကနေ စပါတယ်။ 3 x 280 = 840px ဖြစ်လို့ 160px free space ကျန်ပြီး grow ဖြစ်တဲ့ card တွေဆီ ခွဲပေးပါတယ်။ Container 840px ထက် ကျဉ်းရင် card အားလုံး တူတူ ကျုံ့ပါတယ်။

  2. Line 5.featured-card {

    Pro card မှာ class နှစ်ခုလုံး ရှိပြီး specificity တူလို့ stylesheet ထဲ နောက်မှ ရေးတဲ့ ဒီ rule က flex-grow ကို override လုပ်ပါတယ်။ shrink နဲ့ basis ကတော့ .pricing-card ကနေ ဆက်ရပါတယ်။

  3. Line 6order: -1;

    ကျန်တဲ့ card တွေရဲ့ order က 0 (default) ဖြစ်လို့ -1 က Pro ကို မျက်စိနဲ့ မြင်ရတဲ့ ရှေ့ဆုံးကို ပို့ပါတယ်။ Tab key နဲ့ screen reader အစဉ်ကတော့ HTML အတိုင်း Basic, Pro, Team ပဲ ကျန်ပါတယ်။

  4. Line 7flex-grow: 1.3;

    Free space 160px ကို 1.3 : 1 : 1 နဲ့ ခွဲလို့ Pro က 63px ခန့်၊ ကျန်နှစ်ခုက 48.5px ခန့်စီ ရပါတယ်။ Pro က 1.3 ဆ ကျယ်တာ မဟုတ်ဘဲ 15px ခန့်ပဲ ပိုကျယ်ပါတယ်။

You should see
1000px row မှာ Pro card က ရှေ့ဆုံး (ဘယ်ဘက်ဆုံး) ကို ရောက်ပြီး 343px ခန့် ကျယ်ပါတယ်။ ပြီးမှ Basic နဲ့ Team က 328.5px ခန့်စီ ကျယ်ပါတယ်။

align-self နဲ့ order ကို ဘယ်အချိန် သုံးမလဲ

align-self က item တစ်ခုတည်းကို cross axis မှာ ကျန်တာတွေနဲ့ မတူအောင် ညှိပါတယ်။ ဥပမာ container က default stretch ဖြစ်နေပေမယ့် sidebar ထဲက ကြော်ငြာ box ကို align-self: flex-start ပေးရင် box က column အမြင့်အပြည့် မဆန့်တော့ဘဲ သူ့ content အရွယ်ပဲ ရှိပါတယ်။ ဒါက sticky box တွေအတွက် အရေးကြီးပါတယ်၊ stretch ဖြစ်နေတဲ့ box မှာ ကပ်ဖို့ နေရာ မကျန်လို့ပါ။

order ကို သတိနဲ့ သုံးပါ

order က visual အစဉ်ကိုပဲ ပြောင်းပါတယ်။ Featured card ကို ရှေ့ပို့တာလို အလှဆင်တဲ့ ပြောင်းလဲမှုမှာ အဆင်ပြေပေမယ့် form field တွေ၊ အဆင့်လိုက် ညွှန်ကြားချက်တွေလို အစဉ်က အဓိပ္ပာယ် ရှိတဲ့ နေရာမှာ HTML ကို ပြင်ပါ။

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

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

Dashboard တွေရဲ့ အခြေခံ layout က flex: 0 0 240px sidebar နဲ့ flex: 1; min-width: 0 main area ပါ။ min-width: 0 ကို မေ့ရင် ကျယ်တဲ့ table, chart ဒါမှမဟုတ် code block တစ်ခုက layout တစ်ခုလုံးကို ဖောက်ထွက်စေပါတယ်။ Search bar မှာ input ကို flex: 1 ပေးပြီး button ကို flex: none ပေးလေ့ ရှိပါတယ် — input က နေရာ ကျန်သမျှ ယူပြီး button က သူ့စာသား အရွယ်အတိုင်း မပြောင်းဘဲ နေပါတယ်။

Card row တွေမှာ flex: 1 1 280px လို shorthand နဲ့ flex-wrap ကို တွဲပြီး media query မလိုဘဲ responsive ဖြစ်အောင် လုပ်ပါတယ်။ Chat bubble တွေမှာ ကိုယ့် message ကို align-self: flex-end နဲ့ ညာဘက်ကပ်ပြီး တခြားသူ့ message ကို flex-start နဲ့ ဘယ်ဘက်ကပ်ထားတာလည်း တွေ့ရပါတယ်။ Mobile ပေါ်မှာ product ပုံကို စာသားရဲ့ အပေါ်ကို ပို့ချင်တဲ့အခါ media query ထဲမှာ order ကို သုံးတတ်ပေမယ့် ပုံက အလှဆင်တာ ဖြစ်မှသာ ဘေးကင်းပါတယ်။

အမှားများ

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

1. min-width: 0 ကို မေ့တာ

ရှည်တဲ့ URL, <pre> ဒါမှမဟုတ် text-overflow: ellipsis ပါတဲ့ စာသားကို flex item ထဲ ထည့်ပြီး "ellipsis မပေါ်ဘူး" ဒါမှမဟုတ် "layout ဖောက်ထွက်တယ်" ဆိုရင် item မှာ min-width: 0 ပေးပါ (column မှာဆို min-height: 0)။

2. width ပေးပြီး fixed ဖြစ်မယ်ထင်တာ

Sidebar ကို width: 200px ပေးရုံနဲ့ flex-shrink: 1 က ကျန်နေလို့ နေရာ ကျပ်ရင် ကျုံ့သွားပါတယ်။ အရွယ် အတိအကျ လိုရင် flex: 0 0 200px လို့ ရေးပါ။

3. flex: 1 နဲ့ flex: auto ကို ရောတာ

Tab bar မှာ tab တွေ အကျယ် တူစေချင်ရင် flex: 1 (basis 0) ကို သုံးရပါတယ်။ flex: auto (basis auto) ကို သုံးရင် စာသား ရှည်တဲ့ tab က ပိုကျယ်သွားပါတယ် — 600px row မှာ "Hi" နဲ့ စာသားရှည် tab က 300/300 မဟုတ်ဘဲ 227/373 ခန့် ဖြစ်ပါတယ်။

Best Practices

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

  • flex-grow, flex-shrink, flex-basis ကို သီးခြား မရေးဘဲ flex shorthand ကို value သုံးခုလုံးနဲ့ ရေးပါ၊ မရေးထားတဲ့ value တွေရဲ့ default ကို ခန့်မှန်းစရာ မလိုတော့ပါဘူး။
  • ကျန်တဲ့ နေရာကို ယူရမယ့် main column တိုင်းမှာ flex: 1 နဲ့ min-width: 0 ကို အတွဲလိုက် ရေးပါ။
  • order ကို သုံးပြီးတိုင်း Tab key နဲ့ page ကို တစ်ခါ ဖြတ်သွားကြည့်ပါ။

Mini challenge

Mini challenge: sidebar, content နဲ့ ad

800px ကျယ်တဲ့ flex row ထဲမှာ column သုံးခုကို item property တွေနဲ့ အရွယ် သတ်မှတ်ပါ။

  • .sidebar က 200px အတိအကျ ဖြစ်ရမယ်၊ မကြီးရ၊ မကျုံ့ရ
  • .content က ကျန်တဲ့ နေရာ အားလုံး ယူရမယ်၊ ရှည်တဲ့ URL ကြောင့် မကျယ်လာရ
  • <pre> က ဘေးတိုက် overflow မဖြစ်ဘဲ သူ့အထဲမှာ scroll ဖြစ်ရမယ်
  • .ad က 160px ကနေ စ၊ ကျုံ့လို့ရ၊ မကြီးရ၊ row အမြင့်အပြည့် မဆန့်ရ
items.html
<style>
  .layout { display: flex; gap: 16px; width: 800px; }
  /* TODO: .sidebar = exactly 200px, never grows or shrinks */
  /* TODO: .content = all remaining width, even with the long URL */
  /* TODO: pre = scroll inside instead of overflowing */
  /* TODO: .ad = starts at 160px, may shrink, never grows, not stretched */
</style>

<div class="layout">
  <nav class="sidebar">Menu</nav>
  <main class="content">
    <h1>Release notes</h1>
    <pre>https://example.com/downloads/2026/a-very-long-unbroken-file-name.zip</pre>
  </main>
  <aside class="ad">Ad</aside>
</div>

ရလဒ်: Sidebar 200px၊ content 408px (800 − 200 − 160 − gap 2 ခု x 16)၊ ad 160px ဖြစ်ပါတယ်။ URL က content ထဲမှာ ဘေးတိုက် scroll ဖြစ်ပြီး ad က "Ad" စာသား အမြင့်လောက်ပဲ ရှိပါတယ်။

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

Content အတွက် flex: 1 တစ်ခုတည်း မလုံလောက်ပါဘူး၊ min-width ကို ပြင်ရပါမယ်။ Ad ကို stretch မဖြစ်စေဖို့ container ကို မထိဘဲ item ပေါ်မှာ align-self ကို သုံးပါ။

အဖြေ ကြည့်ရန်
items.html
<style>
  .layout { display: flex; gap: 16px; width: 800px; }
  .sidebar {
    flex: 0 0 200px;
    background: #f1f5f9;
  }
  .content {
    flex: 1;
    min-width: 0;
  }
  pre {
    overflow-x: auto;
    background: #0f172a;
    color: #e2e8f0;
  }
  .ad {
    flex: 0 1 160px;
    align-self: flex-start;
    background: #fef3c7;
  }
</style>

<div class="layout">
  <nav class="sidebar">Menu</nav>
  <main class="content">
    <h1>Release notes</h1>
    <pre>https://example.com/downloads/2026/a-very-long-unbroken-file-name.zip</pre>
  </main>
  <aside class="ad">Ad</aside>
</div>

Quick Quiz 1 / 3

500px row ထဲမှာ A { flex: 1 0 100px } နဲ့ B { flex: 3 0 100px } ရှိရင် B ဘယ်လောက် ကျယ်မလဲ။

Quick Quiz 2 / 3

flex: 1 ပေးထားတဲ့ item ထဲမှာ white-space: nowrap, overflow: hidden, text-overflow: ellipsis ပါတဲ့ <p> ရှိပေမယ့် ellipsis မပေါ်ဘဲ item က ကျယ်ထွက်နေပါတယ်။ ဘာ ထည့်ရမလဲ။

Quick Quiz 3 / 3

HTML အစဉ်က A, B, C ဖြစ်ပြီး C { order: -1 } ပေးထားရင် Tab key နဲ့ focus ဘယ်လို ရွေ့မလဲ။

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

  • Item အရွယ်က flex-basis ကနေ စပြီး free space ကို grow အချိုးနဲ့ ခွဲ၊ overflow ကို shrink × basis နဲ့ ကျုံ့ပါတယ်။
  • flex: 1 က တူညီတဲ့ အပိုင်း၊ flex: auto က content + အပို၊ flex: 0 0 200px က fixed အရွယ်ပါ။
  • Item မကျုံ့ရင် min-width: 0 (column မှာ min-height: 0) ကို ထည့်ပါ။
  • align-self က item တစ်ခုတည်းကို cross axis မှာ ညှိပြီး order က visual အစဉ်ကိုပဲ ပြောင်းပါတယ်။

Basic concepts of Flexbox — MDN Web Docs

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

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

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

Flex Items | Thuta Learning