ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
CSS ကို page တစ်ခုထဲ ထည့်ဖို့ နေရာ သုံးနေရာ ရှိပါတယ်။ External stylesheet က .css file သီးခြားတစ်ခုဖြစ်ပြီး <link> နဲ့ page အများကြီးက မျှသုံးပါတယ်။ Internal CSS က page တစ်ခုတည်းရဲ့ <head> ထဲက <style> block ဖြစ်ပါတယ်။ Inline style ကတော့ element တစ်ခုတည်းရဲ့ style attribute ထဲမှာ တိုက်ရိုက် ရေးတာပါ။
အိမ်ထောင်စု စည်းကမ်းနဲ့ နှိုင်းရရင် external stylesheet က အိမ်သားအားလုံး လိုက်နာရတဲ့ စည်းကမ်းစာရွက်၊ internal က အခန်းတစ်ခန်းရဲ့ တံခါးမှာ ကပ်ထားတဲ့ မှတ်စု၊ inline ကတော့ လူတစ်ယောက်တည်းကို တိုက်ရိုက် ပြောလိုက်တဲ့ ချွင်းချက်ပါ။
Problem → Why → Solution
ပြဿနာ: stylesheet ကို ပြင်ပေမယ့် စျေးနှုန်းအရောင် မပြောင်းဘူး
ပထမ version မှာ developer တစ်ယောက်က product card ၃၀ ရဲ့ စျေးနှုန်းကို အမြန်ဆုံး အနီရောင် ဖြစ်စေဖို့ style="color: red;" ကို element တိုင်းမှာ ထည့်ခဲ့ပါတယ်။ လများကြာတော့ design အသစ်က စျေးနှုန်းကို အစိမ်းရောင် လိုချင်လို့ shop.css ထဲမှာ .price { color: green; } ကို ထည့်ပါတယ်။ ဘာမှ မပြောင်းပါဘူး။ နောက် developer က !important ထည့်ပြီး ဖြေရှင်းလိုက်ပြီး အဲဒီကနေ !important ပြိုင်ပွဲ စပါတော့တယ်။
<link rel="stylesheet" href="shop.css"> <!-- contains .price { color: green; } -->
<p class="price" style="color: red;">$19</p> <!-- still red -->
<p class="price" style="color: red;">$24</p> <!-- still red -->ဘာကြောင့် ဒီလို ဖြစ်တာလဲ
style attribute ထဲက declaration တွေက stylesheet ထဲက selector နဲ့ ရေးထားတဲ့ rule ဘယ်ဟာကိုမဆို (!important မဟုတ်ရင်) အနိုင်ရပါတယ်၊ selector ဘယ်လောက် တိကျတိကျ။ ဒါကြောင့် inline style တွေ များလာလေ stylesheet က ထိန်းချုပ်မှု ဆုံးရှုံးလေ ဖြစ်ပြီး ပြင်ချင်ရင် HTML element တစ်ခုချင်းကို ပြန်လိုက်ပြင်ရပါတယ်။
Core concept: နေရာ သုံးနေရာ၊ cascade တစ်ခုတည်း
| နည်း | ဘယ်မှာ ရေးလဲ | ဘာအတွက် သင့်တော်လဲ |
|---|---|---|
| External | <head> ထဲက <link> နဲ့ ချိတ်ထားတဲ့ .css file | Real site တွေ၊ page အများကြီး မျှသုံးတဲ့ style |
| Internal | <head> ထဲက <style> block | Page တစ်ခုတည်း စမ်းတာ၊ demo၊ email template |
| Inline | Element ရဲ့ style="" attribute | JavaScript က တွက်ထုတ်တဲ့ dynamic value တစ်ခု |
နည်း သုံးမျိုးလုံးက author (ကိုယ်ရေးတဲ့) style တွေ ဖြစ်ပြီး cascade တစ်ခုတည်းထဲ ဝင်ပါတယ်။ External နဲ့ internal ကြားမှာ ပင်ကိုယ် အဆင့်မြင့်တဲ့ တစ်ခု မရှိပါဘူး — "internal က external ကို အမြဲ အနိုင်ရတယ်" ဆိုတာ အယူအဆ မှားပါ။ Selector specificity တူရင် HTML ထဲမှာ နောက်ကျပြီး ပေါ်တဲ့ rule က အနိုင်ရပါတယ်၊ ဒါကြောင့် <link> ကို <style> ရဲ့ နောက်မှာ ထားရင် external file က အနိုင်ရပါတယ်။ Inline style ကတော့ သီးခြားပါ: selector မရှိဘဲ element ကို တိုက်ရိုက် ပစ်မှတ်ထားလို့ selector-based rule အားလုံးကို အနိုင်ရပါတယ်။
External file တွေမှာ performance အကျိုးလည်း ရှိပါတယ်။ Browser က .css file ကို တစ်ခါ download လုပ်ပြီး cache ထဲ သိမ်းလို့ နောက် page တွေက ပြန်မ download ရပါဘူး။ Internal နဲ့ inline CSS ကတော့ HTML page တိုင်းနဲ့ အတူ ထပ်ခါထပ်ခါ ပို့ရပါတယ်။ Browser က <head> ထဲက stylesheet တွေ ရောက်မလာမချင်း page ကို မဆွဲဘဲ စောင့်လို့ (render-blocking) style မပါတဲ့ content ခဏ ပေါ်ပြီး ပျောက်သွားတာမျိုး မဖြစ်ပါဘူး — ဒါကြောင့် <link> ကို <head> ထဲမှာ ထားရပါတယ်။
Inline style မှာ ကန့်သတ်ချက်တွေလည်း ရှိပါတယ်: :hover လို state တွေ၊ media query နဲ့ ::before လို pseudo-element တွေကို style attribute ထဲမှာ ရေးလို့ မရပါဘူး။ ဒါကြောင့် inline ကို ချွင်းချက် တစ်ခုအဖြစ်ပဲ သုံးပါ။
Stylesheet တစ်ခုထဲကနေ နောက်တစ်ခုကို @import url("forms.css"); နဲ့လည်း ဆွဲထည့်လို့ ရပြီး အဲဒီ line က file ရဲ့ ထိပ်ဆုံးမှာ ရှိရပါတယ်။ ဒါပေမယ့် browser က ပထမ file ကို download လုပ်ပြီး ဖတ်ပြီးမှ @import ကို တွေ့လို့ ဒုတိယ file ကို နောက်မှ စ download လုပ်ပါတယ်။ ဒီလို တစ်ခုပြီးမှ တစ်ခု ဖြစ်တဲ့ download ကွင်းဆက်က page ကို နှေးစေလို့ file အများကြီးကို HTML ထဲမှာ <link> တွေ ယှဉ်ပြီး ထည့်တာ ဒါမှမဟုတ် build tool နဲ့ ပေါင်းတာက ပိုကောင်းပါတယ်။
- HTML ကို အပေါ်ကနေ အောက် ဖတ်တယ်
- <link> နဲ့ <style> rule တွေ စုတယ်
- Specificity တူရင် နောက်ကျတာ အနိုင်
- Inline style က selector rule ကို အနိုင်
External နဲ့ internal ကြားမှာ နေရာ (source order) က ဆုံးဖြတ်ပါတယ်၊ file အမျိုးအစား က မဆုံးဖြတ်ပါဘူး။
အထွေထွေ ပုံစံ: နည်း သုံးမျိုး
<head>
<link rel="stylesheet" href="path/to/file.css"> <!-- external -->
<style>
selector { property: value; } /* internal */
</style>
</head>
<body>
<element style="property: value;">...</element> <!-- inline -->
</body><link> နဲ့ <style> နှစ်ခုလုံး <head> ထဲမှာ ရှိရပါတယ်။ href က HTML file ကနေ CSS file ဆီ relative path ဖြစ်ပြီး style attribute ထဲမှာ selector နဲ့ brace မပါဘဲ declaration တွေကိုပဲ ရေးပါတယ်။
Example: page တစ်ခုထဲမှာ နည်း သုံးမျိုးလုံး
Folder တစ်ခုထဲမှာ file နှစ်ခု ဖန်တီးပါ။ ပထမ file က styles.css ဖြစ်ပြီး ဒီ rule တစ်ခုတည်း ပါပါတယ်:
body {
font-family: system-ui, sans-serif;
margin: 2rem;
}ဒုတိယ file က အောက်က index.html ပါ။ ဖွင့်ပြီးတဲ့အခါ button နှစ်ခုကို Inspect လုပ်ပြီး Styles pane မှာ inline style ကို "element.style" ဆိုတဲ့ ခေါင်းစဉ်အောက်မှာ ထိပ်ဆုံးမှာ ပြတာနဲ့ .btn ရဲ့ background-color က မျဉ်းဖြတ်ခံရတာကို ကြည့်ပါ။
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Three ways to add CSS</title>
<!-- 1. external: one file shared by every page that links it -->
<link rel="stylesheet" href="styles.css">
<!-- 2. internal: rules for this page only -->
<style>
.btn {
padding: 8px 16px;
border: none;
color: white;
background-color: #1d4ed8;
}
</style>
</head>
<body>
<h1>Account</h1>
<button class="btn">Save</button>
<!-- 3. inline: this one element only -->
<button class="btn" style="background-color: #b91c1c;">Delete</button>
</body>
</html>Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- External stylesheet
<link rel="stylesheet" href="styles.css">rel="stylesheet" က browser ကို ဒီ file ဟာ CSS ဖြစ်ကြောင်း ပြောပြီး href="styles.css" က HTML file နဲ့ folder တူတဲ့ file ကို ညွှန်ပါတယ်။ File နာမည် မှားရင် error မပြဘဲ style မရှိဘဲ ပေါ်လို့ DevTools ရဲ့ Network tab မှာ 404 ကို စစ်ပါ။
- Internal block
<style>ဒီ block ထဲက .btn rule က index.html မှာပဲ အကျုံးဝင်ပါတယ်။ တခြား page တစ်ခုက button တွေကို ဒီလို ပုံပေါ်စေချင်ရင် ဒီ rule ကို styles.css ထဲ ရွှေ့ရပါမယ်။
- Class rule for both buttons
background-color: #1d4ed8;Button နှစ်ခုလုံးမှာ class="btn" ပါလို့ ဒီ declaration နှစ်ခုလုံးကို ရွေးပါတယ်။ "Save" မှာ ယှဉ်ပြိုင်သူ မရှိလို့ အပြာ ဖြစ်ပါတယ်။
- Inline override
<button class="btn" style="background-color: #b91c1c;">Delete</button>style attribute ထဲက background-color က .btn ရဲ့ အပြာကို အနိုင်ရပါတယ်။ padding နဲ့ color ကိုတော့ inline မှာ မရေးထားလို့ .btn ကနေ ဆက်ရပါတယ် — inline style က သူရေးထားတဲ့ property တွေကိုပဲ အစားထိုးပါတယ်။
"Account" heading က styles.css ကနေ sans-serif font ကို ရပြီး page ပတ်လည်မှာ 2rem (32px) margin ရှိပါတယ်။ Button နှစ်ခုလုံး padding နဲ့ အဖြူစာ ရပါတယ်။ "Save" က အပြာ (#1d4ed8)၊ "Delete" ကတော့ inline style က .btn ကို အနိုင်ရလို့ အနီရင့် (#b91c1c) ဖြစ်ပါတယ်။CSS file ထဲက path တွေ
Project ကြီးလာရင် CSS ကို css/ folder ထဲ ထည့်လေ့ ရှိပါတယ်။ HTML ထဲက href က HTML file ကနေ စတွက်ပေမယ့် CSS file ထဲက url() ကတော့ CSS file ကနေ စတွက်ပါတယ်။ ဒါကြောင့် css/site.css ထဲမှာ url(img/bg.png) လို့ ရေးရင် browser က css/img/bg.png ကို ရှာပါတယ် — project root က img/ ကို ညွှန်ချင်ရင် ../img/bg.png လို့ ရေးရပါမယ်။ ပုံ path မှားတာလည်း error မပြဘဲ ပျက်လို့ Network tab ထဲက အနီရောင် 404 line ကို ရှာပါ။
Where each path is resolved from
--------------------------------
project/
index.html <link href="css/site.css"> (from index.html)
css/
site.css url(../img/bg.png) (from site.css)
img/
bg.pngလက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
Production site အများစုမှာ build tool တစ်ခု (Vite၊ Next.js စသဖြင့်) က CSS file အများကြီးကို ပေါင်းပြီး minify လုပ်ကာ app-3f9a1c.css လို နာမည်မှာ content hash ပါတဲ့ external file တစ်ခု ဒါမှမဟုတ် နည်းနည်းကို ထုတ်ပေးပါတယ်။ File အကြောင်းအရာ ပြောင်းရင် နာမည် ပြောင်းလို့ browser တွေက cache ကို ကြာကြာ သိမ်းထားနိုင်ပြီး update တွေကိုလည်း ချက်ချင်း ရပါတယ်။
Internal နဲ့ inline CSS တို့လည်း သူ့နေရာနဲ့သူ ရှိပါတယ်။ HTML email တွေမှာ email client အများစုက external stylesheet ကို မဖတ်လို့ inline style ကို အဓိက သုံးရပါတယ်။ JavaScript က progress bar ရဲ့ width (style.width = "40%") လို run နေစဉ် တွက်ထုတ်တဲ့ value တွေကို inline အဖြစ် ထည့်ပါတယ်။ Security ကြပ်တဲ့ site တွေမှာ Content-Security-Policy header က inline style တွေကို ပိတ်ထားနိုင်လို့ external file က အမြဲ အလုံခြုံဆုံး ရွေးချယ်မှု ဖြစ်ပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. href path မှားတာ
CSS file က css/ folder ထဲမှာ ရှိပေမယ့် href="styles.css" လို့ ရေးတာ၊ Styles.css နဲ့ styles.css လို စာလုံးအကြီးအသေး ကွဲတာ (Linux server တွေမှာ case-sensitive ဖြစ်ပါတယ်) က အဖြစ်အများဆုံးပါ။ Page က လုံးဝ style မပါဘဲ ပေါ်ရင် Network tab မှာ CSS file ရဲ့ status ကို စစ်ပါ။
2. <style> tag ထဲမှာ HTML ရေးတာ ဒါမှမဟုတ် .css file ထဲမှာ <style> ထည့်တာ
.css file ထဲမှာ CSS rule တွေပဲ ရှိရပါတယ် — <style> tag ထည့်ရင် browser က ပထမ rule ကို invalid selector အဖြစ် ပစ်ပါတယ်။ ပြောင်းပြန်အနေနဲ့ HTML ထဲမှာ CSS rule တွေကို <style> မပါဘဲ ရေးရင် page ပေါ်မှာ စာသားအဖြစ် ပေါ်လာပါတယ်။
3. Override လုပ်ဖို့ inline style ကို အမြဲ ရွေးတာ
Inline style က အမြဲ အနိုင်ရလို့ ဆွဲဆောင်မှု ရှိပေမယ့် နောက်ပိုင်း stylesheet ကနေ ပြန်ပြောင်းဖို့ !important ပဲ ကျန်ပါတော့တယ်။ Rule တစ်ခု အနိုင်မရရင် DevTools မှာ ဘယ် rule က အနိုင်ရနေလဲ ကြည့်ပြီး selector ဒါမှမဟုတ် အစီအစဉ်ကို ပြင်ပါ။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- Site အသစ်တိုင်းကို external stylesheet တစ်ခုနဲ့ စပါ၊ page တစ်ခုတည်း ဖြစ်နေရင်တောင် နောက်မှ page ထပ်တိုးတဲ့အခါ အလုပ် မပိုပါဘူး။
- <link> တွေကို <head> ထဲမှာ ထားပြီး အထွေထွေ file (reset၊ base) ကို အရင်၊ အသေးစိတ် file (page တစ်ခုချင်း) ကို နောက်မှ ထားပါ — နောက်ကျတာက tie ဖြစ်ရင် အနိုင်ရလို့ပါ။
- JavaScript က inline style ထည့်ရမယ့်အခါ property တစ်ခုတည်း (width၊ transform) ကိုပဲ ထည့်ပြီး ကျန်တဲ့ ပုံပေါ်ပုံကို class တွေနဲ့ ထိန်းပါ။
- Internal <style> နဲ့ စမ်းပြီး အဆင်ပြေသွားရင် rule တွေကို .css file ထဲ ရွှေ့ဖို့ မမေ့ပါနဲ့။
Mini challenge
Mini challenge: inline style တွေကို rule တွေအဖြစ် ပြောင်းပါ
Starter ထဲက menu က inline style တွေနဲ့ ပြည့်နေပါတယ်။ ပုံပေါ်ပုံ တူတူပဲ ကျန်အောင် style တိုင်းကို <head> ထဲက <style> block ရဲ့ class rule တွေအဖြစ် ရွှေ့ပြီး style attribute တွေကို ဖျက်ပါ။
- ပြီးတဲ့အခါ style="" attribute တစ်ခုမှ မကျန်ရဘူး
- ထပ်နေတဲ့ style နှစ်ခု (menu item paragraph တွေ) ကို class rule တစ်ခုတည်းနဲ့ ရွေးရမယ်
- <style> block ရဲ့ ထိပ်မှာ နောက်ပိုင်း menu.css ဆီ ရွှေ့မယ့်အကြောင်း comment ထည့်ရမယ်
- Browser မှာ ပုံပေါ်ပုံက မူလနဲ့ တူရမယ်
<!-- TODO: add <!DOCTYPE html>, <head> with a <style> block, and <body> -->
<!-- TODO: move each style="..." into a class rule, then delete the attribute -->
<h2 style="color: #1d4ed8; font-family: Georgia, serif;">Menu</h2>
<p style="color: #374151; line-height: 1.6;">Mohinga - $4</p>
<p style="color: #374151; line-height: 1.6;">Tea leaf salad - $5</p>
<p style="color: #166534; font-weight: bold;">Today: free green tea</p>ရလဒ်: Page က အရင်အတိုင်း ပုံပေါ်ပါတယ် — Georgia font နဲ့ အပြာ ခေါင်းစဉ်၊ မီးခိုးရင့်ရောင် menu item နှစ်ခု၊ bold အစိမ်းရောင် offer စာကြောင်း။ ကွာခြားချက်က HTML ထဲမှာ class တွေပဲ ကျန်တော့တာပါ။
အရိပ်အမြွက် ကြည့်ရန်
Class နာမည်ကို ပုံပေါ်ပုံ (blue-text) အစား အဓိပ္ပာယ် (menu-title၊ menu-item၊ offer) နဲ့ ပေးပါ — နောက်မှ အရောင် ပြောင်းရင်တောင် နာမည်က မှန်နေဆဲ ဖြစ်ပါတယ်။
အဖြေ ကြည့်ရန်
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Menu</title>
<style>
/* later: cut these rules into menu.css and use <link> instead */
.menu-title {
color: #1d4ed8;
font-family: Georgia, serif;
}
.menu-item {
color: #374151;
line-height: 1.6;
}
.offer {
color: #166534;
font-weight: bold;
}
</style>
</head>
<body>
<h2 class="menu-title">Menu</h2>
<p class="menu-item">Mohinga - $4</p>
<p class="menu-item">Tea leaf salad - $5</p>
<p class="offer">Today: free green tea</p>
</body>
</html>Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- External (<link>) က real site တွေအတွက် ပုံသေ ရွေးချယ်မှု ဖြစ်ပြီး page အများကြီး မျှသုံးကာ cache ဖြစ်ပါတယ်။
- Internal (<style>) က page တစ်ခုတည်း စမ်းတာနဲ့ demo တွေအတွက် သင့်တော်ပါတယ်။
- Specificity တူရင် external နဲ့ internal ကြားမှာ HTML ထဲ နောက်ကျတဲ့ rule က အနိုင်ရပါတယ်။
- Inline style က selector rule အားလုံးကို အနိုင်ရလို့ dynamic value လို ချွင်းချက်တွေအတွက်ပဲ သုံးပါ။
CSS reference — MDN Web Docs