ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Element တစ်ခုရဲ့ background ကို နံရံတစ်ချပ်လို့ မြင်ကြည့်ပါ။ အရင်ဆုံး နံရံကို ဆေးတစ်ရောင် သုတ်ပါတယ် (background-color)။ ပြီးမှ ဓာတ်ပုံ ဒါမှမဟုတ် အရောင်ရောထားတဲ့ gradient ကို အပေါ်ကနေ ကပ်ပါတယ် (background-image)။ ဓာတ်ပုံကို ဘယ်လောက်အရွယ်နဲ့ ကပ်မလဲ၊ ဘယ်နေရာ ချိန်မလဲ၊ ထပ်ခါထပ်ခါ ကပ်မလားဆိုတာကိုတော့ background-size, background-position, background-repeat တွေက ဆုံးဖြတ်ပါတယ်။
အရေးကြီးတာက background ဟာ အလှဆင်တာ သက်သက်ပဲ ဖြစ်တာပါ။ Screen reader က background image ကို မဖတ်ပါဘူး၊ image မတက်လာရင်လည်း အောက်က ဆေးရောင်ပဲ မြင်ရမှာပါ။ ဒါကြောင့် စာတွေ ဖတ်လို့ရနေဖို့ ဆေးရောင်ကို အမြဲ ရွေးထားရပါမယ်။
Problem → Why → Solution
ပြဿနာ: ဓာတ်ပုံပေါ်က စာ ပျောက်သွားတာ
အောက်က hero က designer ရဲ့ laptop ပေါ်မှာ လှပါတယ်၊ ဓာတ်ပုံက မှောင်ပြီး စာဖြူက ပေါ်လွင်နေလို့ပါ။ ဒါပေမယ့် network နှေးလို့ ဓာတ်ပုံ မတက်ခင် စက္ကန့်အနည်းငယ်အတွင်း page က ဖြူဖြူကြီးဖြစ်ပြီး စာဖြူက လုံးဝ မမြင်ရပါဘူး။ ဓာတ်ပုံကို ပိုလင်းတဲ့ ပုံနဲ့ လဲလိုက်တဲ့နေ့မှာလည်း ကောင်းကင်ဖြူဖြူပေါ်က ခေါင်းစဉ်ကို ဘယ်သူမှ မဖတ်နိုင်တော့ပါဘူး။ ဖုန်း screen ကျဉ်းကျဉ်းမှာတော့ cover က ပုံရဲ့ ဘေးနှစ်ဖက်ကို ဖြတ်ပစ်လို့ team photo ထဲက လူတွေရဲ့ မျက်နှာ တစ်ဝက် ပျောက်သွားပါတယ်။
.hero {
color: white;
/* no fallback color, no overlay, crop never checked */
background: url("team-photo.jpg") center / cover;
}ဘာကြောင့် ဒီလို ဖြစ်တာလဲ
Background image က download ပြီးမှ ပေါ်လာတာဖြစ်ပြီး ပုံထဲက အလင်းအမှောင်ကိုလည်း CSS က မသိပါဘူး။ background shorthand မှာ color မရေးထားတော့ background-color က transparent ဖြစ်သွားပြီး အောက်က page ရဲ့ အဖြူရောင် ပေါက်လာပါတယ်။ Fallback color တစ်ခု၊ စာနဲ့ ပုံကြားမှာ မှောင်တဲ့ overlay layer တစ်ခု ထည့်ပြီး screen အကျယ် အမျိုးမျိုးမှာ crop ကို စစ်ကြည့်ရပါမယ်။
Core concept: background က layer တွေ ထပ်ထားတာပါ
Browser က element တစ်ခုရဲ့ background ကို အောက်ကနေ အပေါ် layer လိုက် ဆွဲပါတယ်။ အောက်ဆုံးမှာ background-color တစ်ခုတည်း ရှိပြီး border ရဲ့ အောက်ထိ ဖုံးပါတယ်။ အဲဒီအပေါ်မှာ background-image မှာ ရေးထားတဲ့ image တွေကို နောက်ဆုံးက စပြီး ဆွဲတာမို့ ပထမဆုံး ရေးထားတဲ့ image က user နဲ့ အနီးဆုံး (အပေါ်ဆုံး) layer ဖြစ်ပါတယ်။ background-image: linear-gradient(...), url("photo.jpg") ဆိုရင် gradient က ဓာတ်ပုံရဲ့ အပေါ်မှာ ရှိပါတယ်။
Gradient (linear-gradient, radial-gradient) တွေက browser ကိုယ်တိုင် ဆွဲပေးတဲ့ image တွေပါ၊ ဒါကြောင့် background-color မှာ မထည့်ရဘဲ background-image မှာ ထည့်ရပါတယ်။ File download မလိုလို့ ချက်ချင်း ပေါ်ပြီး ဘယ် size နဲ့မဆို ချောချောမွေ့မွေ့ ဆွဲပေးပါတယ်။ linear-gradient(135deg, A, B) ဆိုရင် အရောင် A က ဘယ်ဘက်အပေါ်ထောင့်နားက စပြီး ညာဘက်အောက်ထောင့်ဘက်ကို B အဖြစ် ပြောင်းသွားပါတယ်။
Image layer တစ်ခုချင်းစီကို property သုံးခုက ထိန်းပါတယ်။ background-repeat ရဲ့ default က repeat ဖြစ်လို့ ပုံသေးရင် tile လို ထပ်ခါထပ်ခါ ပေါ်ပါတယ်။ background-position ရဲ့ default က ဘယ်ဘက်အပေါ်ထောင့်ပါ။ background-size: cover က ပုံရဲ့ အချိုးအစားကို မပျက်စေဘဲ box တစ်ခုလုံး ဖုံးတဲ့အထိ ချဲ့တာမို့ box နဲ့ ပုံ အချိုး မတူရင် အစွန်းတွေ ဖြတ်ခံရပါတယ်။ contain ကတော့ ပုံတစ်ခုလုံး ဝင်အောင် ချုံ့တာမို့ ဘာမှ မဖြတ်ပေမယ့် ဘေးမှာ နေရာလွတ် ကျန်နိုင်ပါတယ်။ background-position က cover ဖြတ်တဲ့အခါ ဘယ်အပိုင်းကို ထားမလဲ ဆုံးဖြတ်ပေးပါတယ်။
နောက်ဆုံး background ဟာ content မဟုတ်ပါဘူး။ alt text မရှိ၊ screen reader က မဖတ်၊ print ထုတ်ရင်လည်း browser အများစုက ဖြုတ်ပစ်ပါတယ်။ အဓိပ္ပာယ်ရှိတဲ့ ပုံ (product photo, chart) ဆိုရင် <img> နဲ့ alt ကို သုံးပါ။
| Property | ဘာကို ထိန်းလဲ | Default |
|---|---|---|
| background-color | Layer အားလုံးရဲ့ အောက်က ဆေးရောင် | transparent |
| background-image | Layer စာရင်း (ပထမ = အပေါ်ဆုံး) | none |
| background-size | Layer ရဲ့ အရွယ် (cover, contain, 200px auto) | auto |
| background-position | Layer ကို ဘယ်နေရာ ချိန်မလဲ | 0% 0% (ဘယ်အပေါ်) |
| background-repeat | Tile လို ထပ်ကပ်မလား | repeat |
- background-color ကို အောက်ဆုံး သုတ်
- နောက်ဆုံး image layer ကို ဆွဲ
- ရှေ့ layer တွေကို အပေါ်က ထပ်ဆွဲ
- ပထမ layer အပေါ်မှာ စာ ပေါ်
Image တစ်ခု မတက်လာရင် အဲဒီ layer ပဲ ပျောက်ပါတယ် — အောက်က color နဲ့ အပေါ်က overlay က ကျန်နေပါတယ်။
အထွေထွေ ပုံစံ
selector {
background-color: fallback-color;
background-image: top-layer, bottom-layer;
background-repeat: repeat | no-repeat;
background-position: x y;
background-size: cover | contain | width height;
}background-color က fallback ဖြစ်ပြီး background-image မှာ layer တွေကို comma နဲ့ ခွဲရေးပါတယ်။ repeat, position, size မှာလည်း comma နဲ့ ခွဲပြီး layer တစ်ခုချင်းစီအတွက် တန်ဖိုး ပေးလို့ ရပါတယ်၊ တစ်ခုတည်း ရေးရင် layer အားလုံးကို သက်ရောက်ပါတယ်။
Example: gradient hero section
ဒီ example က landing page ထိပ်က hero section ကို ဓာတ်ပုံ မသုံးဘဲ gradient တစ်ခုတည်းနဲ့ ဆွဲထားပါတယ်။ background-color ကိုလည်း gradient ရဲ့ ပထမအရောင်နဲ့ တူအောင် ထားထားလို့ gradient မပေါ်တဲ့ အခြေအနေမှာတောင် ပုံစံ မပြောင်းသလောက် ဖြစ်ပါတယ်။ repeat, position, size သုံးကြောင်းက နောက်ပိုင်း gradient နေရာမှာ ဓာတ်ပုံ ထည့်တဲ့နေ့အတွက် ကြိုပြင်ထားတာပါ။
.hero {
min-height: 420px;
background-color: #101827;
background-image: linear-gradient(135deg, #101827, #123b52);
background-repeat: no-repeat;
background-position: center;
background-size: cover;
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Line 2
min-height: 420px;Background က element ရဲ့ box ထဲမှာပဲ ဆွဲတာပါ။ Content မရှိတဲ့ div ရဲ့ height က 0 ဖြစ်လို့ min-height မရှိရင် background ကို ဘာမှ မမြင်ရပါဘူး။
- Line 3
background-color: #101827;အောက်ဆုံး layer ပါ။ Gradient ရဲ့ ပထမအရောင်နဲ့ တူလို့ image layer မဆွဲနိုင်ရင်တောင် စာဖြူ ဖတ်လို့ ရနေပါတယ်။
- Line 4
background-image: linear-gradient(135deg, #101827, #123b52);Browser က ဆွဲပေးတဲ့ image ပါ။ 135deg ကြောင့် ဘယ်အပေါ်က navy မှောင်မှောင်ကနေ ညာအောက်က blue-teal ဘက်ကို ပြောင်းသွားပါတယ်။
- Lines 5-7
background-size: cover;Gradient မှာ ကိုယ်ပိုင် အရွယ် မရှိလို့ box အပြည့် ဆွဲပြီးသားပါ၊ ဒါကြောင့် ဒီ သုံးကြောင်းက အခု မမြင်ရတဲ့ ကွာခြားချက်ပါ။ url() ဓာတ်ပုံ ထည့်လိုက်တာနဲ့ tile မဖြစ်အောင်၊ အလယ်ကို ချိန်ပြီး box အပြည့် ဖုံးအောင် အလုပ်လုပ်ပါမယ်။
420px မြင့်တဲ့ hero တစ်ခုလုံးကို ဘယ်အပေါ်ထောင့်က navy မှောင်မှောင် (#101827) ကနေ ညာအောက်ထောင့်က blue-teal (#123b52) ဆီ ပြောင်းသွားတဲ့ ထောင့်ဖြတ် gradient က ဖုံးထားပါတယ်။ Tile အဆက်ကြောင်း တစ်ခုမှ မပေါ်ပါဘူး။Overlay layer နဲ့ shorthand
ဓာတ်ပုံပေါ်က စာကို ဖတ်ရလွယ်အောင် လုပ်တဲ့ အသုံးအများဆုံးနည်းက ဓာတ်ပုံရဲ့ အပေါ်မှာ ဖောက်ထွင်းမြင်ရတဲ့ အမှောင် layer တစ်ခု ထပ်ထည့်တာပါ။ linear-gradient မှာ အရောင်တူ နှစ်ခု ပေးရင် တစ်ရောင်တည်း layer ဖြစ်ပါတယ်။ ပုံထဲက ဘယ်နေရာ လင်းလင်း စာနဲ့ contrast ရနေပါမယ်။
.hero {
background-color: #1e293b; /* fallback */
background-image:
linear-gradient(rgb(0 0 0 / 0.5), rgb(0 0 0 / 0.5)), /* overlay, on top */
url("hero.webp"); /* photo, below */
background-size: cover;
background-position: center top;
}
/* The same thing as one shorthand: the color goes in the LAST layer only */
.hero-short {
background:
linear-gradient(rgb(0 0 0 / 0.5), rgb(0 0 0 / 0.5)),
url("hero.webp") center top / cover no-repeat,
#1e293b;
}background shorthand ကို သုံးရင် သတိထားစရာ နှစ်ခု ရှိပါတယ်။ Position နဲ့ size ကို center top / cover လို / နဲ့ ခွဲရေးရပြီး color ကို နောက်ဆုံး layer ထဲမှာပဲ ထည့်ခွင့်ရှိပါတယ်။ ပိုအရေးကြီးတာက shorthand က မရေးထားတဲ့ sub-property အားလုံးကို default ပြန်ထားတာပါ။ ဒါကြောင့် shorthand ကို အရင်ရေး၊ ပြင်ချင်တဲ့ property တစ်ခုချင်းကို နောက်မှ longhand နဲ့ ရေးပါ။
လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
Marketing site တွေရဲ့ hero banner, blog post ရဲ့ cover, login page နောက်ခံ စတာတွေမှာ ဓာတ်ပုံ + overlay + fallback color ပုံစံကို အမြဲတွေ့ရပါတယ်။ Dashboard နဲ့ card UI တွေမှာတော့ ဓာတ်ပုံထက် background-color အပါးလေးတွေနဲ့ section တွေကို ခွဲပြတာ ပိုများပြီး gradient ကို button နဲ့ badge အသေးလေးတွေမှာ သုံးကြပါတယ်။ Design system တွေက ဒီ အရောင်တွေကို --surface, --surface-muted လို CSS variable တွေအဖြစ် သတ်မှတ်ထားလေ့ ရှိပါတယ်။
Performance ဘက်ကလည်း စဉ်းစားရပါတယ်။ CSS background image ကို browser က CSS file ကို parse လုပ်ပြီး element နဲ့ ကိုက်တာ သိမှ download စလုပ်တာမို့ HTML ထဲက <img> ထက် နောက်ကျပြီး ပေါ်လေ့ ရှိပါတယ်။ Page ရဲ့ အကြီးဆုံး ပုံဖြစ်တဲ့ hero image အတွက် WebP လို ချုံ့ထားတဲ့ format သုံးပြီး file size ကို သေးအောင် ထားပါ၊ အရေးကြီးရင် <img> ဒါမှမဟုတ် preload ကိုလည်း စဉ်းစားပါ။ Release မလုပ်ခင် DevTools မှာ image ကို block လုပ်ပြီး fallback color ပေါ်မှာ စာ ဖတ်လို့ ရလား စစ်တာကလည်း ငါးမိနစ်ပဲ ကြာတဲ့ စစ်ဆေးမှုပါ။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. Shorthand က fallback color ကို ဖျက်ပစ်တာ
.card { background-color: #fef3c7; } ရေးပြီး နောက်က .card.promo { background: url(stars.svg) no-repeat; } ရေးရင် promo card ရဲ့ background-color က transparent ဖြစ်သွားပါတယ်။ Shorthand က color ကို ဖော်မပြတော့ default ပြန်ထားလိုက်လို့ပါ။ background-image ကို longhand နဲ့ ရေးပါ၊ ဒါမှမဟုတ် shorthand ထဲမှာ color ကို ထပ်ထည့်ပါ။
2. Content ကို background image ထဲ ထည့်တာ
"50% off" လို စာကို ဓာတ်ပုံထဲ ရိုက်ထည့်ပြီး background အဖြစ် သုံးရင် screen reader က မဖတ်ပါဘူး၊ ဘာသာပြန်လို့ မရ၊ zoom လုပ်ရင် ဝါးပြီး copy လည်း မရပါဘူး။ စာကို HTML text အဖြစ် ထားပါ၊ အဓိပ္ပာယ်ရှိတဲ့ ပုံဆိုရင် <img alt="..."> ကို သုံးပါ။
3. cover ရဲ့ crop ကို မစစ်တာ
Desktop မှာ ကျယ်ပြန့်တဲ့ banner ဖြစ်ပေမယ့် ဖုန်းမှာ box က ထောင်လိုက် ဖြစ်သွားလို့ cover က ပုံရဲ့ ဘေးနှစ်ဖက်ကို ဖြတ်ပစ်ပါတယ်။ အရေးကြီးတဲ့ အပိုင်း (မျက်နှာ, product) ဘယ်နားမှာ ရှိလဲ ကြည့်ပြီး background-position: center top လို တန်ဖိုးနဲ့ အဲဒီ အပိုင်းကို ချိန်ထားပါ၊ ပြီးရင် 360px အကျယ်မှာ စစ်ကြည့်ပါ။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- Background image သုံးတိုင်း စာနဲ့ contrast ကောင်းတဲ့ background-color ကို fallback အဖြစ် ထည့်ပါ။
- ဓာတ်ပုံပေါ်မှာ စာထားရင် overlay layer ထည့်ပြီး ပုံ အလင်းဆုံးနေရာမှာ contrast ကို စစ်ပါ။
- အလှဆင်တဲ့ ပုံအတွက်ပဲ CSS background သုံးပါ၊ အဓိပ္ပာယ်ရှိတဲ့ ပုံအတွက် <img alt> သုံးပါ။
Mini challenge
Mini challenge: overlay ပါတဲ့ banner
ခရီးသွား website တစ်ခုအတွက် ဓာတ်ပုံ banner ဆောက်ပါ။ ဓာတ်ပုံ file မရှိသေးတဲ့ အခုအချိန်မှာလည်း ခေါင်းစဉ်ဖြူကို ရှင်းရှင်း ဖတ်လို့ရနေရပါမယ်။
- စာဖြူ ဖတ်လို့ရတဲ့ မှောင်တဲ့ fallback background-color (#1e293b) ထည့်ရမယ်
- background-image မှာ layer နှစ်ခု — အပေါ်က 55% အမည်း overlay, အောက်က url("mountains.webp")
- ပုံက tile မဖြစ်ဘဲ banner အပြည့် ဖုံးပြီး အလယ်ကို ချိန်ရမယ်
- background shorthand မသုံးဘဲ longhand တွေနဲ့ပဲ ရေးရမယ်
<!doctype html>
<html lang="en">
<style>
.banner {
/* TODO: generous padding and centered white text */
/* TODO: a dark fallback color for when the photo is missing */
/* TODO: two layers - a translucent black overlay FIRST,
then url("mountains.webp") */
/* TODO: no tiling, centered, covering the whole banner */
}
</style>
<section class="banner">
<h1>Trekking in Shan State</h1>
<p>Three-day routes from Kalaw to Inle Lake</p>
</section>
</html>ရလဒ်: mountains.webp မရှိသေးတော့ dark slate (#1e293b) ကို overlay က ပိုမှောင်စေထားတဲ့ banner ပေါ်မှာ အလယ်ညှိ ခေါင်းစဉ်ဖြူနဲ့ စာကြောင်းတစ်ကြောင်း ပေါ်ပါတယ်။ ဓာတ်ပုံ ထည့်လိုက်ရင် ပုံက 55% မှိန်သွားပြီး banner အပြည့် ဖုံးပါမယ်၊ စာက ဆက်ပြီး ဖတ်လို့ ရနေပါမယ်။
အရိပ်အမြွက် ကြည့်ရန်
အရောင်တစ်ရောင်တည်း layer လိုချင်ရင် linear-gradient(rgb(0 0 0 / 0.55), rgb(0 0 0 / 0.55)) လို့ အရောင်တူ နှစ်ခု ပေးပါ။ Layer စာရင်းမှာ ပထမ ရေးတာက အပေါ်ဆုံးမှာ ပေါ်ပါတယ်။
အဖြေ ကြည့်ရန်
<!doctype html>
<html lang="en">
<style>
.banner {
padding: 80px 24px;
text-align: center;
color: #ffffff;
background-color: #1e293b;
background-image:
linear-gradient(rgb(0 0 0 / 0.55), rgb(0 0 0 / 0.55)),
url("mountains.webp");
background-repeat: no-repeat;
background-position: center;
background-size: cover;
}
</style>
<section class="banner">
<h1>Trekking in Shan State</h1>
<p>Three-day routes from Kalaw to Inle Lake</p>
</section>
</html>Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Background ကို အောက်ကနေ ဆွဲပါတယ် — color က အောက်ဆုံး၊ background-image ထဲမှာ ပထမ ရေးတဲ့ layer က အပေါ်ဆုံးပါ။
- cover က box ကို အပြည့် ဖုံးပြီး အစွန်းတွေ ဖြတ်ပါတယ်၊ contain က ပုံအကုန် ပြပြီး နေရာလွတ် ကျန်နိုင်ပါတယ်၊ background-position က ဖြတ်မယ့်နေရာကို ထိန်းပါတယ်။
- ဓာတ်ပုံပေါ်က စာအတွက် fallback color နဲ့ overlay layer အမြဲ ထည့်ပါ။
- background shorthand က မရေးထားတဲ့ sub-property တွေကို reset လုပ်တာမို့ longhand နဲ့ override လုပ်ပါ။
CSS reference — MDN Web Docs