Thuta Learning
HTML
BasicWeb Developmentအခြေခံ

Attributes

HTMLသင်ခန်းစာ 625 မိနစ်

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

  • Attribute တွေကို start tag ထဲမှာ quote နဲ့ မှန်မှန်ကန်ကန် ရေးနိုင်ဖို့
  • id၊ class၊ lang လို global attribute တွေနဲ့ href၊ src၊ alt လို element-specific attribute တွေကို ခွဲခြားနိုင်ဖို့
  • Boolean attribute တွေက ရှိနေရုံနဲ့ on ဖြစ်ကြောင်း ရှင်းပြနိုင်ဖို့
  • Page တစ်ခုမှာ id ကို တစ်ခါတည်း သုံးပြီး ပုံတွေကို အဓိပ္ပာယ်ရှိတဲ့ alt ပေးနိုင်ဖို့

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

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

Element က ဘာလဲ (link၊ ပုံ၊ button) ဆိုတာ ပြောပြီး attribute တွေက အဲဒီ element အကြောင်း အသေးစိတ်ကို ပြောပါတယ်။ ပါဆယ်ထုပ်တစ်ထုပ်ပေါ်က တံဆိပ်တွေလိုပါပဲ — "ပို့ရမယ့် လိပ်စာ"၊ "ကွဲလွယ်တယ်"၊ "ဒီဘက်က အပေါ်" စတာတွေ ဖြစ်ပါတယ်။ Link ကို ဘယ်ကို သွားရမလဲ (href)၊ ပုံ file က ဘယ်မှာလဲ (src)၊ ပုံ မမြင်ရတဲ့သူအတွက် ဘာပြောရမလဲ (alt) ဆိုတာ attribute တွေက ပြောပါတယ်။

Attribute တွေကို start tag ထဲမှာ name="value" ပုံစံနဲ့ ရေးပြီး တစ်ခုနဲ့တစ်ခု space ခြားပါတယ်။ End tag ထဲမှာ attribute ဘယ်တော့မှ မထည့်ပါဘူး။

Problem → Why → Solution

ပြဿနာ: disabled="false" ရေးထားပေမယ့် button က ပိတ်နေဆဲ

Payment page တစ်ခုမှာ "Pay now" button ကို ဖွင့်ထားချင်လို့ disabled="false" လို့ ရေးလိုက်ပါတယ်။ Subscribe checkbox ကိုလည်း မရွေးထားချင်လို့ checked="no" လို့ ရေးပါတယ်။ Browser မှာ ဖွင့်ကြည့်တော့ button က မီးခိုးရောင်ဖြစ်ပြီး နှိပ်လို့ မရသလို checkbox ကလည်း ရွေးပြီးသား ဖြစ်နေပါတယ်။ Customer တွေ ငွေပေးလို့ မရဘဲ ပြန်ထွက်သွားကြပါတယ်။

checkout.html
<button type="submit" disabled="false">Pay now</button>
<input type="checkbox" checked="no"> Subscribe to offers

ဘာကြောင့် ဒီလို ဖြစ်တာလဲ

disabled နဲ့ checked က boolean attribute တွေပါ။ Boolean attribute တွေမှာ value က အရေးမကြီးပါဘူး — attribute ရှိနေရင် "true"၊ မရှိရင် "false" ပါ။ ဒါကြောင့် disabled="false" ဆိုတာ "disabled" လို့ ရေးထားတာနဲ့ အတူတူပါပဲ။ Off လုပ်ချင်ရင် attribute ကို လုံးဝ ဖယ်ထုတ်ရပါမယ်။

Core concept: attribute ရေးနည်း စည်းမျဉ်းနဲ့ အမျိုးအစားများ

Attribute တွေကို start tag ထဲမှာ element နာမည်ရဲ့ နောက်မှာ ရေးပြီး အစဉ်က အရေးမကြီးပါဘူး။ Attribute နာမည်တွေကို lowercase နဲ့ ရေးပြီး value တွေကို double quote ထဲ အမြဲ ထည့်ပါ — quote မပါရင် value ထဲက ပထမ space မှာ value ပြီးသွားပါတယ်။ Element တစ်ခုမှာ attribute တစ်ခုကို တစ်ကြိမ်ပဲ ရေးရပါတယ်၊ class နှစ်ခါ ရေးရင် browser က ပထမတစ်ခုကိုပဲ ယူပြီး ဒုတိယကို လျစ်လျူရှုပါတယ်။ Value ထဲမှာ double quote ပါနေရင် single quote နဲ့ ထုပ်လို့လည်း ရပါတယ်။ id value တွေက စာလုံးအကြီးအသေး ခွဲပါတယ် — Intro နဲ့ intro က id နှစ်ခု မတူပါဘူး၊ ဒါကြောင့် စာလုံးပေါင်း တစ်မျိုးတည်းကို ရွေးပြီး အမြဲ သုံးပါ။

Attributeဘယ်မှာ သုံးလဲအလုပ်
idElement အားလုံး (global)Page တစ်ခုလုံးမှာ တစ်ခုတည်းသော နာမည်
classElement အားလုံး (global)CSS/JS အတွက် အုပ်စုနာမည်၊ space ခြားပြီး အများကြီး ပေးလို့ရ
href<a>Link သွားမယ့် လိပ်စာ
src / alt<img>ပုံ file နဲ့ ပုံကို စာနဲ့ ဖော်ပြချက်
for<label>Label ကို ဒီ id ရှိတဲ့ input နဲ့ ချိတ်
disabled / checkedForm control တွေ (boolean)ရှိနေရင် on၊ မရှိရင် off

Global attribute တွေ (id၊ class၊ lang၊ title၊ hidden) ကို element တိုင်းမှာ သုံးလို့ ရပါတယ်။ Element-specific attribute တွေကတော့ သက်ဆိုင်တဲ့ element မှာပဲ အဓိပ္ပာယ်ရှိပါတယ် — <p href="..."> လို ရေးရင် error မတက်ပေမယ့် ဘာမှ မဖြစ်ပါဘူး။ id ကို page တစ်ခုမှာ တစ်ခါပဲ သုံးရပြီး class ကိုတော့ element အများကြီးမှာ ထပ်ခါထပ်ခါ သုံးလို့ ရပါတယ်။

alt က ပုံကို မမြင်ရတဲ့သူတွေ (screen reader သုံးသူ၊ ပုံ load မဖြစ်တဲ့အခါ) အတွက် ပုံရဲ့ နေရာမှာ အစားထိုးမယ့် စာပါ။ "image" ဒါမှမဟုတ် file နာမည် မဟုတ်ဘဲ ပုံက ဘာကို ပြောချင်လဲ ရေးပါ။ အလှဆင်ဖို့သက်သက် ပုံဆိုရင် alt="" (အလွတ်) ထားလို့ screen reader က ကျော်သွားပါတယ်။ width နဲ့ height ကတော့ ပုံ load မဖြစ်ခင် နေရာ ကြိုချန်ထားလို့ page ခုန်တာကို ကာကွယ်ပါတယ်။ target="_blank" ကတော့ link ကို tab အသစ်မှာ ဖွင့်ပါတယ်။

Element နဲ့ သူ့ setting တွေ
  1. Element က ဘာလဲ ပြော
  2. Attribute တွေက အသေးစိတ် ထပ်ပေး
  3. Browser က name="value" ကို ဖတ်
  4. Boolean: ရှိရင် on၊ မရှိရင် off

Boolean attribute ကို off လုပ်ဖို့ value ပြောင်းလို့ မရပါ — attribute ကို ဖယ်ရပါတယ်။

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

html
<element name="value" other-name="value">content</element>

<element boolean-attribute>content</element>

<voidelement name="value">

Attribute တွေက start tag ထဲမှာပဲ ရှိပြီး space နဲ့ ခြားပါတယ်။ Boolean attribute ကို value မပါဘဲ နာမည်တစ်ခုတည်း ရေးပါတယ်။ Void element မှာလည်း attribute တွေ ထည့်လို့ ရပါတယ်။

Example: attribute အမျိုးမျိုး

ဒီ example မှာ global attribute (id၊ class)၊ link နဲ့ ပုံ attribute တွေ၊ label နဲ့ input ကို ချိတ်တဲ့ for/id နဲ့ boolean attribute နှစ်ခု (checked၊ disabled) ပါပါတယ်။ popa.jpg ကို file နဲ့ folder တစ်ခုတည်းမှာ ထားရင် ပုံပေါ်ပြီး မထားရင် broken-image icon နဲ့ alt စာကို မြင်ရပါမယ်။

attributes.html
<p id="intro" class="lead">Learn HTML with small, working examples.</p>
<p><a href="https://developer.mozilla.org/" target="_blank">Open MDN Web Docs</a></p>
<p><img src="popa.jpg" alt="Mount Popa at sunrise" width="300" height="200"></p>
<p>
  <input type="checkbox" id="news" checked>
  <label for="news">Email me new lessons</label>
</p>
<p><button type="button" disabled>Send</button></p>

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

  1. Line 1<p id="intro" class="lead">

    id="intro" က page တစ်ခုလုံးမှာ ဒီ paragraph တစ်ခုတည်းရဲ့ နာမည်ဖြစ်ပြီး URL ရဲ့ #intro နဲ့ ဒီနေရာကို တိုက်ရိုက် ခုန်လို့ ရပါတယ်။ class="lead" ကိုတော့ နောက်ပိုင်း CSS နဲ့ အလှဆင်ဖို့ element အများကြီးမှာ ထပ်သုံးလို့ ရပါတယ်။

  2. Line 2<a href="https://developer.mozilla.org/" target="_blank">

    href က သွားမယ့် လိပ်စာ ဖြစ်ပြီး target="_blank" က tab အသစ်မှာ ဖွင့်ပါတယ်။ ခေတ်မီ browser တွေက ဒီလို link တွေအတွက် ဖွင့်လိုက်တဲ့ page ကို မူရင်း page ကို ထိန်းချုပ်ခွင့် အလိုအလျောက် မပေးပါဘူး။

  3. Line 3<img src="popa.jpg" alt="Mount Popa at sunrise" width="300" height="200">

    src က ပုံ file၊ alt က ပုံကို စာနဲ့ ဖော်ပြချက်၊ width နဲ့ height က ပုံ မရောက်ခင် နေရာ ကြိုချန်ပေးတာပါ။ img က void element ဖြစ်လို့ end tag မရှိပါဘူး။

  4. Lines 5-6<label for="news">Email me new lessons</label>

    for="news" က id="news" ရှိတဲ့ checkbox နဲ့ label ကို ချိတ်ပါတယ်။ ဒါကြောင့် စာကို နှိပ်လည်း checkbox ပြောင်းပြီး screen reader က checkbox ကို label စာနဲ့ ဖတ်ပါတယ်။ checked က boolean ဖြစ်လို့ value မပါပါဘူး။

  5. Line 8<button type="button" disabled>Send</button>

    disabled ရှိနေလို့ button ကို နှိပ်လို့ မရ၊ Tab နဲ့လည်း ရောက်လို့ မရပါဘူး။ ပြန်ဖွင့်ချင်ရင် disabled ကို ဖယ်ရပါမယ်။

You should see
ပထမ paragraph၊ အောက်မှာ "Open MDN Web Docs" link (နှိပ်ရင် tab အသစ်မှာ ပွင့်)၊ 300×200 အရွယ် ပုံ (popa.jpg မရှိရင် alt စာ "Mount Popa at sunrise")၊ ရွေးပြီးသား checkbox နဲ့ "Email me new lessons" label (label ကို နှိပ်ရင်လည်း checkbox ပြောင်း) နဲ့ နှိပ်လို့မရတဲ့ မီးခိုးရောင် "Send" button ကို မြင်ရပါမယ်။

ကိုယ်ပိုင် data အတွက် data-* attribute

Standard attribute တွေထဲမှာ မပါတဲ့ အချက်အလက်ကို element ပေါ်မှာ သိမ်းချင်ရင် data- နဲ့ စတဲ့ နာမည်ကို သုံးပါ၊ ဥပမာ data-product-id="42"။ Browser က ဒါတွေကို ပြလည်း မပြ၊ အဓိပ္ပာယ်လည်း မပေးပါဘူး — JavaScript က ဖတ်ဖို့ပါ။ ကိုယ်ပိုင် နာမည်တွေ (ဥပမာ price="25") ကို တီထွင်ရေးရင် validator က error ပြပြီး နောင်တချိန် standard attribute အသစ်နဲ့ နာမည်တိုက်နိုင်ပါတယ်။

title attribute ကို အားမကိုးပါနဲ့

title="..." က mouse တင်ထားမှ ပေါ်တဲ့ tooltip လေး ပြပါတယ်။ ဒါပေမယ့် touch screen မှာ mouse မရှိလို့ မမြင်ရသလို keyboard သုံးသူတွေနဲ့ screen reader အများစုကလည်း ယုံကြည်လောက်အောင် မဖတ်ပြပါဘူး။ အရေးကြီးတဲ့ အချက်အလက်ကို page ပေါ်မှာ မြင်ရတဲ့ စာအဖြစ် ထားပါ။

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

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

Real project တွေမှာ class က CSS နဲ့ HTML ကို ချိတ်တဲ့ အဓိကနည်းပါ — design system တွေက class="btn btn-primary" လို class အများကြီး ပေါင်းသုံးကြပါတယ်။ id ကိုတော့ page ထဲက အပိုင်းတစ်ခုဆီ link လုပ်ဖို့ (#pricing)၊ label နဲ့ input ချိတ်ဖို့နဲ့ JavaScript က element တစ်ခုကို ရှာဖို့ သုံးပါတယ်။ React မှာတော့ class ကို className လို့ ရေးရပေမယ့် browser ရောက်တဲ့အခါ class attribute ပဲ ဖြစ်သွားပါတယ်။

Accessibility audit တွေမှာ alt မပါတဲ့ ပုံ၊ label မချိတ်ထားတဲ့ input တွေက အဖြစ်အများဆုံး တွေ့ရတဲ့ ပြဿနာတွေထဲ ပါပါတယ်။ Lighthouse လို tool တွေက ဒါတွေကို အလိုအလျောက် စစ်ပေးပါတယ်။ Online shop တွေမှာ "Add to cart" button ကို stock မရှိတဲ့အခါ disabled လုပ်တာ၊ data-* attribute နဲ့ product id သိမ်းပြီး JavaScript က cart ထဲ ထည့်တာတွေက နေ့စဉ် သုံးနေကျ pattern တွေပါ။

အမှားများ

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

1. id တစ်ခုကို နှစ်ခါ သုံးတာ

Card နှစ်ခုကို copy ကူးရင်း id="email" နှစ်ခု ဖြစ်သွားရင် label နှစ်ခုလုံး ပထမ input ကိုပဲ ချိတ်ပြီး #email link နဲ့ getElementById() ကလည်း ပထမ element ကိုပဲ ရှာတွေ့ပါတယ်။ ဒုတိယ input ကို နှိပ်တဲ့ label က ပထမ input ကို focus လုပ်သွားပါတယ်။ id တိုင်းကို page မှာ တစ်ခုတည်း ဖြစ်အောင် ထားပါ။

2. Value ကို quote မထည့်တာ

<img alt=Mount Popa at sunrise> လို ရေးရင် alt က "Mount" တစ်လုံးတည်း ဖြစ်ပြီး Popa၊ at၊ sunrise တို့က အဓိပ္ပာယ်မရှိတဲ့ attribute သုံးခု ဖြစ်သွားပါတယ်။ Value တိုင်းကို double quote ထဲ ထည့်ပါ။

3. alt ကို အသုံးမဝင်တဲ့ စာနဲ့ ဖြည့်တာ

alt="image"၊ alt="IMG_2031.jpg" ဒါမှမဟုတ် ကော်ပီကူးထားတဲ့ caption အရှည်ကြီးတွေက screen reader သုံးသူကို ဘာမှ မပြောပြပါဘူး။ ပုံက ဒီ page မှာ ဘာကို ပြောချင်လဲ တိုတိုရေးပါ၊ အလှဆင်ပုံဆိုရင် alt="" ထားပါ။

Best Practices

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

  • id နဲ့ class နာမည်တွေကို ပုံပန်းကို မဟုတ်ဘဲ အဓိပ္ပာယ်ကို ပြောတဲ့ စကားလုံးနဲ့ ပေးပါ: class="red-text" ထက် class="error-message" က ပိုကောင်းပါတယ်။
  • Multi-word နာမည်တွေကို lowercase နဲ့ hyphen ခြားရေးပါ (contact-form)၊ space ဘယ်တော့မှ မပါစေပါနဲ့။
  • Attribute အများကြီး ပါတဲ့ tag ကို attribute တစ်ခု တစ်ကြောင်းစီ ခွဲရေးပြီး diff နဲ့ review ကို ဖတ်ရ လွယ်အောင် လုပ်ပါ။
  • Image တိုင်းကို alt ရေးတဲ့အခါ "ဒီပုံကို ဖုန်းထဲက ဖတ်ပြရရင် ဘာပြောမလဲ" လို့ ကိုယ့်ကိုယ်ကို မေးပါ။

Mini challenge

Mini challenge: product card

ပုဂံ ယွန်းထည် ပန်းကန်လုံးအတွက် product card တစ်ခုကို attribute အမျိုးမျိုး မှန်မှန်ကန်ကန် သုံးပြီး ရေးပါ။

  • article မှာ class တစ်ခု၊ h2 မှာ page အတွက် တစ်ခုတည်းသော id ရှိရမယ်
  • img မှာ src၊ အဓိပ္ပာယ်ရှိတဲ့ alt၊ width နဲ့ height ပါရမယ်
  • ဈေးနှုန်း paragraph မှာ class နှစ်ခု (space ခြား) ပါရမယ်
  • Tab အသစ်မှာ ပွင့်တဲ့ link တစ်ခုနဲ့ disabled ဖြစ်နေတဲ့ "Sold out" button တစ်ခု ပါရမယ်
product.html
<!-- TODO: article with a class -->
  <!-- TODO: h2 with a unique id -->
  <!-- TODO: img with src, alt, width and height -->
  <!-- TODO: price paragraph with two classes -->
  <!-- TODO: link that opens in a new tab -->
  <!-- TODO: button that is disabled -->
<!-- TODO: close the article -->

ရလဒ်: Heading၊ ပုံ (ဒါမှမဟုတ် alt စာ)၊ ဈေးနှုန်း၊ tab အသစ်မှာ ပွင့်တဲ့ link နဲ့ နှိပ်လို့ မရတဲ့ "Sold out" button ပါတဲ့ card ကို မြင်ရပါမယ်။

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

Attribute အများကြီး ပါတဲ့ tag ကို line တစ်ကြောင်း ရှည်လွန်းရင် attribute တစ်ခုကို line တစ်ကြောင်းစီ ခွဲရေးလို့ ရပါတယ်။ disabled ကို value မပါဘဲ ရေးပါ။

အဖြေ ကြည့်ရန်
product.html
<article class="product">
  <h2 id="lacquer-bowl">Lacquer bowl from Bagan</h2>
  <img
    src="lacquer-bowl.jpg"
    alt="Red and black lacquer bowl with a fine gold pattern"
    width="320"
    height="240">
  <p class="price on-sale">25,000 Ks</p>
  <p>
    <a href="https://en.wikipedia.org/wiki/Burmese_lacquerware" target="_blank">
      Learn about Burmese lacquerware
    </a>
  </p>
  <button type="button" disabled>Sold out</button>
</article>

Quick Quiz 1 / 3

<button disabled="false">Pay</button> ကို နှိပ်လို့ ရမလား။

Quick Quiz 2 / 3

<img src="popa.jpg" alt=Mount Popa at sunrise> မှာ browser က ဘယ် alt value ကို ယူမလဲ။

Quick Quiz 3 / 3

Page တစ်ခုထဲမှာ element အများကြီးပေါ် ထပ်ခါထပ်ခါ သုံးဖို့ ရည်ရွယ်ထားတဲ့ attribute က ဘာလဲ။

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

  • Attribute တွေကို start tag ထဲမှာ name="value" ပုံစံ၊ double quote နဲ့ ရေးပါ။
  • Global attribute (id၊ class၊ lang) က element တိုင်းမှာ၊ href၊ src၊ alt လို attribute တွေက သက်ဆိုင်ရာ element မှာပဲ အလုပ်လုပ်ပါတယ်။
  • Boolean attribute (disabled၊ checked) တွေက ရှိနေရင် on — off ဖို့ ဖယ်ရပါတယ်။
  • id က page မှာ တစ်ခုတည်း၊ class က ထပ်သုံးလို့ရပြီး ပုံတိုင်းကို အဓိပ္ပာယ်ရှိတဲ့ alt ပေးပါ။

HTML Living Standard — HTML Syntax — WHATWG

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

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

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

Attributes | Thuta Learning