Thuta Learning
HTML
IntermediateWeb Developmentအခြေခံ

Responsive Images

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

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

  • srcset ထဲက w descriptor တွေနဲ့ sizes attribute ကို သုံးပြီး browser ဘယ် file ရွေးမလဲ ကိုယ်တိုင် တွက်နိုင်ဖို့
  • Fixed-size image တွေအတွက် 1x / 2x descriptor ကို သုံးနိုင်ဖို့
  • Art direction နဲ့ format fallback အတွက် picture နဲ့ source ကို မှန်မှန်ကန်ကန် ရေးနိုင်ဖို့
  • width/height နဲ့ max-width: 100% ကို ပေါင်းသုံးပြီး layout shift ကို ကာကွယ်နိုင်ဖို့

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

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

အင်္ကျီဆိုင်တစ်ဆိုင်ကို စဉ်းစားကြည့်ပါ။ Size တစ်မျိုးတည်း ရောင်းရင် ကလေးတွေအတွက် ကြီးလွန်းပြီး လူကြီးတွေအတွက် ကျဉ်းလွန်းပါတယ်။ Responsive image ဆိုတာ ပုံတစ်ပုံကို S, M, L size အမျိုးမျိုးနဲ့ ချပေးထားပြီး ဝယ်သူ (browser) ကိုယ်တိုင် ကိုယ့်ကိုယ်ကို တိုင်းပြီး ကိုက်တဲ့ size ကို ယူသွားခိုင်းတာပါ။

Browser က ကိုယ့် screen ဘယ်လောက် ကျယ်လဲ၊ pixel density (1x လား 2x လား) ဘယ်လောက်လဲ သိပါတယ်။ ကျွန်တော်တို့ ပေးရမှာက file စာရင်း (srcset) နဲ့ ပုံက page ပေါ်မှာ ဘယ်လောက် ကျယ်မလဲဆိုတဲ့ အချက် (sizes) ပါ။ ကျန်တဲ့ တွက်ချက်မှုကို browser က လုပ်ပါတယ်။

Problem → Why → Solution

ပြဿနာ: ဖုန်းမှာ 3 MB ပုံကို download လုပ်ခိုင်းတာ

Camera ကနေ တိုက်ရိုက်ယူလာတဲ့ 4000 pixel အကျယ်ရှိတဲ့ ပုံကို img တစ်ခုနဲ့ ထည့်ပြီး CSS နဲ့ ချုံ့ပြထားပါတယ်။ Desktop မှာ လှပေမယ့် 375 pixel ကျယ်တဲ့ ဖုန်းကလည်း file အပြည့်ကို download လုပ်ရပါတယ်။ Mobile data နှေးတဲ့နေရာမှာ ပုံ ပေါ်လာဖို့ စက္ကန့်အတော်ကြာ စောင့်ရပြီး user ရဲ့ data ကိုလည်း အလကား ကုန်စေပါတယ်။

index.html
<!-- one 4000px, 3.2 MB file for every device -->
<img src="bagan-original.jpg" alt="Temples of Bagan at sunrise"
     style="width: 100%;">

CSS နဲ့ ချုံ့တာ ဘာကြောင့် မလုံလောက်တာလဲ

CSS က ပုံကို screen ပေါ်မှာ ဘယ်လောက်အရွယ် ပြမလဲ ဆုံးဖြတ်ရုံပါ၊ download လုပ်မယ့် file ကို မပြောင်းပါဘူး။ src တစ်ခုပဲ ရှိရင် browser မှာ ရွေးစရာ မရှိလို့ device တိုင်းက byte တိုင်းကို download လုပ်ရပါတယ်။ srcset ပေးလိုက်မှ browser က ကိုယ့် screen နဲ့ ကိုက်တဲ့ file သေးသေးကို ရွေးနိုင်ပါတယ်။

Core concept: srcset က ရွေးစရာ၊ sizes က ပုံရဲ့ အကျယ်

srcset က comma ခြားထားတဲ့ candidate စာရင်းပါ။ Candidate တစ်ခုစီမှာ file URL နဲ့ descriptor ပါပါတယ် — photo-800.jpg 800w ဆိုတာ "ဒီ file က pixel 800 ကျယ်တယ်" လို့ ပြောတာပါ။ Browser က file ကို download မလုပ်ခင် ရွေးရမှာမို့ file တစ်ခုချင်းရဲ့ အကျယ်ကို ကြိုပြောပေးရတာပါ။

ဒါပေမယ့် browser က CSS မ load ရသေးခင် ရွေးရလို့ ပုံက page ပေါ်မှာ ဘယ်လောက် ကျယ်မလဲ မသိသေးပါဘူး။ အဲဒါကို sizes က ပြောပေးပါတယ်။ sizes="(max-width: 800px) 100vw, 800px" ဆိုတာ "viewport 800px နဲ့ အောက်ဆိုရင် ပုံက screen အပြည့် ကျယ်မယ်၊ မဟုတ်ရင် 800px ကျယ်မယ်" ပါ။ Media condition တွေကို ဘယ်ကနေ ညာ စစ်ပြီး ပထမဆုံး ကိုက်တဲ့ တစ်ခုကို ယူကာ နောက်ဆုံးက condition မပါတဲ့ value က default ပါ။ sizes မပါရင် default က 100vw ဖြစ်ပါတယ်။

အဲဒီနောက် browser က slot အကျယ်ကို device pixel ratio နဲ့ မြှောက်ပါတယ်။ 375px ဖုန်း 2x screen ဆိုရင် 375 × 2 = 750 device pixel လိုလို့ 800w file က လုံလောက်တဲ့ အသေးဆုံးပါ။ 1x screen ဆိုရင် 375 ပဲ လိုလို့ 400w file ကို ယူပါတယ်။ Spec က browser ကို ဒီထက် ပိုလွတ်လပ်ခွင့် ပေးထားပါတယ် — data saver mode မှာ ပိုသေးတာ ယူနိုင်သလို cache ထဲမှာ ပိုကြီးတာ ရှိပြီးသားဆိုရင် အဲဒါကို ပြန်သုံးနိုင်ပါတယ်။ ဒါကြောင့် srcset က အမိန့်မဟုတ်ဘဲ အကြံပြုချက်ပါ။

src ကိုလည်း ဆက်ထားရပါမယ်၊ srcset ကို နားမလည်တဲ့ tool တွေအတွက် fallback ဖြစ်ပါတယ်။ width နဲ့ height attribute တွေက ပုံရဲ့ aspect ratio ကို browser ကို ကြိုပြောလို့ ပုံ မရောက်ခင် နေရာ ချန်ထားပေးပြီး CSS ရဲ့ max-width: 100%; height: auto; က ပုံကို container ထဲ အချိုးမပျက် ဝင်အောင် လုပ်ပါတယ်။

မှတ်ထားရမယ့် ပုံစံ: slot × density → file
  1. sizes ကနေ slot အကျယ်ကို ရှာ
  2. Device pixel ratio နဲ့ မြှောက်
  3. လုံလောက်တဲ့ အသေးဆုံး w file ရွေး
  4. CSS က ပုံကို slot ထဲ ဆွဲ

srcset နဲ့ sizes က ဘယ် file ကို download မလဲ ဆုံးဖြတ်ပြီး CSS က ဘယ်လောက်အရွယ် ပြမလဲ ဆုံးဖြတ်ပါတယ်။

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

html
<!-- width switching: same picture, different file widths -->
<img src="name-medium.jpg"
     srcset="name-small.jpg SMALLw, name-medium.jpg MEDIUMw, name-large.jpg LARGEw"
     sizes="(media-condition) slot-width, default-slot-width"
     width="W" height="H" alt="Description">

<!-- density switching: fixed-size image -->
<img src="icon.png" srcset="icon.png 1x, icon@2x.png 2x"
     width="W" height="H" alt="Description">

Image ရဲ့ အကျယ်က layout နဲ့ လိုက်ပြောင်းရင် w descriptor နဲ့ sizes ကို သုံးပြီး logo, avatar လို အရွယ်အစား မပြောင်းတဲ့ ပုံဆိုရင် x descriptor ကို သုံးပါ။ Candidate တစ်ခုတည်းမှာ w နဲ့ x ကို ရောမသုံးရပါဘူး။

Example: content column ထဲက landscape photo

ဒီ photo က ဖုန်းမှာ screen အပြည့် ကျယ်ပြီး screen ကြီးတွေမှာ 800px column ထဲမှာ ရပ်ပါတယ်။ File သုံးမျိုး ချပေးထားပြီး browser ဘယ်ဟာ ရွေးမလဲ device လေးမျိုးအတွက် output မှာ ပြထားပါတယ်။

photo.html
<style>
  img { max-width: 100%; height: auto; }
</style>

<img
  src="photo-800.jpg"
  srcset="photo-400.jpg 400w, photo-800.jpg 800w, photo-1200.jpg 1200w"
  sizes="(max-width: 800px) 100vw, 800px"
  width="800" height="533"
  alt="Hot-air balloons over the temples of Bagan at sunrise">

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

  1. srcsrc="photo-800.jpg"

    srcset ကို နားမလည်တဲ့ tool တွေအတွက် fallback ပါ။ ပျမ်းမျှ size ဖြစ်တဲ့ file ကို ထားတာ ပုံမှန်ပါ။

  2. srcsetsrcset="photo-400.jpg 400w, photo-800.jpg 800w, photo-1200.jpg 1200w"

    File သုံးခုနဲ့ တစ်ခုစီရဲ့ တကယ့် pixel အကျယ် (w) ပါ။ w value တွေက file ကိုယ်တိုင်ရဲ့ အကျယ်နဲ့ ကိုက်ရပါမယ်၊ မကိုက်ရင် browser က မှားတွက်ပါလိမ့်မယ်။

  3. sizessizes="(max-width: 800px) 100vw, 800px"

    Viewport 800px နဲ့ အောက်မှာ slot က 100vw ဖြစ်ပြီး ကျန်တာမှာ 800px ပါ။ ဒါက CSS ရဲ့ max-width: 100% နဲ့ ကိုက်ညီပါတယ် — sizes က CSS ကို ထပ်ပြောတာပဲ ဖြစ်ရပါမယ်။

  4. width / heightwidth="800" height="533"

    Aspect ratio (800:533 ≈ 3:2) ကို browser ကို ကြိုပြောလို့ ပုံ မရောက်ခင် နေရာချန်ထားပြီး စာသားတွေ အောက်ကို ခုန်မဆင်းပါဘူး။

  5. CSSimg { max-width: 100%; height: auto; }

    Container ထက် ပိုမကျယ်အောင် ကန့်သတ်ပြီး height: auto က width/height attribute ကြောင့် ပုံ ဆန့်ထွက်တာကို ကာကွယ်ပါတယ်။

You should see
ပုံတစ်ပုံ ပေါ်ပါတယ် — ဖုန်းမှာ screen အကျယ်အပြည့်၊ screen ကြီးမှာ 800px အကျယ်။ ပထမဆုံး ဝင်ကြည့်တဲ့အခါ (cache မရှိ) download လုပ်တဲ့ file:
375px ဖုန်း, 1x → photo-400.jpg
375px ဖုန်း, 2x → photo-800.jpg
1280px desktop, 1x → photo-800.jpg
1280px desktop, 2x → photo-1200.jpg
File မရှိရင် alt text ပေါ်ပါတယ်။

picture: art direction နဲ့ format fallback

srcset က ပုံ တစ်ပုံတည်းရဲ့ size မတူတဲ့ version တွေအတွက်ပါ။ ဖုန်းမှာ crop ကွဲတဲ့ ပုံ (ဥပမာ လူကို ဇူးမ်ဆွဲထားတဲ့ စတုရန်းပုံ) ပြချင်ရင်တော့ art direction လိုပြီး picture ကို သုံးပါတယ်။ Browser က source တွေကို အစဉ်လိုက် စစ်ပြီး media ဒါမှမဟုတ် type ကိုက်တဲ့ ပထမဆုံး source ကို ယူပါတယ်။ ဘာမှ မကိုက်ရင် img ကို သုံးပါတယ်။ img က မဖြစ်မနေ ပါရပြီး alt လည်း img ပေါ်မှာပဲ ရေးရပါတယ်။

hero.html
<picture>
  <source media="(max-width: 600px)" srcset="team-square.jpg"
          width="600" height="600">
  <source type="image/avif" srcset="team-wide.avif">
  <img src="team-wide.jpg" width="1200" height="500"
       alt="Our five-person support team in the Yangon office">
</picture>

loading="lazy" ကို ဘယ်အချိန် သုံးမလဲ

Page အောက်ပိုင်းက ပုံတွေမှာ loading="lazy" ထည့်ရင် user scroll နဲ့ နီးလာမှ download လုပ်ပါတယ်။ Page ထိပ်ဆုံးက hero ပုံကိုတော့ lazy မလုပ်ပါနဲ့၊ ပထမဆုံး မြင်ရမယ့် ပုံ နောက်ကျပြီး ပေါ်လာပါလိမ့်မယ်။

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

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

Production site တွေမှာ ပုံ size မျိုးစုံကို လက်နဲ့ export မလုပ်ပါဘူး။ Build tool, CMS ဒါမှမဟုတ် image CDN တွေက original ပုံ တစ်ပုံကနေ 400, 800, 1200 စတဲ့ width တွေနဲ့ AVIF/WebP format တွေကို အလိုအလျောက် ထုတ်ပေးပြီး srcset ကိုပါ ရေးပေးပါတယ်။ Next.js ရဲ့ Image component လို framework component တွေလည်း နောက်ကွယ်မှာ ဒီ srcset နဲ့ sizes ကိုပဲ ထုတ်ပေးတာပါ။ ဒါကြောင့် ဒီ lesson ကို နားလည်ထားရင် component ကို sizes prop မှန်မှန် ပေးနိုင်ပါတယ်။

Team တွေက ပုံရဲ့ performance ကို browser DevTools ရဲ့ Network tab နဲ့ Lighthouse လို tool တွေနဲ့ စစ်ပါတယ်။ ဖုန်း emulation မှာ ဘယ် file download ဖြစ်လဲ၊ ပုံက ပြတဲ့ size ထက် အများကြီး ကြီးနေလား ကြည့်ပါတယ်။ E-commerce site တွေမှာ product ပုံတွေက page weight ရဲ့ အများစု ဖြစ်တတ်လို့ srcset မှန်မှန် ရေးတာနဲ့ mobile မှာ page ပိုမြန်လာပြီး data ကုန်တာ သိသိသာသာ လျော့သွားတတ်ပါတယ်။

အမှားများ

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

1. w descriptor တွေ ပေးပြီး sizes မေ့တာ

sizes မပါရင် browser က ပုံကို 100vw လို့ ယူဆပါတယ်။ ပုံက sidebar ထဲမှာ 300px ပဲ ကျယ်ပေမယ့် 1440px desktop 2x screen က 2880px လိုတယ်ထင်ပြီး အကြီးဆုံး file ကို download လုပ်ပါလိမ့်မယ်။ CSS layout နဲ့ ကိုက်တဲ့ sizes ကို အမြဲ ရေးပါ။

2. Width/height မထည့်လို့ layout ခုန်တာ

Attribute မပါရင် browser က ပုံ download ပြီးမှ အမြင့်ကို သိလို့ အောက်က စာသားတွေ ရုတ်တရက် အောက်ကို ခုန်ဆင်းပြီး user က မှားနှိပ်မိတတ်ပါတယ်။ File ရဲ့ တကယ့် width နဲ့ height ကို ရေးပြီး CSS မှာ height: auto ထည့်ပါ။

3. Size ပြောင်းရုံအတွက် picture သုံးတာ

media query တွေ ပါတဲ့ source တွေက browser ကို အဲဒီ file ကိုပဲ သုံးဖို့ အတင်းခိုင်းလို့ density နဲ့ data saver ကို ထည့်မတွက်နိုင်တော့ပါဘူး။ ပုံ တစ်ပုံတည်းရဲ့ size ပဲ ကွာရင် img ပေါ်က srcset နဲ့ sizes ကို သုံးပြီး picture ကို crop ပြောင်းဖို့ ဒါမှမဟုတ် format fallback အတွက်ပဲ ထားပါ။

Best Practices

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

  • sizes ကို CSS layout ကို ကြည့်ပြီး ရေးပါ၊ layout ပြောင်းတိုင်း sizes ကိုလည်း လိုက်ပြင်ပါ။
  • Width သုံးမျိုး လေးမျိုးလောက် (ဥပမာ 400, 800, 1200, 1600) က အများအားဖြင့် လုံလောက်ပါတယ်၊ ဆယ်မျိုး မလိုပါဘူး။
  • DevTools ရဲ့ device emulation မှာ cache ပိတ်ပြီး ဘယ် file download ဖြစ်လဲ ကိုယ်တိုင် စစ်ကြည့်ပါ။
  • alt ကို ပုံ တစ်ပုံအတွက် တစ်ခုပဲ img ပေါ်မှာ ရေးပါ၊ ပုံ ဘာပြလဲ ဖော်ပြပါ၊ file name ကို မရေးပါနဲ့။

Mini challenge

Mini challenge: crop ပြောင်းတဲ့ hero ပုံ

ခရီးသွား blog post တစ်ခုရဲ့ hero ပုံကို ရေးပါ။ 700px ထက် ကျယ်တဲ့ screen မှာ landscape crop၊ ကျဉ်းတဲ့ screen မှာ စတုရန်း crop ပြရပါမယ်။ Crop တစ်ခုစီမှာ width နှစ်မျိုး ရှိပါတယ်။

  • picture ကို သုံးပြီး landscape source မှာ media="(min-width: 701px)" ထည့်ရမယ်
  • Landscape: hero-wide-1000.jpg (1000w), hero-wide-2000.jpg (2000w); square: hero-square-600.jpg (600w), hero-square-1200.jpg (1200w)
  • ပုံက screen အကျယ်အပြည့် ပြမှာမို့ sizes="100vw" ထည့်ရမယ်
  • img မှာ alt, width, height ပါရမယ်
trip.html
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Trip report</title>
  <!-- TODO: CSS so the image never overflows and keeps its ratio -->
</head>
<body>
  <h1>Three days in Bagan</h1>
  <!-- TODO: picture
       - source for wide screens: two landscape widths
       - img fallback: two square widths, sizes, width/height, alt -->
</body>
</html>

ရလဒ်: Cache မရှိတဲ့ ပထမ visit မှာ: 375px 1x → hero-square-600.jpg၊ 375px 2x → hero-square-1200.jpg၊ 1000px 1x → hero-wide-1000.jpg၊ 1280px 1x → hero-wide-2000.jpg။ File မရှိရင် alt text ပေါ်ပါတယ်။

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

Browser က source တွေကို အစဉ်လိုက် စစ်လို့ media ပါတဲ့ landscape source ကို ရှေ့ဆုံးမှာ ထားပါ။ Square crop ကို img ပေါ်မှာ ထားရင် source မကိုက်တဲ့ ကျဉ်းတဲ့ screen တွေအတွက် default ဖြစ်သွားပါတယ်။

အဖြေ ကြည့်ရန်
trip.html
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Trip report</title>
  <style>
    body { margin: 0; }
    img { display: block; max-width: 100%; height: auto; }
  </style>
</head>
<body>
  <h1>Three days in Bagan</h1>
  <picture>
    <source media="(min-width: 701px)"
            srcset="hero-wide-1000.jpg 1000w, hero-wide-2000.jpg 2000w"
            sizes="100vw" width="2000" height="800">
    <img src="hero-square-600.jpg"
         srcset="hero-square-600.jpg 600w, hero-square-1200.jpg 1200w"
         sizes="100vw" width="1200" height="1200"
         alt="Hot-air balloons drifting over Bagan's temples at sunrise">
  </picture>
</body>
</html>

Quick Quiz 1 / 3

srcset="s.jpg 400w, m.jpg 800w, l.jpg 1600w" sizes="100vw" ရှိတဲ့ ပုံကို 400px ကျယ်ပြီး 2x screen ရှိတဲ့ ဖုန်းက (cache မရှိ) ဘယ် file ကို ယူမလဲ။

Quick Quiz 2 / 3

Logo က page တိုင်းမှာ 160 × 40 CSS pixel အမြဲ ပြပါတယ်။ ဘယ် srcset က အသင့်တော်ဆုံးလဲ။

Quick Quiz 3 / 3

picture ထဲမှာ <source media="(max-width: 600px)" srcset="crop.jpg"> ပြီးမှ <img src="wide.jpg" alt="..."> ရှိပါတယ်။ 1200px desktop က ဘာကို ပြမလဲ။

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

  • srcset က file candidate တွေနဲ့ ၎င်းတို့ရဲ့ pixel အကျယ် (w) ကို ပေးပြီး sizes က ပုံရဲ့ layout slot အကျယ်ကို ပြောပါတယ်။
  • Browser က slot အကျယ် × device pixel ratio ကို တွက်ပြီး လုံလောက်တဲ့ အသေးဆုံး file ကို ရွေးပါတယ်။
  • Fixed-size ပုံတွေအတွက် 1x/2x descriptor ကို သုံးပြီး picture ကို crop ပြောင်းဖို့နဲ့ format fallback အတွက် ထားပါ။
  • width/height နဲ့ max-width: 100%; height: auto; က layout shift ကို ကာကွယ်ပါတယ်။

HTML Living Standard — Embedded Content — WHATWG

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

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

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

Responsive Images | Thuta Learning