ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Flex container ကို ဘတ်စ်ကားတစ်စီးလို့ မြင်ကြည့်ပါ။ Container property တွေက ကားမောင်းသူရဲ့ စည်းကမ်းတွေပါ — ခုံတန်းတွေ ဘယ်ဘက်ကို မျက်နှာမူမလဲ (flex-direction)၊ ခုံပြည့်ရင် နောက်တန်း ဆက်ထိုင်ခွင့် ရှိလား (flex-wrap)၊ ခုံတန်းထဲက လူတွေကို ဘယ်လို ဖြန့်ထိုင်ခိုင်းမလဲ (justify-content)၊ ခုံတစ်ခုချင်းမှာ ဘယ်လို နေရာယူမလဲ (align-items)၊ လူတွေကြား ဘယ်လောက် ခြားမလဲ (gap) ဆိုတာတွေပါ။
အရေးကြီးတာက ဒီ စည်းကမ်းတွေ အားလုံးကို parent ပေါ်မှာ တစ်နေရာတည်း ရေးတာပါ။ Child တစ်ခုချင်းစီကို လိုက်ပြင်စရာ မလိုဘဲ container တစ်ခုကို ပြင်ရုံနဲ့ item အားလုံး လိုက်ပြောင်းပါတယ်။
Problem → Why → Solution
ပြဿနာ: margin နဲ့ ခြားတော့ wrap ဖြစ်တဲ့အခါ ပျက်တယ်
Toolbar ထဲက button တွေကို ခြားဖို့ item တိုင်းကို margin-right: 12px ပေးထားပါတယ်။ Screen ကျဉ်းလာလို့ flex-wrap: wrap နဲ့ line နှစ်ကြောင်း ဖြစ်သွားတော့ ပြဿနာ သုံးခု ပေါ်လာပါတယ်: line တိုင်းရဲ့ နောက်ဆုံး button နောက်မှာ မလိုအပ်တဲ့ 12px ကျန်ပြီး ညာဘက် မတန်းညီတော့ပါဘူး၊ line နှစ်ကြောင်းကြားမှာ ကွက်လပ် လုံးဝ မရှိဘဲ ကပ်နေပါတယ်၊ ပြီးတော့ toolbar ကို အမြင့် ပေးထားတော့ line တွေက container တစ်ခုလုံးမှာ ဝေးဝေး ပြန့်ကျဲသွားပါတယ်။
.toolbar {
display: flex;
flex-wrap: wrap;
height: 200px; /* lines drift apart */
}
.toolbar > * {
margin-right: 12px; /* trailing space on every line */
} /* and no space between lines */ဘာကြောင့် ဒီလို ဖြစ်တာလဲ
Margin က item တစ်ခုချင်းစီရဲ့ ပိုင်ဆိုင်မှုဖြစ်လို့ item က line ရဲ့ အဆုံးမှာ ရှိနေမှန်း မသိဘဲ margin ကို အမြဲ ယူထားပါတယ်။ gap ကတော့ container ပိုင်ဆိုင်မှုဖြစ်ပြီး item နှစ်ခုကြားမှာပဲ ထည့်လို့ line အစ၊ အဆုံးမှာ ကွက်လပ် မကျန်ပါဘူး၊ row-gap ကလည်း line တွေကြားကို ခြားပေးပါတယ်။ Line တွေ ပြန့်ကျဲသွားတာကတော့ align-content ရဲ့ default (stretch) က container ရဲ့ အမြင့်ပိုကို line တွေဆီ ခွဲပေးလို့ပါ — align-content: flex-start က line တွေကို အပေါ်မှာ စုထားပေးပါတယ်။
Core concept: container property တွေကို axis အလိုက် အုပ်စုဖွဲ့ခြင်း
Container property တွေကို အုပ်စု လေးခုနဲ့ မှတ်ရင် လွယ်ပါတယ်။ ပထမ — ဦးတည်ချက်: flex-direction က row, row-reverse, column, column-reverse ထဲက တစ်ခု ဖြစ်ပါတယ်။ Reverse value တွေက item တွေကို ပြောင်းပြန် ပြပေမယ့် HTML အစဉ်ကို မပြောင်းလို့ Tab key နဲ့ သွားတဲ့ အစဉ်က မူလအတိုင်း ကျန်ပါတယ်။ ဒုတိယ — line ခွဲခြင်း: flex-wrap ရဲ့ default က nowrap ဖြစ်လို့ item တွေက line တစ်ကြောင်းတည်းမှာ ကျုံ့ပြီး ဝင်အောင် ကြိုးစားပါတယ်၊ wrap ပေးမှ line အသစ်ကို ဆင်းပါတယ်။ flex-flow: row wrap က property နှစ်ခုကို တစ်ကြောင်းတည်း ရေးတဲ့ shorthand ပါ။
တတိယ — main axis ပေါ်က free space ဖြန့်ခြင်း: justify-content ပါ။ 400px row ထဲမှာ 50px item သုံးခု ရှိရင် free space က 250px ဖြစ်ပြီး value တစ်ခုစီက အဲဒီ 250px ကို အောက်က ဇယားအတိုင်း ခွဲပါတယ်။ Column မှာလည်း value တွေက အပေါ်ကနေ အောက် အတူတူ အလုပ်လုပ်ပေမယ့် container မှာ ခွဲစရာ free space ကျန်အောင် height ရှိမှ ဖြစ်ပါတယ်။
| Value | Item တွေရဲ့ ဘယ်ဘက် အစွန်း (px) |
|---|---|
| flex-start (default) | 0, 50, 100 — အားလုံး အစမှာ စု |
| center | 125, 175, 225 |
| space-between | 0, 175, 350 — အစွန်းမှာ ကွက်လပ် မရှိ |
| space-around | 41.7, 175, 308.3 — အစွန်းမှာ တစ်ဝက် |
| space-evenly | 62.5, 175, 287.5 — ကွက်လပ် အားလုံး တူ |
စတုတ္ထ — cross axis ပေါ်က ညှိခြင်း: align-items က line တစ်ကြောင်းအတွင်းမှာ item တစ်ခုချင်းကို ညှိပါတယ် (stretch default၊ flex-start, flex-end, center, baseline)။ baseline က font size မတူတဲ့ item တွေရဲ့ စာသား အောက်ခြေမျဉ်းကို တန်းညီစေပါတယ်။ align-content ကတော့ line အများကြီးရှိတဲ့ wrap container မှာ line တွေကို အုပ်စုလိုက် ညှိတာဖြစ်ပြီး nowrap container မှာ လုံးဝ အကျိုး မရှိပါဘူး။ နောက်ဆုံး gap (row-gap နဲ့ column-gap ရဲ့ shorthand) က item တွေနဲ့ line တွေကြားကိုပဲ ခြားပေးပါတယ်။ Free space ကို မတွက်ခင် gap နေရာကို အရင် နုတ်လို့ justify-content က gap တွေ ယူပြီး ကျန်တဲ့ နေရာကိုပဲ ဖြန့်ပါတယ်။
- Direction: axis နဲ့ အစဉ်
- Wrap: line တစ်ကြောင်းလား များလား
- justify-content: line ထဲက free space
- align-items / align-content: cross axis
- gap: item နဲ့ line ကြား ကွက်လပ်
align-items က line တစ်ကြောင်းထဲက item တွေကို၊ align-content က line တွေကို ရွှေ့ပါတယ်။
အထွေထွေ ပုံစံ
container {
display: flex;
flex-direction: row | row-reverse | column | column-reverse;
flex-wrap: nowrap | wrap;
justify-content: flex-start | center | space-between | ...;
align-items: stretch | flex-start | center | baseline | ...;
align-content: normal | flex-start | center | ...; /* wrap only */
gap: row-gap column-gap;
}ဒီ property အားလုံးကို parent ပေါ်မှာ ရေးပါတယ်။ align-content က wrap ဖြစ်တဲ့ container မှာပဲ အဓိပ္ပာယ် ရှိပြီး gap က value တစ်ခုပဲ ပေးရင် row နဲ့ column နှစ်ဖက်လုံးကို အတူတူ ခြားပါတယ်။
Example: wrap ဖြစ်တဲ့ feature card row
ဒီ example က container property လေးခုကို တွဲသုံးပြီး screen အကျယ်အလိုက် line ခွဲပေးတဲ့ card row တစ်ခု ဆောက်ပါတယ်။ .feature-card ပေါ်က flex shorthand ကို နောက် lesson မှာ အသေးစိတ် လေ့လာပါမယ်၊ ဒီမှာတော့ "240px ကနေ စပြီး ကြီးလည်း ကြီး၊ ကျုံ့လည်း ကျုံ့နိုင်တယ်" လို့ပဲ မှတ်ထားပါ။
.features {
display: flex;
flex-wrap: wrap;
gap: 20px;
justify-content: center;
align-items: stretch;
}
.feature-card {
flex: 1 1 240px;
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Line 3
flex-wrap: wrap;Card တစ်ခုရဲ့ 240px မဆံ့တော့တဲ့အခါ line အသစ်ကို ဆင်းခွင့်ပေးပါတယ်။ ဒီ line မပါရင် card သုံးခုလုံး line တစ်ကြောင်းတည်းမှာ ကျုံ့ပြီး ဆက်နေပါမယ်။
- Line 4
gap: 20px;Card တွေကြား ဘယ်ညာ 20px နဲ့ wrap ဖြစ်တဲ့ line တွေကြား အပေါ်အောက် 20px ကို တစ်ခါတည်း ပေးပါတယ်။ Line အစွန်းမှာ ကွက်လပ် မကျန်ပါဘူး။
- Line 5
justify-content: center;Card တွေက grow ဖြစ်ပြီး line ကို အပြည့် ယူထားလို့ free space မကျန်ပါဘူး၊ ဒါကြောင့် ဒီ line ရဲ့ အကျိုးကို တကယ်တမ်း မမြင်ရပါဘူး။ Card တွေကို grow မပေးဘဲ flex: 0 1 240px ပြောင်းရင်တော့ line တိုင်းက အလယ်မှာ စုသွားပါမယ်။
- Line 6
align-items: stretch;Default value ကို ထင်ရှားအောင် ရေးထားတာပါ။ Line တစ်ကြောင်းထဲက card တွေက အရှည်ဆုံး card ရဲ့ အမြင့်အတိုင်း ညီညီ မြင့်ပါတယ်။
- Line 10
flex: 1 1 240px;Card တစ်ခုစီက 240px ကနေ စပြီး line မှာ ကျန်တဲ့ နေရာကို ခွဲယူပါတယ်။ 240px က အနိမ့်ဆုံး width မဟုတ်ပါဘူး — container က 240px ထက် ကျဉ်းရင် card က ဆက်ကျုံ့ပါတယ်။
800px container မှာ card သုံးခုလုံး line တစ်ကြောင်းတည်းမှာ 253px ခန့်စီ ရှိပါတယ်။ 500px မှာ ပထမ line မှာ 240px card နှစ်ခု ရှိပြီး တတိယ card က နောက် line ကို ဆင်းကာ 500px အပြည့် ဆန့်ပါတယ်။ 200px container မှာတော့ card က 200px အထိ ကျုံ့ပါတယ်။ Line တစ်ကြောင်းထဲက card တွေ အမြင့် တူပါတယ်။align-items နဲ့ align-content ကို မြင်အောင် ကြည့်ခြင်း
flex-wrap: wrap, container 200px tall, two lines
------------------------------------------------
align-content: normal (stretch) align-content: flex-start
+----------------------------+ +----------------------------+
| [a] [b] [c] [d] | | [a] [b] [c] [d] |
| | | [e] |
| line 1 gets half the | | |
| extra height | | lines packed at the top, |
| [e] | | free space stays below |
| | | |
+----------------------------+ +----------------------------+ဘယ်ဘက်ပုံမှာ 20px မြင့်တဲ့ item တွေပါတဲ့ line နှစ်ကြောင်းက 200px container ကို 100px စီ ခွဲယူလို့ ဒုတိယ line က container ရဲ့ အလယ်ကနေ စပါတယ်။ align-items က line တစ်ကြောင်းစီရဲ့ အတွင်းမှာပဲ item ကို ညှိနိုင်လို့ ဒီ ကွက်လပ်ကြီးကို မဖြေရှင်းနိုင်ပါဘူး၊ align-content ကမှ line တွေကို ရွှေ့နိုင်ပါတယ်။ ညာဘက် box မှာ center, space-between ဒါမှမဟုတ် flex-end ကို စမ်းပေးကြည့်ပါ၊ line နှစ်ကြောင်းက block တစ်ခုလို အတူတူ ရွေ့သွားတာကို မြင်ရပါမယ်။
လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
Site header က justify-content: space-between နဲ့ align-items: center ကို အသုံးအများဆုံး တွဲပါ — logo ကို ဘယ်ဘက်အစွန်း၊ menu ကို ညာဘက်အစွန်း ပို့ပြီး အမြင့်မတူတဲ့ နှစ်ခုကို အလယ်မျဉ်းမှာ ညှိပါတယ်။ Tag list၊ filter chip၊ e-commerce ရဲ့ size ရွေးတဲ့ button တွေကတော့ flex-wrap: wrap နဲ့ gap: 8px လို ပုံစံနဲ့ ရေးပြီး screen အကျယ်အလိုက် line အလိုလို ခွဲပါတယ်။
Form ရဲ့ action bar မှာ "Cancel" နဲ့ "Save" ကို ညာဘက် ကပ်ဖို့ justify-content: flex-end ကို သုံးပါတယ်။ Pricing table မှာ နံပါတ်ကြီးကြီးနဲ့ "/month" စာသားသေးကို align-items: baseline နဲ့ စာသား အောက်ခြေမျဉ်း တန်းညီအောင် ထားပါတယ်။ Chat app တွေမှာ message အသစ်တွေကို အောက်ကနေ စ ပြဖို့ flex-direction: column-reverse ကို သုံးတာ တွေ့ရပေမယ့် screen reader ဖတ်တဲ့ အစဉ်က HTML အတိုင်း ဖြစ်ကြောင်း စစ်ဆေးပါ။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. nowrap container မှာ align-content ကို စမ်းတာ
flex-wrap မပေးထားတဲ့ container မှာ align-content: center ပေးရင် ဘာမှ မဖြစ်ပါဘူး၊ ဒါ browser bug မဟုတ်ပါဘူး။ Line တစ်ကြောင်းတည်းကို ညှိချင်ရင် align-items ကို သုံးပါ၊ line တွေကို ညှိချင်ရင် flex-wrap: wrap ကို အရင် ပေးပါ။
2. Spacing အတွက် item margin ကို သုံးတာ
margin-right နဲ့ ခြားရင် နောက်ဆုံး item မှာ :last-child { margin-right: 0 } လို hack လိုလာပြီး wrap ဖြစ်ရင် ဒါတောင် မလုံလောက်ပါဘူး။ Container ပေါ်မှာ gap ကို ရေးပါ — ခေတ်မီ browser အားလုံးက flexbox gap ကို support လုပ်ပါတယ်။
3. Reverse direction ကို အစဉ် ပြင်ဖို့ သုံးတာ
row-reverse က မျက်စိနဲ့ မြင်ရတဲ့ အစဉ်ကိုပဲ ပြောင်းပြီး keyboard Tab နဲ့ screen reader အစဉ်က HTML အတိုင်း ကျန်ပါတယ်။ အဓိပ္ပာယ်ရှိတဲ့ အစဉ် ပြောင်းချင်ရင် HTML ကို ပြင်ပါ။ မဟုတ်ရင် keyboard အသုံးပြုသူတွေက focus က screen ပေါ်မှာ နောက်ပြန် ခုန်နေတာကို မြင်ရပြီး ရှုပ်ထွေးစေသလို accessibility audit မှာလည်း ကျရှုံးပါတယ်။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- Item အရေအတွက် ပြောင်းနိုင်တဲ့ list (tag, chip, button) တိုင်းမှာ flex-wrap: wrap ကို ကြိုထည့်ထားပါ၊ စာသား ရှည်လာတဲ့ ဘာသာစကားတွေမှာ overflow မဖြစ်တော့ပါဘူး။
- Wrap container ကို height ပေးရရင် align-content ကိုပါ တစ်ခါတည်း သတ်မှတ်ပါ။
- space-between ကို item နှစ်ခုထက် ပိုတဲ့ wrap list မှာ ရှောင်ပါ၊ နောက်ဆုံး line က item နည်းရင် ဝေးဝေး ကွဲထွက်သွားပါတယ်။
Mini challenge
Mini challenge: header နဲ့ tag box
Container property တွေ သက်သက်နဲ့ (item တွေကို style မပေးဘဲ) header တစ်ခုနဲ့ wrap ဖြစ်တဲ့ tag box တစ်ခု ဆောက်ပါ။
- .header မှာ logo ကို ဘယ်ဘက်အစွန်း၊ .menu ကို ညာဘက်အစွန်း ထားပြီး cross axis မှာ အလယ်ညှိရမယ်
- .menu ထဲက link တွေကို 16px ခြားရမယ် (margin မသုံးရ)
- .tags (240px x 160px) ထဲမှာ tag တွေ wrap ဖြစ်ပြီး ဘယ်ညာ 12px၊ line ကြား 8px ခြားရမယ်
- Tag line တွေက box ရဲ့ အပေါ်မှာ စုနေရမယ်၊ box တစ်ခုလုံး မပြန့်ကျဲရ
<style>
/* TODO: .header = logo left edge, menu right edge, centred on the cross axis */
/* TODO: .menu = links 16px apart, no margins */
/* TODO: .tags = 240px x 160px, wrapping, 12px between tags, 8px between lines,
lines packed at the top */
.tags span { padding: 4px 8px; background: #e2e8f0; }
</style>
<header class="header">
<strong style="font-size: 28px">Shop</strong>
<nav class="menu"><a href="#">Deals</a><a href="#">Cart</a></nav>
</header>
<div class="tags">
<span>css</span><span>flexbox</span><span>layout</span><span>gap</span>
<span>wrap</span><span>align</span><span>responsive</span>
</div>ရလဒ်: "Shop" က ဘယ်ဘက်အစွန်း၊ Deals/Cart က ညာဘက်အစွန်းမှာ ရှိပြီး နှစ်ခုရဲ့ အလယ်မျဉ်း တူပါတယ်။ Tag တွေက 240px box ထဲမှာ line နှစ်ကြောင်း ခွဲပြီး line တွေကြား 8px ပဲ ခြားကာ box ရဲ့ အပေါ်ပိုင်းမှာ စုနေပြီး အောက်ပိုင်းမှာ နေရာလွတ် ကျန်ပါတယ်။
အရိပ်အမြွက် ကြည့်ရန်
gap ကို value နှစ်ခု ပေးရင် ပထမက row-gap (line ကြား)၊ ဒုတိယက column-gap (item ကြား) ပါ။ Line တွေကို အပေါ်မှာ စုဖို့ align-content ကို သုံးပါ။
အဖြေ ကြည့်ရန်
<style>
.header {
display: flex;
justify-content: space-between;
align-items: center;
}
.menu {
display: flex;
gap: 16px;
}
.tags {
display: flex;
flex-wrap: wrap;
gap: 8px 12px;
align-content: flex-start;
width: 240px;
height: 160px;
border: 1px solid #cbd5e1;
}
.tags span { padding: 4px 8px; background: #e2e8f0; }
</style>
<header class="header">
<strong style="font-size: 28px">Shop</strong>
<nav class="menu"><a href="#">Deals</a><a href="#">Cart</a></nav>
</header>
<div class="tags">
<span>css</span><span>flexbox</span><span>layout</span><span>gap</span>
<span>wrap</span><span>align</span><span>responsive</span>
</div>Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- flex-direction က axis နဲ့ visual အစဉ်ကို ရွေးပြီး reverse value တွေက HTML/Tab အစဉ်ကို မပြောင်းပါဘူး။
- justify-content က main axis ပေါ်က free space ကို ဖြန့်လို့ item တွေ grow ဖြစ်နေရင် အကျိုး မမြင်ရပါဘူး။
- align-items က line တစ်ကြောင်းအတွင်း item တွေကို၊ align-content က wrap container ထဲက line တွေကို ညှိပါတယ်။
- Spacing အတွက် item margin အစား container ပေါ်က gap ကို သုံးပါ။
Basic concepts of Flexbox — MDN Web Docs