ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Flexbox ကို ကြိုးတစ်ချောင်းပေါ်မှာ ပုတီးစေ့တွေ သီထားတာလို့ မြင်ကြည့်ပါ။ ကြိုး (main axis) ကို အလျားလိုက် ဆွဲထားလည်း ရ၊ ဒေါင်လိုက် ဆွဲထားလည်း ရပါတယ်။ ပုတီးစေ့တွေ (flex item တွေ) က ကြိုးတစ်လျှောက် အစဉ်လိုက် စီနေပြီး ကြိုးပေါ်မှာ ဘယ်လို ဖြန့်မလဲ (ဘယ်ဘက်ကပ်၊ အလယ်၊ ညီညီခြား) ဆိုတာနဲ့ ကြိုးရဲ့ ဘေးဘက်မှာ ဘယ်လို ညှိမလဲ ဆိုတာကို သီးခြား ထိန်းလို့ ရပါတယ်။
ဒါကြောင့် navigation bar၊ button group၊ icon နဲ့ စာသား တွဲထားတဲ့ row၊ "ဒီ box ကို အလယ်တည့်တည့် ထားချင်တယ်" ဆိုတဲ့ ပြဿနာတွေမှာ flexbox ကို အသုံးများပါတယ်။ ဒါတွေ အားလုံးက ကြိုး တစ်ချောင်းတည်းပေါ်က အရာတွေ ဖြစ်လို့ပါ။
Problem → Why → Solution
ပြဿနာ: column ပြောင်းလိုက်တော့ center က ပျက်သွားတယ်
"justify-content = horizontal၊ align-items = vertical" လို့ မှတ်ထားပြီး card ထဲက button ကို အောက်ခြေမှာ ထားချင်လို့ flex-direction: column ပြောင်းပြီး align-items: flex-end ပေးလိုက်ပါတယ်။ Button က အောက်ကို မရောက်ဘဲ ညာဘက်ကို ပြေးသွားပါတယ်။ ပြီးတော့ justify-content: center ပေးထားတဲ့ စာသားကလည်း ဘယ်ညာ အလယ် မဟုတ်တော့ဘဲ အပေါ်အောက် အလယ်ကို ရောက်သွားပါတယ်။
.card {
display: flex;
flex-direction: column;
justify-content: center; /* "horizontal"? now vertical */
align-items: flex-end; /* "vertical"? now pushes right */
height: 240px;
}ဘာကြောင့် ဒီလို ဖြစ်တာလဲ
justify-content နဲ့ align-items က screen ရဲ့ ဘယ်ညာ/အပေါ်အောက်ကို မသိပါဘူး၊ axis ကိုပဲ သိပါတယ်။ justify-content က main axis ပေါ်မှာ၊ align-items က cross axis ပေါ်မှာ အမြဲ အလုပ်လုပ်ပါတယ်။ flex-direction: column က main axis ကို ဒေါင်လိုက် လှည့်လိုက်လို့ property နှစ်ခုရဲ့ မြင်ရတဲ့ ဦးတည်ချက်လည်း လိုက်လှည့်သွားတာပါ။ Axis နဲ့ စဉ်းစားရင် ဒီ "bug" က ကြိုတင် ခန့်မှန်းလို့ ရတဲ့ အပြုအမူ ဖြစ်သွားပါတယ်။
Core concept: main axis နဲ့ cross axis
Element တစ်ခုကို display: flex ပေးလိုက်တာနဲ့ အဲဒါက flex container ဖြစ်ပြီး သူ့ရဲ့ တိုက်ရိုက် child တိုင်းက flex item ဖြစ်သွားပါတယ်။ Grandchild တွေကတော့ မပါပါဘူး။ Container ထဲမှာ tag မပါဘဲ ရေးထားတဲ့ စာသားကိုလည်း browser က မမြင်ရတဲ့ anonymous flex item တစ်ခုအဖြစ် ထုပ်ပေးပါတယ်။
Flexbox က one-dimensional layout ပါ — item တွေကို axis တစ်ခုတည်း (main axis) တစ်လျှောက် စီပါတယ်။ flex-direction က main axis ကို ရွေးပါတယ်: row (default) ဆိုရင် စာဖတ်တဲ့ ဦးတည်ချက်အတိုင်း (English မှာ ဘယ်မှ ညာ၊ dir="rtl" မှာ ညာမှ ဘယ်)၊ column ဆိုရင် အပေါ်ကနေ အောက်ပါ။ Cross axis က main axis နဲ့ အမြဲ ထောင့်မှန်ကျပါတယ်။
Container property နှစ်ခုကို axis နဲ့ တွဲမှတ်ပါ: justify-content က free space ကို main axis ပေါ်မှာ ဖြန့်ပြီး align-items က item တွေကို cross axis ပေါ်မှာ ညှိပါတယ်။ Direction ပြောင်းရင် property နဲ့ axis ရဲ့ ဆက်စပ်မှု မပြောင်းဘဲ axis ရဲ့ ဦးတည်ချက်ပဲ ပြောင်းပါတယ်။
flex-direction: row vs column
-----------------------------
row (default) column
main axis ----------------> +---------+ main axis
+-------+-------+-------+ cross | item 1 | |
| item1 | item2 | item3 | axis +---------+ |
+-------+-------+-------+ | | item 2 | v
v +---------+
cross axis ---->"Free space" ဆိုတဲ့ စကားလုံးကိုလည်း မှတ်ထားပါ။ Main axis ပေါ်မှာ container ရဲ့ အရွယ်ထဲက item အားလုံးရဲ့ အရွယ်ကို နုတ်လို့ ကျန်တဲ့ နေရာက free space ပါ။ justify-content က ဒီ ကျန်နေတဲ့ နေရာကိုပဲ ဖြန့်တာပါ။ Item တွေက grow ဖြစ်ပြီး နေရာအပြည့် ယူထားရင် (Flex Items lesson မှာ လေ့လာပါမယ်) free space မကျန်တော့လို့ justify-content က မြင်သာတဲ့ အကျိုး မရှိတော့ပါဘူး။ Container ကိုယ်တိုင်ကို စာကြောင်းထဲမှာ ထားချင်ရင်တော့ display: inline-flex ကို သုံးပါ၊ အထဲက axis စည်းမျဉ်းတွေက အတူတူပါပဲ။
Flex item တွေမှာ default အပြုအမူ အသစ်တွေလည်း ရှိပါတယ်။ align-items ရဲ့ default က stretch ဖြစ်လို့ height မပေးထားတဲ့ item တွေက row ရဲ့ အမြင့်အပြည့် ဆန့်ပါတယ်။ Item တွေက နေရာမလောက်ရင် ကျုံ့ပေမယ့် နေရာပိုနေရင် အလိုလို မကြီးပါဘူး။ Item တွေက wrap မဖြစ်ဘဲ တစ်ကြောင်းတည်းမှာ နေပြီး float နဲ့ vertical-align ကို လျစ်လျူရှုပါတယ်၊ margin တွေလည်း collapse မဖြစ်တော့ပါဘူး။
- flex-direction ကနေ main axis ရှာ
- Cross axis = ထောင့်မှန်ကျတဲ့ ဘက်
- justify-content → main axis
- align-items → cross axis
"Horizontal/vertical" ဆိုတဲ့ စကားလုံးကို မေ့ပစ်ပါ၊ "main/cross" ကိုပဲ သုံးပါ။
အထွေထွေ ပုံစံ
container {
display: flex; /* children become flex items */
flex-direction: row | column; /* picks the main axis */
justify-content: value; /* along the main axis */
align-items: value; /* across, on the cross axis */
}display: flex က container ကို ဖန်တီးပြီး flex-direction က main axis ကို ရွေးပါတယ်။ ကျန်တဲ့ property နှစ်ခုက axis တစ်ခုစီကို ပိုင်ပါတယ်။ Value တွေ အသေးစိတ်ကို နောက် lesson (Flex Container) မှာ လေ့လာပါမယ်။
Example: box တစ်ခုကို အလယ်တည့်တည့် ထားခြင်း
Flexbox ရဲ့ အကျော်ကြားဆုံး အသုံးက child တစ်ခုကို container ရဲ့ အလယ်တည့်တည့် ထားတာပါ။ ဒီ property နှစ်ခုကို comment ထဲမှာ "horizontal / vertical" လို့ ရေးလေ့ရှိပေမယ့် အဲဒါက row မှာပဲ မှန်လို့ ဒီမှာ axis နာမည်နဲ့ ရေးထားပါတယ်။ Container ထဲမှာ 100 x 50 child တစ်ခု ရှိတယ်လို့ ယူဆပါ။
.center-box {
display: flex;
justify-content: center; /* main axis (a row by default) */
align-items: center; /* cross axis (perpendicular) */
min-height: 300px;
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Line 2
display: flex;.center-box ကို flex container ဖြစ်စေပြီး child ကို flex item ဖြစ်စေပါတယ်။ flex-direction မပေးထားလို့ main axis က row (ဘယ်မှ ညာ) ပါ။
- Line 3
justify-content: center; /* main axis (a row by default) */Main axis ပေါ်က free space ကို item ရဲ့ ဘယ်ဘက်နဲ့ ညာဘက်မှာ တူတူ ခွဲပေးလို့ item က ဘယ်ညာ အလယ်ကို ရောက်ပါတယ်။
- Line 4
align-items: center; /* cross axis (perpendicular) */Cross axis (ဒီမှာ အပေါ်အောက်) မှာ item ကို အလယ်မှာ ညှိပါတယ်။ ဒီ line မပါရင် default stretch ကြောင့် height မပေးထားတဲ့ child က container အမြင့်အပြည့် ဆန့်သွားမှာပါ။
- Line 5
min-height: 300px;Cross axis ပေါ်မှာ ညှိဖို့ နေရာ ရှိအောင် container ကို အနည်းဆုံး 300px မြင့်စေပါတယ်။ Content က ပိုရှည်လာရင် container က ကြီးလာနိုင်သေးပါတယ်။
Child က box ရဲ့ အလယ်တည့်တည့်မှာ ပေါ်ပါတယ်။ ဥပမာ 600 x 300 box ထဲက 100 x 50 child က box ရဲ့ ဘယ်ဘက်ကနေ 250px၊ အပေါ်ကနေ 125px မှာ ရှိပါတယ်။Flex လား grid လား
Flex က content ကနေ စတွေးပါတယ် — item တွေ ဘယ်လောက် ကြီးလဲ ကြည့်ပြီး လိုင်းတစ်ကြောင်းပေါ်မှာ ဖြန့်ပါတယ်။ Grid က layout ကနေ စတွေးပါတယ် — row နဲ့ column ကို အရင် ဆွဲပြီး item တွေကို အကွက်ထဲ ထည့်ပါတယ်။ Item တွေ ဒေါင်လိုက်ရော အလျားလိုက်ပါ တန်းညီဖို့ လိုရင် grid ကို သုံးပါ။
| Layout | သင့်တော်တဲ့ tool |
|---|---|
| Navbar၊ toolbar၊ button group | Flex |
| Avatar + စာသား (media object) | Flex |
| Header / sidebar / main / footer page frame | Grid |
| Column တွေ တန်းညီရမယ့် card gallery | Grid |
လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
Website တိုင်းနီးပါးရဲ့ header က flex row တစ်ခုပါ — logo ဘယ်ဘက်မှာ၊ menu ညာဘက်မှာ၊ align-items: center နဲ့ အမြင့်မတူတဲ့ logo နဲ့ link တွေကို အလယ်တန်းညီ ထားပါတယ်။ Chat app ရဲ့ message တစ်ခုစီက avatar နဲ့ စာသားပါတဲ့ media object ဖြစ်ပြီး ဒါလည်း flex row ပါ။ Form တွေမှာ label, input, button ကို တစ်တန်းတည်း ထားတာလည်း ဒီ pattern ပါပဲ။
Column direction ကိုလည်း မကြာခဏ တွေ့ရပါတယ် — sidebar navigation မှာ link တွေကို အပေါ်ကနေ အောက် စီပြီး "Log out" ကို အောက်ခြေကို ပို့တာ၊ card တွေမှာ ပုံ၊ စာသား၊ button ကို ဒေါင်လိုက် စီပြီး button ကို အောက်ခြေမှာ ကပ်ထားတာတွေပါ။ Mobile မှာ row ကို column ပြောင်းဖို့ media query ထဲမှာ flex-direction တစ်ကြောင်းတည်း ပြောင်းရုံပဲ လိုလို့ responsive design အတွက်လည်း အဆင်ပြေပါတယ်။ Arabic ဒါမှမဟုတ် Hebrew လို RTL ဘာသာစကားတွေမှာ row က ညာမှ ဘယ်ကို အလိုလို လှည့်သွားတာကလည်း flexbox ရဲ့ အားသာချက်ပါ။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. Column ပြောင်းပြီး property တွေကို မလှည့်တာ
Row မှာ ဘယ်ညာ center အတွက် justify-content ကို သုံးခဲ့ရင် column မှာ အဲဒီ အလုပ်ကို align-items က လုပ်ရပါတယ်။ Direction ပြောင်းတိုင်း property နှစ်ခုကို axis နဲ့ ပြန်စစ်ပါ။
2. Container မှာ အမြင့် မရှိဘဲ column ကို center လုပ်တာ
flex-direction: column; justify-content: center; ပေးပေမယ့် container ရဲ့ height က auto ဖြစ်နေရင် container က content အရွယ်ပဲ ရှိလို့ ဖြန့်စရာ free space မရှိပါဘူး၊ ဒါကြောင့် ဘာမှ ရွေ့မသွားပါဘူး။ height ဒါမှမဟုတ် min-height ပေးပါ။
3. Grandchild တွေကိုပါ flex item ဖြစ်မယ်ထင်တာ
<nav> ကို flex ပေးပြီး link တွေက <ul><li> ထဲမှာ ရှိနေရင် flex item က <ul> တစ်ခုတည်းပဲ ဖြစ်ပါတယ်။ Link တွေကို row စီချင်ရင် <ul> ကို display: flex ပေးပါ။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- Flex layout ကို debug လုပ်တဲ့အခါ DevTools ရဲ့ flex badge ကို နှိပ်ပြီး overlay ဖွင့်ပါ၊ main axis နဲ့ free space ကို မျက်စိနဲ့ မြင်ရပါတယ်။
- CSS comment တွေမှာ "horizontal / vertical" အစား "main axis / cross axis" လို့ ရေးပါ၊ direction ပြောင်းလည်း comment က မှန်နေဆဲ ဖြစ်ပါတယ်။
- Row ကို visual အရ ပြောင်းပြန် လုပ်ချင်ရင် HTML အစဉ်ကို ပြင်တာက keyboard အသုံးပြုသူတွေအတွက် အကောင်းဆုံးပါ။
Mini challenge
Mini challenge: media object နဲ့ sidebar
Row axis နဲ့ column axis နှစ်မျိုးလုံးကို သုံးပြီး layout နှစ်ခု ဆောက်ပါ။
- .media မှာ avatar နဲ့ စာသားကို ဘေးချင်းကပ် ထားပြီး cross axis ပေါ်မှာ အလယ်ညှိရမယ်၊ ကြား 12px
- .sidebar (အမြင့် 300px) ထဲက child တွေကို အပေါ်ကနေ အောက် စီရမယ်
- .logout link က sidebar ရဲ့ အောက်ခြေမှာ ရှိရမယ်၊ margin hack မသုံးရ
- Sidebar child တွေက sidebar အကျယ်အပြည့် ဖြစ်နေရမယ် (default stretch ကို မဖျက်ရ)
<style>
/* TODO: .media = avatar beside text, centred on the cross axis, 12px gap */
.avatar { width: 48px; height: 48px; border-radius: 50%; background: #94a3b8; }
/* TODO: .sidebar = 200px wide, 300px tall, children top to bottom,
with .logout pushed to the bottom */
</style>
<div class="media"><div class="avatar"></div><p>Mya Mya posted a new photo</p></div>
<nav class="sidebar">
<div class="links"><a href="#">Home</a> <a href="#">Inbox</a></div>
<a class="logout" href="#">Log out</a>
</nav>ရလဒ်: Avatar ရဲ့ ညာဘက်မှာ စာသား ရှိပြီး နှစ်ခုရဲ့ အလယ်မျဉ်း တူပါတယ်။ Sidebar ထဲမှာ Home/Inbox က အပေါ်ဆုံး၊ "Log out" က အောက်ခြေမှာ ရှိပြီး နှစ်ခုလုံး sidebar အကျယ်အပြည့် ဖြစ်ပါတယ်။
အရိပ်အမြွက် ကြည့်ရန်
Sidebar ရဲ့ main axis က column ဖြစ်လို့ child နှစ်ခုကို အစနဲ့ အဆုံး ခွဲထုတ်ဖို့ justify-content ကို သုံးပါ (space-between)။
အဖြေ ကြည့်ရန်
<style>
.media {
display: flex;
align-items: center;
gap: 12px;
}
.avatar { width: 48px; height: 48px; border-radius: 50%; background: #94a3b8; }
.sidebar {
display: flex;
flex-direction: column;
justify-content: space-between;
width: 200px;
height: 300px;
background: #f1f5f9;
}
</style>
<div class="media"><div class="avatar"></div><p>Mya Mya posted a new photo</p></div>
<nav class="sidebar">
<div class="links"><a href="#">Home</a> <a href="#">Inbox</a></div>
<a class="logout" href="#">Log out</a>
</nav>Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- display: flex က တိုက်ရိုက် child တွေကိုပဲ flex item ဖြစ်စေပါတယ်။
- flex-direction က main axis ကို ရွေးပြီး cross axis က အဲဒါနဲ့ ထောင့်မှန်ကျပါတယ်။
- justify-content က main axis၊ align-items က cross axis — direction ဘယ်လိုပဲ ပြောင်းပြောင်း ဒီဆက်စပ်မှု မပြောင်းပါဘူး။
- Axis တစ်ခုတည်းပေါ်က layout တွေအတွက် flex၊ row ရော column ပါ တန်းညီရမယ့် layout တွေအတွက် grid ကို ရွေးပါ။
Basic concepts of Flexbox — MDN Web Docs