🏠 Home / Hub

🎨 CSS Lesson 01 — Selectors & Specificity

← Back to CSS Menu  |  🏠 Hub

1. Element Selector

Tag name တိုက်ရိုက် target လုပ်တယ် — page ထဲ အဲဒီ element အကုန်ကို apply ဖြစ်

p    { color: red; }
h4   { color: blue; }
span { color: green; }

ဒီ paragraph က p selector ကြောင့် red ဖြစ်တယ်

ဒီ heading က h4 selector ကြောင့် blue ဖြစ်တယ်

ဒီ span က span selector ကြောင့် green ဖြစ်တယ်

2. Class Selector (.)

Class တူတဲ့ element တွေ အကုန်ကို target — element တစ်ခုမှာ class တစ်ခုထက်မက ထည့်လို့ရ

.highlight { background: yellow; }
.bold-text { font-weight: bold; }
.big-text  { font-size: 1.3em; }

<p class="highlight bold-text">Multiple classes!</p>

highlight class ထည့်ထားတယ်

bold-text class ထည့်ထားတယ်

big-text class ထည့်ထားတယ်

Classes 3 ခုလုံး ထည့်ထားတယ်!

3. ID Selector (#)

Page ထဲ unique element တစ်ခုပဲ target — ID တစ်ခုကို element တစ်ခုပဲ သုံးသင့်

#special-title {
  color: red;
  font-size: 1.4em;
  text-decoration: underline;
}

ပုံမှန် paragraph (class/id မပါ)

ID="special-title" — unique element!

ပုံမှန် paragraph နောက်တစ်ကြောင်း

4. Combinator Selectors

div p      → Descendant  (div ထဲ မည်သည့် p မဆို)
div > p    → Child       (div ရဲ့ direct child p ပဲ)
h5 + p     → Adjacent    (h5 ရဲ့ ချက်ချင်းနောက် p)
h5 ~ p     → Sibling     (h5 ရဲ့ နောက် sibling p အကုန်)
Descendant (div p)

green — descendant

green — nested ရှိရင်လည်း

Child (div > p)

red bold — direct child

ပုံမှန် — nested မဟုတ်

Adjacent (h5 + p)
Heading

ချက်ချင်းနောက် p → blue!

ဒုတိယ p → မပြောင်းဘူး

Sibling (h5 ~ p)
Heading

sibling p → yellow bg

ဒါလည်း sibling → yellow bg

5. Pseudo-class (:)

Element ရဲ့ state ပေါ် မူတည်ပြီး style ချ — :hover, :focus, :nth-child, :first-child...

a:hover      { background: blue; color: white; }
li:first-child  { color: green; }
li:nth-child(2) { color: blue; }
li:nth-child(odd) { background: #f0f0f0; }
input:focus  { border-color: blue; }

Links: Home About Contact

Hover over links to see :hover effect


:nth-child demo

  • Item 1 — :first-child (green)
  • Item 2 — :nth-child(2) (blue)
  • Item 3 — :nth-child(odd)
  • Item 4
  • Item 5 — :last-child (red)

:focus demo:

6. Pseudo-element (::)

Element ရဲ့ တစ်ပိုင်းကို target — ::before, ::after, ::first-line, ::first-letter

p::first-line    { color: blue; }       /* ပထမဆုံး line */
p::first-letter  { font-size: 2em; }    /* ပထမဆုံး စာလုံး */
.box::before { content: "→ "; color: green; }
.box::after  { content: " ←"; color: red; }

::first-line (blue):
ဒီ paragraph ရဲ့ ပထမဆုံး line ကသာ blue ဖြစ်မယ်
ဒုတိယ line ကတော့ ပုံမှန် color ဖြစ်နေမယ်


::before / ::after:

ဒီ text မှာ before နဲ့ after ပါတယ်

ဒါလည်း ထပ်တူ style ဖြစ်တယ်


::first-letter (Drop Cap):

ဒီ paragraph ရဲ့ ပထမဆုံး စာလုံးက ကြီးတယ် — drop cap effect လို့ ခေါ်တယ်

7. Attribute Selector ([])

[type="text"]    { border: 2px solid green; }
[type="email"]   { border: 2px solid blue; }
[disabled]       { opacity: 0.4; }
[href^="https"]  { color: green; }   /* starts with */
[href$=".pdf"]   { color: red; }     /* ends with */
[href*="vue"]    { color: orange; }  /* contains */
a[target="_blank"]::after { content: " ↗"; }
Vue.js site [target=_blank] → ↗ icon

8. Specificity — ဘယ် Style ကအနိုင်ရသလဲ

Conflict ဖြစ်ရင် specificity အမြင့်ဆုံးကအနိုင်ရတယ်

/* Specificity တွက်နည်း: (inline, id, class, element) */

p              → (0, 0, 0, 1) → score: 1
.class         → (0, 0, 1, 0) → score: 10
#id            → (0, 1, 0, 0) → score: 100
style=""       → (1, 0, 0, 0) → score: 1000
!important     → nuclear option 💣
elem
p { color: blue } → score: 1
class
.class { color: green } → score: 10
id
#id { color: red } → score: 100

ဒီ text က blue ဖြစ်ဖို့ p{color:blue} ရှိပေမဲ့ .class{green} ရှိပေမဲ့ — #id{red} ကအနိုင်ရတယ်!

Next: CSS Lesson 02 → Box Model

📌 Study Checklist