ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Selector က လိပ်စာ တစ်ခုလိုပါပဲ။ "p" ဆိုရင် "paragraph အားလုံးဆီ"၊ ".note" ဆိုရင် "note class ကပ်ထားတဲ့ element အားလုံးဆီ"၊ "#intro" ဆိုရင် "id=intro ရှိတဲ့ element တစ်ခုတည်းဆီ" လို့ ပြောတာပါ။ လိပ်စာ တိကျလေ ရောက်တဲ့ element နည်းလေ ဖြစ်ပါတယ်။
Element တစ်ခုကို လိပ်စာ နှစ်ခုက တစ်ပြိုင်နက် စာပို့ပြီး မတူတဲ့ အမိန့်တွေ ပေးရင် (တစ်ခုက အပြာ၊ တစ်ခုက အနီ) browser က ပိုတိကျတဲ့ လိပ်စာကို ယုံပါတယ်။ အဲဒီ "ဘယ်လောက် တိကျလဲ" ကို specificity လို့ ခေါ်ပါတယ်။
Problem → Why → Solution
ပြဿနာ: danger button က အနီရောင် မဖြစ်ဘူး၊ ဒါကြောင့် !important ထည့်လိုက်တယ်
Sidebar ထဲက button တွေကို အပြာရောင် ဖြစ်စေဖို့ #sidebar .button rule တစ်ခု ရှိပြီးသားပါ။ အခု "Delete account" button ကို အနီရောင် ဖြစ်စေချင်လို့ .button-danger class အသစ် ထည့်ပြီး stylesheet ရဲ့ အောက်ဆုံးမှာ rule ရေးပါတယ်။ Button က အပြာရောင်ပဲ ဆက်ဖြစ်နေပါတယ်။ နောက်ဆုံး ရေးတဲ့ rule က အနိုင်ရမယ်လို့ ထင်ထားလို့ ရှုပ်ထွေးသွားပြီး !important ထည့်လိုက်တော့ အလုပ်ဖြစ်သွားပါတယ်။
တစ်ပတ်ကြာတော့ တခြားသူက disabled state ကို မီးခိုးရောင် ဖြစ်စေချင်ပေမယ့် သူ့ rule ကလည်း !important ကို မကျော်နိုင်လို့ နောက်ထပ် !important တစ်ခု ထပ်ထည့်ရပါတယ်။
#sidebar .button { /* specificity (1,1,0) */
background-color: #1d4ed8;
}
.button-danger { /* (0,1,0): loses, even though it comes later */
background-color: #b91c1c;
}
/* the "fix" that starts an arms race */
.button-danger { background-color: #b91c1c !important; }ဘာကြောင့် ဒီလို ဖြစ်တာလဲ
Source order (နောက်ကျတာ အနိုင်ရတာ) က specificity တူမှသာ အလုပ်လုပ်ပါတယ်။ #sidebar .button မှာ ID တစ်ခု ပါလို့ class တစ်ခုတည်း ပါတဲ့ .button-danger ကို ဘယ်နေရာမှာ ရေးရေး အနိုင်ရပါတယ်။ !important က ဒီ စည်းမျဉ်းကို ကျော်ပေမယ့် ပြဿနာ အမှန် — ID ပါတဲ့ မလိုအပ်ဘဲ တိကျလွန်းတဲ့ selector — ကို ဖုံးထားရုံပါ။ အမှန် ဖြေရှင်းနည်းက ID ကို ဖယ်ပြီး .sidebar-button လို class တစ်ခုတည်းသုံးတာပါ။
Core concept: selector အမျိုးအစားတွေနဲ့ specificity
| Selector | ဘာကို ရွေးလဲ | Specificity |
|---|---|---|
| p | <p> element အားလုံး (type selector) | (0,0,1) |
| .note | class="note" ပါတဲ့ element အားလုံး | (0,1,0) |
| #intro | id="intro" ရှိတဲ့ element တစ်ခု | (1,0,0) |
| * | Element အားလုံး (universal) | (0,0,0) |
| article p | <article> ထဲက p အားလုံး (descendant) | (0,0,2) |
Selector တွေကို ပေါင်းစပ်လို့ ရပါတယ်။ Space မပါဘဲ ကပ်ရေးရင် "element တစ်ခုတည်းက ဒီအချက်အားလုံးနဲ့ ကိုက်ရမယ်" — p.note က note class ပါတဲ့ paragraph၊ .card.featured က class နှစ်ခုလုံး ပါတဲ့ element ပါ။ Space ခြားရင်တော့ descendant ဖြစ်သွားပါတယ် — .card .featured က .card ရဲ့ အထဲမှာ ရှိတဲ့ .featured element ပါ။ Comma ကတော့ selector list ဖြစ်ပြီး h1, h2 က h1 ရော h2 ရော ရွေးပါတယ်။
Specificity ကို ကော်လံ သုံးခုနဲ့ တွက်ပါတယ်: (ID အရေအတွက်, class/attribute/pseudo-class အရေအတွက်, type/pseudo-element အရေအတွက်)။ ဘယ်ကနေ ညာကို နှိုင်းယှဉ်ပြီး ပထမဆုံး ကွာတဲ့ ကော်လံက ဆုံးဖြတ်ပါတယ်၊ ဒါကြောင့် (1,0,0) က (0,5,0) ကို အနိုင်ရပါတယ် — class ဘယ်လောက်များများ ID တစ်ခုကို မကျော်နိုင်ပါဘူး။ Universal selector * နဲ့ combinator (space, >) တွေက ဘာမှ မပေါင်းထည့်ပါဘူး။
Specificity က cascade ရဲ့ အဆင့်တစ်ဆင့်သာ ဖြစ်ပါတယ်။ Browser က အရင်ဆုံး origin နဲ့ importance (!important ပါလား) ကို ကြည့်ပြီး cascade layer တွေ ရှိရင် layer အစီအစဉ်ကို ကြည့်ပါတယ်၊ ပြီးမှ specificity ကို ကြည့်ပြီး အဲဒါ တူရင် stylesheet ထဲမှာ နောက်ကျတဲ့ rule က အနိုင်ရပါတယ်။ Inline style attribute ကတော့ selector rule အားလုံးထက် မြင့်ပါတယ်။ ဒါကြောင့် ကောင်းမွန်တဲ့ နည်းဗျူဟာက specificity ကို နိမ့်နိမ့်နဲ့ တူတူ ထိန်းထားတာပါ — အများစုကို class တစ်ခုတည်းနဲ့ ရေးပြီး ID ကို styling အတွက် ရှောင်ရင် rule တစ်ခုကို override လုပ်ချင်တဲ့အခါ နောက်မှာ ရေးလိုက်ရုံနဲ့ ရပါတယ်။
Specificity ကို ကိုယ်တိုင် တွက်ဖို့ မလိုအပ်တဲ့အခါလည်း ရှိပါတယ်: DevTools ရဲ့ Styles pane မှာ selector ပေါ် mouse တင်လိုက်ရင် browser အများစုက (1,0,1) လို specificity ကို tooltip နဲ့ ပြပါတယ်။ ဒါပေမယ့် ကိုယ်တိုင် တွက်တတ်တာက rule တစ်ခု မရေးခင်ကတည်းက အနိုင်ရမရ ခန့်မှန်းနိုင်စေပါတယ်။
- !important ပါသလား စစ်
- Specificity (ID, class, type) ကို နှိုင်း
- တူရင် နောက်ကျတဲ့ rule အနိုင်
ဒီ ယှဉ်ပြိုင်မှုက property တစ်ခုချင်းအလိုက် ဖြစ်ပါတယ် — ရှုံးတဲ့ rule ရဲ့ တခြား property တွေက ဆက်အလုပ်လုပ်ပါတယ်။
အထွေထွေ ပုံစံ: အခြေခံ selector တွေ
element { } /* every <element> */
.class-name { } /* every element with class="class-name" */
#id-name { } /* the one element with id="id-name" */
* { } /* every element */
element.class-name { } /* one element matching both */
ancestor descendant { } /* descendant anywhere inside ancestor */
first, second { } /* selector list: both */HTML ထဲမှာ class="note" လို့ အစက် မပါဘဲ ရေးပြီး CSS ထဲမှာ .note လို့ အစက်နဲ့ ရေးပါတယ်။ ID လည်း အတူတူပါပဲ: HTML မှာ id="intro"၊ CSS မှာ #intro ပါ။
Example: တစ်ခုထက်ပိုတဲ့ rule တွေ ထိတဲ့ paragraph တွေ
ဒီ page မှာ paragraph လေးခု ရှိပြီး တစ်ခုချင်းကို မတူတဲ့ rule အစုတွေက ထိပါတယ်။ Browser မှာ မဖွင့်ခင် paragraph တစ်ခုချင်းရဲ့ အရောင်၊ bold ဖြစ်မဖြစ်နဲ့ line-height ကို စာရွက်ပေါ်မှာ ခန့်မှန်းရေးကြည့်ပြီးမှ DevTools နဲ့ တိုက်စစ်ပါ။
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Selectors</title>
<style>
p { color: #374151; } /* (0,0,1) */
.note { color: #1d4ed8; } /* (0,1,0) */
#intro { color: #b91c1c; } /* (1,0,0) */
p.note { font-weight: bold; } /* (0,1,1) */
h1, h2 { font-family: Georgia, serif; } /* selector list */
article p { line-height: 1.8; } /* (0,0,2) descendant */
</style>
</head>
<body>
<article>
<h1>Selectors</h1>
<p id="intro" class="note">Intro paragraph</p>
<p class="note">A note</p>
<p>Plain paragraph</p>
</article>
<h2>Outside the article</h2>
<p>Outside paragraph</p>
</body>
</html>Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Three rules want the colour
#intro { color: #b91c1c; } /* (1,0,0) */ပထမ paragraph ကို p (0,0,1)၊ .note (0,1,0)၊ #intro (1,0,0) သုံးခုလုံးက color ပေးဖို့ ကြိုးစားပါတယ်။ ID ကော်လံမှာ 1 ရှိတဲ့ #intro က အနိုင်ရလို့ အနီရောင် ဖြစ်ပါတယ်။
- Class beats type
.note { color: #1d4ed8; } /* (0,1,0) */"A note" မှာ ID မရှိလို့ p နဲ့ .note ပဲ ယှဉ်ပါတယ်။ (0,1,0) က (0,0,1) ထက် ဒုတိယ ကော်လံမှာ ကြီးလို့ အပြာ ဖြစ်ပါတယ်။
- Compound selector
p.note { font-weight: bold; } /* (0,1,1) */Space မပါတဲ့ p.note က note class ပါတဲ့ paragraph ကိုပဲ ရွေးပါတယ်။ ပထမ paragraph နှစ်ခု bold ဖြစ်ပါတယ်။ font-weight ကို တခြား rule က မထိလို့ ဒီမှာ ယှဉ်ပြိုင်သူ မရှိပါဘူး — #intro က color မှာ အနိုင်ရပေမယ့် ဒီ bold ကို မထိခိုက်ပါဘူး။
- Descendant selector
article p { line-height: 1.8; } /* (0,0,2) descendant */Space က "အထဲမှာ" လို့ ဆိုလိုလို့ <article> အထဲက paragraph သုံးခုပဲ line-height 1.8 ရပါတယ်။ Article အပြင်ဘက်က "Outside paragraph" က browser default line-height နဲ့ ကျန်ပါတယ်။
- Selector list
h1, h2 { font-family: Georgia, serif; } /* selector list */Comma က rule နှစ်ခု ရေးစရာ မလိုအောင် h1 ရော h2 ရော တစ်ခါတည်း ရွေးပါတယ်။ h2 က article အပြင်မှာ ရှိပေမယ့် ဒီ selector မှာ descendant အခြေအနေ မပါလို့ ရွေးခံရပါတယ်။
"Intro paragraph" — အနီရင့် (#b91c1c)၊ bold၊ line-height 1.8။ "A note" — အပြာ (#1d4ed8)၊ bold၊ line-height 1.8။ "Plain paragraph" — မီးခိုးရင့် (#374151)၊ ပုံမှန် အထူ၊ line-height 1.8။ "Outside paragraph" — မီးခိုးရင့်၊ ပုံမှန် အထူ၊ browser default line-height။ h1 နဲ့ h2 နှစ်ခုလုံး Georgia font ဖြစ်ပါတယ်။Specificity ကို နိမ့်နိမ့် ထိန်းတဲ့ tool တွေ
:where() ထဲက selector တွေက specificity လုံးဝ မပေါင်းပါဘူး၊ ဒါကြောင့် :where(#sidebar) p က (0,0,1) ပဲ ရှိပြီး ရိုးရိုး p rule တစ်ခုနဲ့တောင် နောက်မှ ရေးရင် override လုပ်လို့ ရပါတယ်။ :is() ကတော့ သူ့ argument တွေထဲက အတိကျဆုံး တစ်ခုရဲ့ specificity ကို ယူပါတယ်။ Cascade layer (@layer) တွေကလည်း အုပ်စုလိုက် အစီအစဉ် ချပေးပါတယ်: layer ထဲမှာ မရှိတဲ့ ပုံမှန် rule တွေက layer ထဲက rule တွေကို specificity ဘယ်လောက်ပဲ ကွာကွာ အနိုင်ရပါတယ်။
/* (0,0,1): the ID inside :where() counts for nothing */
:where(#sidebar) p { color: #b91c1c; }
p { color: #1d4ed8; } /* also (0,0,1) and later: wins */
@layer base {
p.lead { color: #b91c1c; } /* (0,1,1), but inside a layer */
}
p { color: #1d4ed8; } /* unlayered: beats the layer */လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
Team ကြီးတွေက specificity ပြိုင်ပွဲ မဖြစ်အောင် naming convention တွေ သုံးကြပါတယ်။ BEM (Block, Element, Modifier) မှာ .card၊ .card__title၊ .card--featured လို class တစ်ခုတည်း selector တွေကိုပဲ ရေးလို့ rule အားလုံးရဲ့ specificity (0,1,0) တူပြီး ပဋိပက္ခကို source order နဲ့ပဲ ဖြေရှင်းပါတယ်။ Tailwind CSS လို utility framework တွေကလည်း class တစ်ခုတည်း rule တွေနဲ့ အလားတူ အကျိုးကို ရပါတယ်။
ID တွေကို styling အတွက် မသုံးပေမယ့် page ထဲ link ချိတ်ဖို့ (href="#pricing")၊ form label နဲ့ input ချိတ်ဖို့ (for="email") နဲ့ JavaScript က element တစ်ခုကို ရှာဖို့ ဆက်သုံးပါတယ်။ Third-party widget ဒါမှမဟုတ် CMS က ထုတ်ပေးတဲ့ HTML ကို override လုပ်ရတဲ့အခါ DevTools မှာ သူ့ selector ရဲ့ specificity ကို ကြည့်ပြီး တူညီတဲ့ specificity နဲ့ ကိုယ့် stylesheet ကို နောက်မှ load လုပ်တာက !important ထက် အများကြီး သန့်ရှင်းပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. CSS ထဲမှာ အစက် ဒါမှမဟုတ် # မေ့တာ (HTML မှာ ပိုထည့်တာ)
CSS မှာ note { } လို့ ရေးရင် <note> ဆိုတဲ့ tag ကို ရှာလို့ ဘာမှ မကိုက်ပါဘူး။ ပြောင်းပြန်အနေနဲ့ HTML မှာ class=".note" လို့ ရေးရင် class နာမည်က ".note" ဖြစ်သွားလို့ .note selector နဲ့ မကိုက်ပါဘူး။ CSS မှာ အစက်၊ HTML မှာ အစက် မပါ။
2. .a.b နဲ့ .a .b ကို ရောထွေးတာ
Space တစ်ခုက အဓိပ္ပာယ် လုံးဝ ပြောင်းစေပါတယ်။ .card.featured က class နှစ်ခုလုံး ပါတဲ့ element ကို ရွေးပြီး .card .featured က .card အထဲက element ကို ရွေးပါတယ်။ Rule က မကိုက်ရင် DevTools ရဲ့ Elements panel မှာ class တွေ ဘယ် element ပေါ်မှာ ရှိလဲ စစ်ပါ။
3. Override ဖို့ selector ကို ရှည်အောင် ဆွဲတာ
body div.main .content ul li a လို selector က ယာယီ အနိုင်ရပေမယ့် HTML ဖွဲ့စည်းပုံ နည်းနည်း ပြောင်းတာနဲ့ ကျိုးပြီး နောက် override တိုင်းကို ပိုရှည်တဲ့ selector လိုအောင် လုပ်ပါတယ်။ Class တစ်ခုထည့်ပြီး selector တိုတိုနဲ့ ရေးပါ။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- Style လုပ်ချင်တဲ့ element တိုင်းကို ပုံပေါ်ပုံ မဟုတ်ဘဲ အဓိပ္ပာယ်ကို ပြတဲ့ class နာမည် (.price၊ .nav-link) ပေးပါ။
- Type selector တွေကို body၊ a၊ h1 လို base style တွေအတွက်ပဲ သုံးပြီး component တွေကို class နဲ့ ရေးပါ။
- Descendant selector ကို အဆင့် နှစ်ဆင့်ထက် မပိုစေပါနဲ့ — HTML ဖွဲ့စည်းပုံနဲ့ တင်းကျပ်စွာ ချိတ်မိသွားပါတယ်။
- !important ကို ထည့်ချင်စိတ် ပေါ်လာတိုင်း ဘယ် rule က အနိုင်ရနေလဲ DevTools မှာ အရင် ကြည့်ပါ။
Mini challenge
Mini challenge: tea house navigation
Starter ထဲက HTML ကို မပြောင်းဘဲ ID selector တစ်ခုမှ မသုံးဘဲ rule တွေ ရေးပါ။ လက်ရှိ page ကို ပြတဲ့ link က တခြား link တွေထက် ထင်ရှားရပါမယ်။
- header ထဲက h1 ကို descendant selector နဲ့ Georgia font ပေးရမယ်
- nav ထဲက link အားလုံးကို အရောင်တစ်ခုနဲ့ ညာဘက် margin ပေးရမယ်
- .active link ကို bold နဲ့ မတူတဲ့ အရောင် ပေးရမယ် — nav a ကို အနိုင်ရအောင် specificity သုံးပါ (!important မသုံးရ)
- p.note ကို italic ဖြစ်စေပြီး ရိုးရိုး paragraph ကို မထိရဘူး
<!-- TODO: wrap in <!DOCTYPE html>, <head> and <body>; add a <style> block -->
<style>
/* TODO: header h1 - Georgia font */
/* TODO: nav a - colour + margin-right */
/* TODO: the .active link inside nav - bold + another colour (beat nav a) */
/* TODO: p.note - italic */
</style>
<header>
<h1 id="site-title">Golden Tea House</h1>
<nav>
<a href="/">Home</a>
<a href="/menu" class="active">Menu</a>
<a href="/contact">Contact</a>
</nav>
</header>
<p>Open daily from 7 am.</p>
<p class="note">Closed on public holidays.</p>ရလဒ်: ခေါင်းစဉ်က Georgia font ဖြစ်ပါတယ်။ Link သုံးခုလုံး အပြာ၊ ကြားမှာ နေရာလွတ် ရှိပြီး "Menu" link ကတော့ bold နဲ့ အနီရင့်ရောင် ဖြစ်ပါတယ်။ "Closed on public holidays." က italic ဖြစ်ပြီး "Open daily" စာကြောင်းက မပြောင်းပါဘူး။
အရိပ်အမြွက် ကြည့်ရန်
nav a ရဲ့ specificity က (0,0,2) ပါ။ nav a.active က (0,1,2) ဖြစ်လို့ stylesheet ထဲ ဘယ်နေရာမှာ ရေးရေး အနိုင်ရပါတယ်။
အဖြေ ကြည့်ရန်
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Golden Tea House</title>
<style>
header h1 {
font-family: Georgia, serif;
}
nav a {
color: #1d4ed8;
margin-right: 1rem;
}
nav a.active {
font-weight: bold;
color: #b91c1c;
}
p.note {
font-style: italic;
}
</style>
</head>
<body>
<header>
<h1 id="site-title">Golden Tea House</h1>
<nav>
<a href="/">Home</a>
<a href="/menu" class="active">Menu</a>
<a href="/contact">Contact</a>
</nav>
</header>
<p>Open daily from 7 am.</p>
<p class="note">Closed on public holidays.</p>
</body>
</html>Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Type (p)၊ class (.note)၊ ID (#intro)၊ universal (*) selector တွေနဲ့ comma list ကို သုံးပြီး element တွေကို ရွေးပါတယ်။
- .a.b က element တစ်ခုတည်းမှာ class နှစ်ခု၊ .a .b က .a အထဲက .b ဖြစ်ပါတယ်။
- Specificity ကို (ID, class, type) နဲ့ ဘယ်ကနေ ညာ နှိုင်းပြီး တူရင် နောက်ကျတဲ့ rule က အနိုင်ရပါတယ်။
- Class တစ်ခုတည်း selector တွေနဲ့ specificity ကို နိမ့်နိမ့် ထိန်းပြီး !important နဲ့ styling ID တွေကို ရှောင်ပါ။
CSS cascade and specificity — MDN Web Docs