Thuta Learning
CSS
AdvancedWeb Developmentအခြေခံ

Pseudo-elements

CSSသင်ခန်းစာ 2325 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • ::before နဲ့ ::after box တွေ ပေါ်လာဖို့ content property ဘာကြောင့် မဖြစ်မနေ လိုလဲ ရှင်းပြနိုင်ဖို့
  • Generated box တွေက element ရဲ့ အထဲမှာ ပထမ/နောက်ဆုံး child အဖြစ် ဝင်တာကို သိပြီး position နဲ့ နေရာချနိုင်ဖို့
  • ::marker, ::first-line, ::placeholder, ::selection တို့နဲ့ element အစိတ်အပိုင်းတွေကို style လုပ်နိုင်ဖို့
  • Screen reader တွေ generated text ကို ဖတ်တာကို သိပြီး decorative content နဲ့ အရေးကြီးတဲ့ content ကို ခွဲခြားနိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

ရိုးရိုးလေး ရှင်းရရင်

Pseudo-class က element တစ်ခုလုံးရဲ့ အခြေအနေကို ရွေးတာဖြစ်ပြီး pseudo-element ကတော့ HTML ထဲမှာ tag အဖြစ် မရှိတဲ့ "အပိုင်း" တစ်ခုကို ရွေးတာပါ။ စာပိုဒ်တစ်ခုရဲ့ ပထမ စာကြောင်း၊ list item ရှေ့က နံပါတ်၊ user ရွေးထားတဲ့ စာသား စတာတွေက တကယ်ရှိတဲ့ element တွေ မဟုတ်ပေမယ့် ::first-line, ::marker, ::selection နဲ့ style လုပ်လို့ရပါတယ်။

::before နဲ့ ::after ကတော့ ပိုထူးခြားပါတယ် — element ထဲမှာ box အသစ်တစ်ခုကို CSS ကနေ ဖန်တီးပေးပါတယ်။ ခေါင်းစဉ်အောက်က အရောင်မျဉ်း၊ quote mark ကြီး၊ "New" badge လို အလှဆင် အစိတ်အပိုင်းတွေကို HTML မထပ်ဘဲ ထည့်နိုင်ပါတယ်။

Problem → Why → Solution

ပြဿနာ: style အပြည့်ရေးထားပေမယ့် မျဉ်း မပေါ်တာ

ခေါင်းစဉ်အောက်မှာ 64px ရှည်တဲ့ အရောင်မျဉ်းလေး ထည့်ချင်လို့ ::after ပေါ်မှာ display, width, height, background အကုန် ရေးလိုက်ပါတယ်။ Page ကို refresh လုပ်တော့ ဘာမှ မပေါ်ပါဘူး၊ DevTools ရဲ့ Elements panel မှာလည်း ::after ကို ရှာမတွေ့ပါဘူး။ ခေါင်းစဉ်ရဲ့ အမြင့်က 30px ပဲ ဆက်ရှိနေပြီး အပို 11px (margin 8px + height 3px) ဘယ်တော့မှ မပေါင်းလာပါဘူး။

heading.css
.section-title::after {
  /* content is missing, so this box is never generated */
  display: block;
  width: 64px;
  height: 3px;
  margin-top: 8px;
  background: crimson;
}

ဘာကြောင့် box မဖန်တီးတာလဲ

::before နဲ့ ::after ရဲ့ content က default အားဖြင့် none ဖြစ်ပြီး content: none ဆိုရင် browser က box ကို လုံးဝ မဆောက်ပါဘူး — ကျန်တဲ့ property တွေက ချိတ်စရာ box မရှိလို့ ဘာမှ မလုပ်ပါဘူး။ content: "" (string အလွတ်) တစ်ကြောင်း ထည့်လိုက်တာနဲ့ box ပေါ်လာပြီး ခေါင်းစဉ်က 41px ဖြစ်သွားပါတယ်။

Core concept: generated box တွေနဲ့ element အစိတ်အပိုင်းတွေ

Pseudo-element ကို colon နှစ်ခု (::) နဲ့ ရေးပါတယ်၊ pseudo-class နဲ့ ခွဲသိနိုင်အောင်ပါ။ အဟောင်း :before, :after, :first-line, :first-letter လေးခုကိုတော့ colon တစ်ခုနဲ့ ရေးလည်း browser တွေ လက်ခံနေဆဲပါ၊ ဒါပေမယ့် code အသစ်မှာ :: ကို သုံးပါ။ Selector တစ်ခုမှာ pseudo-element တစ်ခုပဲ ထည့်လို့ရပြီး selector ရဲ့ နောက်ဆုံးမှာ ရှိရပါမယ်။

::before က element ရဲ့ ပထမ child အဖြစ်၊ ::after က နောက်ဆုံး child အဖြစ် element ရဲ့ အထဲမှာ ဝင်ပါတယ် — element ရဲ့ ရှေ့ ဒါမှမဟုတ် နောက် အပြင်ဘက်မှာ မဟုတ်ပါဘူး။ ဒါကြောင့် element ရဲ့ color, font စတာတွေကို inherit လုပ်ပြီး element ပေါ်မှာ position: relative ထားရင် position: absolute နဲ့ element ထဲမှာ နေရာချလို့ရပါတယ်။ Default display က inline ဖြစ်လို့ width နဲ့ height က အလုပ်မလုပ်ပါဘူး၊ block ဒါမှမဟုတ် inline-block လို့ ပြောင်းရပါတယ်။ <img>, <input> လို content ကို element ကိုယ်တိုင် မထိန်းတဲ့ replaced element တွေမှာ ::before / ::after ကို အားကိုးလို့ မရပါဘူး။

text
Where ::before and ::after live
-------------------------------
<h2 class="title">          the element's box
 +-------------------------------------------+
 | [::before]  Section text  [::after]       |
 +-------------------------------------------+
   first child ^                ^ last child
   both are INSIDE the h2 and inherit its styles

content က string ("→ ")၊ ပုံ (url(icon.svg))၊ attribute တန်ဖိုး (attr(data-label))၊ counter တွေ လက်ခံပါတယ်။ Generated text ကို user က mouse နဲ့ ရွေးလို့ copy ကူးလို့ မရပါဘူး၊ JavaScript ရဲ့ textContent ထဲမှာလည်း မပါပါဘူး။ ဒါပေမယ့် screen reader တွေက များသောအားဖြင့် ဖတ်ပါတယ် — Chromium မှာ ::before { content: "★ " } ပါတဲ့ Save button ရဲ့ accessible name က "★ Save" ဖြစ်သွားပါတယ်။ Decorative ဖြစ်ရင် content: "★" / "" လို slash နောက်က alternative text အလွတ်နဲ့ ဖျောက်လို့ရပါတယ်၊ ဒီ syntax ကို လက်ရှိ major browser တွေ support လုပ်ပေမယ့် မသိတဲ့ browser က declaration တစ်ခုလုံးကို လျစ်လျူရှုလို့ fallback content line တစ်ကြောင်း အရင်ရေးထားပါ။

Specificity မှာ pseudo-element တစ်ခုက element selector တစ်ခုနဲ့ တန်ဖိုးတူပါတယ်။ Pseudo-class ကို pseudo-element ရဲ့ ရှေ့မှာ ထားလို့ရပါတယ် — a:hover::after က link ကို hover လုပ်နေတုန်း ::after box ကို ပြောင်းပါတယ်၊ ဒါပေမယ့် a::after:hover လို ပြောင်းပြန် ရေးရင် selector တစ်ခုလုံး invalid ဖြစ်ပြီး rule ကို လျစ်လျူရှုပါတယ်။ ::before / ::after ကို hover လုပ်လို့ ရတဲ့ သီးခြား element အဖြစ် မမြင်ပါနဲ့၊ event တွေက parent element ဆီ ရောက်ပါတယ်။

Pseudo-element နှစ်မျိုး
  1. အပိုင်းကို ရွေး: ::marker, ::first-line
  2. Box အသစ် ဆောက်: ::before / ::after
  3. content ရှိမှ box ပေါ်
  4. Box က element ထဲမှာ child အဖြစ် နေ

::before / ::after ကို element ရဲ့ အထဲက ပထမနဲ့ နောက်ဆုံး span လေးနှစ်ခုလို မြင်ပါ — HTML ထဲမှာ မမြင်ရတာပဲ ကွာပါတယ်။

အထွေထွေ ပုံစံ

css
.element { position: relative; }        /* anchor for absolute boxes */

.element::before {
  content: "text" / "alt text";           /* required: "" for an empty box */
  position: absolute;                      /* or display: block */
}

li::marker { color: value; }             /* style an existing part */
p::first-line { font-weight: value; }

Box ဆောက်တဲ့ ::before / ::after မှာ content ကို အမြဲ ရေးပြီး layout အတွက် display ဒါမှမဟုတ် position ကို ပေးပါ။ Element အစိတ်အပိုင်းကို style လုပ်တဲ့ pseudo-element တွေမှာတော့ content မလိုပါဘူး။

Example: ခေါင်းစဉ် accent နဲ့ note icon

ဒီ example မှာ ::after နဲ့ ဗလာ box တစ်ခုကို gradient မျဉ်းအဖြစ် ဆွဲပြီး ::before နဲ့ emoji စာသားကို ထည့်ပါတယ်။ <h2 class="section-title"> နဲ့ <p class="note"> ပါတဲ့ HTML မှာ စမ်းပြီး DevTools Elements panel မှာ ::before, ::after node တွေကို ဖွင့်ကြည့်ပါ။

.section-title::after {
  content: "";
  display: block;
  width: 64px;
  height: 3px;
  margin-top: 8px;
  background: linear-gradient(90deg, cyan, magenta);
}

.note::before {
  content: "💡 ";
}

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. content: "";content: "";

    String အလွတ်ဖြစ်ပေမယ့် box ကို ဖန်တီးဖို့ လုံလောက်ပါတယ်။ ဒီ line ကို ဖျက်လိုက်ရင် ကျန်တဲ့ property ခြောက်ခုလုံး အလုပ်မလုပ်တော့ပါဘူး။

  2. display: block;display: block;

    Inline ကနေ block ပြောင်းလို့ width/height အလုပ်လုပ်ပြီး မျဉ်းက ခေါင်းစဉ်စာသားရဲ့ အောက်ကို ဆင်းပါတယ်။

  3. width/heightheight: 3px;

    64px x 3px မျဉ်းဖြစ်ပြီး margin-top: 8px နဲ့ ပေါင်းလို့ ခေါင်းစဉ်က 11px ပိုမြင့်လာပါတယ်။

  4. gradientbackground: linear-gradient(90deg, cyan, magenta);

    Box ထဲမှာ စာမရှိလို့ မြင်ရတာ background ပဲ ဖြစ်ပါတယ်။ ဘယ်ကနေ ညာ cyan ကနေ magenta ကူးပါတယ်။

  5. .note::beforecontent: "💡 ";

    စာသား content ဖြစ်လို့ inline အဖြစ် note ရဲ့ စာရှေ့မှာ ပေါ်ပါတယ်။ Screen reader က emoji ရဲ့ နာမည် ("light bulb" လိုမျိုး) ကို ဖတ်နိုင်လို့ decorative ပဲ ဆိုရင် "💡 " / "" လို alt text အလွတ် ပေးတာ ပိုကောင်းပါတယ်။

You should see
ခေါင်းစဉ်ရဲ့ အောက် 8px မှာ 64px ရှည်၊ 3px ထူတဲ့ cyan-to-magenta gradient မျဉ်း ပေါ်ပြီး note စာသားရဲ့ ရှေ့မှာ 💡 emoji ပေါ်ပါတယ်။ ဒီ emoji ကို mouse နဲ့ ရွေးလို့ မရပါဘူး။

Element အစိတ်အပိုင်းကို style လုပ်တဲ့ pseudo-element များ

Pseudo-elementStyle လုပ်တဲ့ အပိုင်း
::markerList item ရဲ့ bullet ဒါမှမဟုတ် နံပါတ် (color, font, content လို property အနည်းငယ်ပဲ ရ)
::first-lineBlock ရဲ့ ပထမဆုံး ပြထားတဲ့ စာကြောင်း၊ screen အကျယ်အလိုက် ပြောင်းပါတယ်
::first-letterDrop cap အတွက် ပထမ စာလုံး
::placeholderInput ထဲက placeholder စာ
::selectionUser ရွေးထားတဲ့ စာ (color, background လို property အနည်းငယ်)

::placeholder ကို style လုပ်တဲ့အခါ contrast ကို သတိထားပါ — မှိန်လွန်းရင် ဖတ်ရခက်ပြီး label အစားလည်း placeholder ကို မသုံးသင့်ပါဘူး၊ စာရိုက်လိုက်တာနဲ့ ပျောက်သွားလို့ပါ။

လက်တွေ့ project တွေမှာ

Real project တွေမှာ ဘယ်လို သုံးလဲ

Production CSS မှာ ::before / ::after ကို အလှဆင်ဖို့ အများဆုံး သုံးကြပါတယ် — required field label ဘေးက asterisk၊ testimonial card ထိပ်က quote mark ကြီး၊ ပုံပေါ်က gradient overlay၊ dropdown button ရဲ့ CSS-only arrow၊ tooltip ရဲ့ သုံးထောင့်အမြီး စတာတွေပါ။ HTML ကို သန့်သန့်ရှင်းရှင်း ထားနိုင်ပြီး component ရဲ့ style အားလုံး CSS file တစ်ခုထဲမှာ စုထားနိုင်ပါတယ်။

Content-heavy site တွေမှာ ::marker နဲ့ ordered list နံပါတ်တွေကို brand color ပေးတာ၊ ::selection နဲ့ highlight color ကို theme နဲ့ ကိုက်အောင် ညှိတာမျိုး လုပ်ကြပါတယ်။ Team တော်တော်များများရဲ့ စည်းကမ်းက ရိုးရှင်းပါတယ် — CSS ပိတ်လိုက်ရင် ပျောက်သွားလည်း ကိစ္စမရှိတဲ့ အရာပဲ pseudo-element ထဲ ထည့်ရမယ်။ "Sale", "Out of stock" လို အချက်အလက်က HTML ထဲမှာ ရှိရပါမယ်၊ ဒါမှ ဘာသာပြန် tool, search engine နဲ့ copy-paste လုပ်တဲ့ user တွေ မြင်ရမှာပါ။

အမှားများ

အဖြစ်များတဲ့ အမှားတွေ

1. Inline box ပေါ်မှာ width/height ရေးတာ

content: "" ထည့်ထားပေမယ့် display မပြောင်းရင် ::after က inline ဖြစ်နေလို့ width: 64px; height: 3px; က လျစ်လျူရှုခံရပြီး မျဉ်း မမြင်ရပါဘူး။ display: block ဒါမှမဟုတ် inline-block ပေးပါ၊ ဒါမှမဟုတ် position: absolute သုံးပါ (absolute box တွေက block ဖြစ်သွားပါတယ်)။

2. Parent မှာ position: relative မေ့တာ

Quote mark ကို ::before { position: absolute; top: 0; left: 0; } နဲ့ နေရာချပြီး card ပေါ်မှာ position: relative မထားရင် quote mark က card ထောင့်မှာ မဟုတ်ဘဲ positioned ancestor အနီးဆုံး ဒါမှမဟုတ် page ရဲ့ ထောင့်ဆီ ပျံသွားပါတယ်။

3. အရေးကြီးတဲ့ စာကို content ထဲ ထည့်တာ

.price::after { content: " (incl. tax)"; } လို ရေးရင် user က ဈေးနှုန်းကို copy ကူးတဲ့အခါ အဲဒီ စာ ပါမလာပါဘူး၊ ဘာသာပြန် tool တွေလည်း များသောအားဖြင့် မပြန်ပေးပါဘူး။ အဓိပ္ပာယ်ရှိတဲ့ စာကို HTML ထဲ ထည့်ပြီး pseudo-element ကို အလှဆင်ဖို့ပဲ သုံးပါ။

Best Practices

အကောင်းဆုံး အလေ့အကျင့်များ

  • ::before / ::after rule တိုင်းကို content line နဲ့ စရေးပါ၊ ဒါမှ မေ့စရာ မရှိမှာပါ။
  • CSS ကို ပိတ်ပြီး page ကို ကြည့်ပါ — အဓိပ္ပာယ် ပျောက်သွားရင် အဲဒီ content ကို HTML ထဲ ရွှေ့ပါ။
  • Decorative icon နဲ့ emoji တွေကို alt text အလွတ် (/ "") နဲ့ ပေးပြီး screen reader နဲ့ တစ်ခါ စမ်းနားထောင်ပါ။
  • Absolute positioned box တွေအတွက် parent ပေါ်မှာ position: relative ကို တစ်ခါတည်း တွဲရေးပါ။

Mini challenge

Mini challenge: form နဲ့ quote ကို အလှဆင်ပါ

HTML ကို မပြင်ဘဲ pseudo-element တွေနဲ့ပဲ required label၊ quote နဲ့ numbered list ကို အလှဆင်ပါ။ Screen reader တွေ မလိုအပ်တဲ့ အသံ မထွက်စေရ။

  • .required label နောက်မှာ အနီရောင် " *" ထည့်ပြီး alt text ကို အလွတ်ထားပါ (fallback line ပါ ထည့်)
  • .quote ရဲ့ ဘယ်ဘက်အပေါ်ထောင့်မှာ 48px ရှိတဲ့ “ ကို absolute နဲ့ ချပါ
  • .steps list ရဲ့ နံပါတ်တွေကို အပြာရောင် bold ပြောင်းပါ
  • Page တစ်ခုလုံးမှာ ရွေးထားတဲ့ စာရဲ့ နောက်ခံကို အဝါနု ပြောင်းပါ
decorate.html
<style>
  /* TODO: .required::after - red " *", hidden from screen readers */

  /* TODO: .quote - make it the positioning anchor, leave 40px on the left */

  /* TODO: .quote::before - a 48px light-blue opening quote mark, top-left */

  /* TODO: blue, bold numbers for .steps list items */

  /* TODO: light yellow background for selected text */
</style>
<label class="required" for="email">Email</label>
<input id="email" type="email" required>
<blockquote class="quote">Small steps every day.</blockquote>
<ol class="steps"><li>Plan</li><li>Build</li><li>Ship</li></ol>

ရလဒ်: "Email" နောက်မှာ crimson " *" ပေါ်ပေမယ့် input ရဲ့ accessible name က "Email" ပဲ ဖြစ်ပါတယ်။ Quote စာရဲ့ ဘယ်ဘက်မှာ 48px အပြာနု “ ပေါ်ပြီး list နံပါတ် 1. 2. 3. တွေက အပြာ bold ဖြစ်ပါတယ်။ စာရွေးရင် အဝါနု နောက်ခံ ပေါ်ပါတယ်။

အရိပ်အမြွက် ကြည့်ရန်

content: " *"; ပြီးရင် content: " *" / ""; ကို ဆက်ရေးပါ — syntax အသစ်ကို မသိတဲ့ browser က ပထမ line ကို သုံးပါလိမ့်မယ်။ Quote mark အတွက် .quote ပေါ်မှာ position: relative ထားပြီး ::before ကို left: 0; top: -12px နဲ့ ချပါ။

အဖြေ ကြည့်ရန်
decorate.html
<style>
  .required::after {
    content: " *";
    content: " *" / "";
    color: crimson;
  }
  .quote { position: relative; margin: 24px 0; padding-left: 40px; }
  .quote::before {
    content: "“";
    content: "“" / "";
    position: absolute;
    left: 0;
    top: -12px;
    font-size: 48px;
    color: #a5b4fc;
  }
  .steps li::marker { color: #1a73e8; font-weight: bold; }
  ::selection { background: #fde68a; }
</style>
<label class="required" for="email">Email</label>
<input id="email" type="email" required>
<blockquote class="quote">Small steps every day.</blockquote>
<ol class="steps"><li>Plan</li><li>Build</li><li>Ship</li></ol>

Quick Quiz 1 / 3

.badge::after { width: 8px; height: 8px; background: red; border-radius: 50%; } မှာ အနီစက်လေး ဘာကြောင့် မပေါ်တာလဲ။

Quick Quiz 2 / 3

<button class="star">Save</button> မှာ .star::before { content: "★ "; } ရေးထားရင် Chromium မှာ button ရဲ့ accessible name ဘာဖြစ်မလဲ။

Quick Quiz 3 / 3

::before ရဲ့ box က DOM ထဲမှာ ဘယ်နေရာမှာ ရှိသလို ပြုမူလဲ။

မှတ်ထားရမယ့် အချက်များ

  • Pseudo-element တွေကို :: နဲ့ ရေးပြီး element အစိတ်အပိုင်း (::marker, ::first-line, ::selection) ကို style လုပ်တာ ဒါမှမဟုတ် box အသစ် (::before, ::after) ဖန်တီးတာ လုပ်ပါတယ်။
  • ::before / ::after မှာ content မပါရင် box မရှိပါဘူး၊ default က inline ဖြစ်လို့ အရွယ်အစားပေးဖို့ display ကို ပြောင်းပါ။
  • Generated box တွေက element ရဲ့ အထဲမှာ ပထမ/နောက်ဆုံး child ဖြစ်လို့ parent ပေါ်က position: relative နဲ့ absolute နေရာချပါ။
  • Screen reader တွေ generated text ကို ဖတ်ပါတယ် — decoration ကို content: "★" / "" လို alt text အလွတ်နဲ့ ဖျောက်ပြီး အရေးကြီးတဲ့ စာကို HTML ထဲ ထားပါ။

CSS reference — MDN Web Docs

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Pseudo-elements | Thuta Learning