Panduan Responsive Images (srcset)

cara sajikan responsive images dengan srcset dan sizes attributes dan picture element, when untuk gunakan setiap, dan how responsive images fix LCP dan CLS without menjadi sebuah direct sinyal peringkat.

Pertama kali diterbitkan: 2 Jul 2026 · Terakhir diperbarui: 3 Agu 2026 · Advanced
Bahasa

Responsive images let browser (via srcset + sizes) atau author (via <picture>) sajikan right-sized image per device. srcset/sizes adalah resolution switching (sama image, browser picks — sebuah suggestion); <picture> adalah art direction atau format switching (author-dictated crops/formats — sebuah command). sebagian besar situs hanya perlu srcset/sizes. ini adalah not sebuah direct sinyal peringkat dan ini doesn't perubahan what Google indeks — Google indeks src URL — tetapi ini adalah concrete fix Lighthouse recommends untuk oversized images (-nya "Properly size images" _(terjemahan)_ “Properly size images” audit fails di sebuah 4KiB gap) dan, paired dengan explicit width/height, ini adalah how Anda mencegah CLS. pertahankan teks alt identical dan image URLs stable di seluruh breakpoints untuk pengindeksan mobile-pertama. I reuse exact srcset + width/height pattern dari my Ahrefs CLS artikel. Nests di bawah Image SEO hub.

TL;DR — Two jobs, one syntax family. srcset/sizes pada <img> = resolution switching (sama image, browser picks best-fit — sebuah suggestion); <picture> = art direction atau format switching (author-dictated crops/formats — sebuah command). sebagian besar situs hanya perlu pertama. Responsive images adalah not sebuah direct sinyal peringkat dan don’t perubahan what Google indeks — Google indeks src URL, so pertahankan alt text, filenames, dan image URLs stable di seluruh breakpoints (pengindeksan mobile-pertama). nyata payoff adalah Core Web Vitals: right-sizing images adalah fix behind Lighthouse’s “Properly size images” (terjemahan) “Properly size images” audit (fails di sebuah 4KiB gap), dan width/height (atau aspect-ratio) — not srcset — adalah what mencegah CLS. Don’t lazy-muat LCP image. I reuse exact srcset + width/height pattern dari my Ahrefs CLS piece.

Evidence for this claim The HTML responsive-image features srcset, sizes, and picture let browsers select an appropriate image candidate. Scope: Current HTML responsive image behavior. Confidence: high · Verified: WHATWG HTML: Responsive images Evidence for this claim Google can process responsive images and recommends src as a fallback while using srcset or picture for responsive delivery. Scope: Current Google Images responsive-image guidance. Confidence: high · Verified: Google Search Central: Responsive images

Two berbeda jobs, one syntax family

Everything di ini topic collapses ke two mechanisms, dan half confusion out there comes dari mixing them up:

  1. Resolution switching — yang sama image di berbeda sizes atau pixel densities. Anda give browser sebuah menu dengan srcset dan sizes pada <img>, dan * browser* decides which file untuk fetch berdasarkan viewport dan screen density. ini adalah umum case.
  2. Art direction — sebuah genuinely berbeda image per condition: sebuah wide crop pada desktop, sebuah tight vertical crop pada mobile, atau sebuah berbeda file format entirely. Here Anda dictate choice dengan <picture> element.

web.dev Learn: Responsive images course frames difference exactly right: dengan srcset browser gets suggestions, whereas “the picture element gives commands.” (terjemahan) “ undefined element gives commands.” dan scope-setting line worth tattooing pada wall, juga dari web.dev: “You probably won’t need to use the picture element for most of your responsive images — the srcset and sizes attributes on the img element cover a lot of use cases.” (terjemahan) “Anda probably won’t perlu untuk gunakan undefined element untuk sebagian besar dari Anda responsive images — undefined dan undefined attributes pada undefined element cover sebuah lot dari gunakan cases.” Reach untuk <picture> hanya when Anda actually perlu sebuah berbeda image, not sebuah berbeda size dari yang sama one.

srcset dan sizes — resolution switching

w-descriptors vs. x-descriptors

srcset takes sebuah comma-separated list dari candidate files, setiap tagged dengan sebuah descriptor. ada two jenis:

  • Width descriptors (w) — Anda state setiap file’s intrinsic pixel width (puppy-2000.jpg 2000w). browser combines itu dengan Anda sizes nilai untuk berfungsi out which file best fits space dan device’s pixel density. ini adalah flexible option dan what Anda’ll gunakan sebagian besar dari time.
  • Pixel-density descriptors (x) — Anda state which file adalah untuk which device pixel ratio (logo.png 1x, logo@2x.png 2x). gunakan ini untuk fixed-size images (sebuah avatar, sebuah logo) where displayed size tidak pernah perubahan; Anda tidak perlu sizes dengan x descriptors.

aturan dari thumb: fluid, konten-width images → w descriptors + sizes; fixed-size UI images → x descriptors.

Why sizes penting (dan what happens jika Anda skip ini)

dengan w descriptors, sizes isn’t optional cosmetics — ini adalah how browser knows how big image akan render so ini dapat choose sebelum layout. Per web.dev, sizes “tells the browser what size you expect the image to be displayed at under different conditions,” (terjemahan) “tells browser what size Anda expect image untuk menjadi displayed di di bawah berbeda conditions,” sebagai sebuah comma-separated list dari media conditions dan widths:

sizes="(max-width: 600px) 480px, 1000px"

Read ini sebagai: “if the viewport is 600px or narrower, the image will be about 480px wide; otherwise, about 1000px.” (terjemahan) “jika viewport adalah 600px atau narrower, image akan menjadi tentang 480px wide; otherwise, tentang 1000px.” Omit sizes dan browser assumes image fills full viewport width (100vw) — so pada sebuah wide screen ini dapat pull Anda largest file untuk sebuah image itu’s actually rendering di 400px, quietly defeating whole poin. Missing sizes adalah single sebagian besar umum srcset mistake.

Don’t guess di itu width nilai dari layout di Anda head — periksa image’s actual rendered CSS width di browser (DevTools → Elements → computed width dari <img> box) di setiap breakpoint Anda care tentang, dan set sizes untuk match. sebuah sizes nilai itu doesn’t match nyata rendered width masih causes browser untuk select wrong candidate bahkan though markup adalah syntactically correct — syntax validation alone won’t catch itu; see currentSrc periksa below untuk confirm what actually dimuat.

Worked contoh (my reusable pattern)

ini adalah exact pattern dari my Ahrefs piece, What adalah Cumulative Layout Shift (CLS) & cara meningkatkan ini, expanded dengan sizes:

<img
  src="puppy-1000.jpg"
  srcset="puppy-1000.jpg 1000w,
          puppy-2000.jpg 2000w,
          puppy-3000.jpg 3000w"
  sizes="(max-width: 600px) 480px, 1000px"
  width="1000" height="1000"
  alt="Puppy with balloons" />

setiap piece adalah muat-bearing: src adalah fallback dan URL Google indeks; srcset lists candidates dengan w descriptors; sizes tells browser rendered width; width/height reserve space ( CLS fix — more below); alt stays identical no penting which file memuat.

picture element — art direction dan format switching

When Anda actually perlu ini

gunakan <picture> untuk two things srcset dapat’t melakukan:

  1. Art direction — sebuah berbeda crop per breakpoint. web.dev’s contoh: pada sebuah narrow phone Anda mungkin sajikan tall, tight crop; pada sebuah wide desktop, sebuah pendek, wide one. sama subject, deliberately berbeda framing.
  2. Format switching — offer AVIF/WebP dengan sebuah JPEG fallback via <source type="…">, letting browser take pertama format ini mendukung. ini ties straight back untuk format guidance pada Image SEO hub.

syntax (dan fallback aturan)

<picture> element wraps one atau more <source> elements dan selalu ends dengan sebuah plain <img>:

<!-- Art direction: different crop per breakpoint -->
<picture>
  <source media="(max-width: 600px)" srcset="hero-crop-mobile.jpg">
  <img src="hero-crop-desktop.jpg" width="1200" height="675"
       alt="Product hero shot">
</picture>

<!-- Format switching: modern format with a fallback -->
<picture>
  <source type="image/avif" srcset="hero.avif">
  <source type="image/webp" srcset="hero.webp">
  <img src="hero.jpg" width="1200" height="675" alt="Product hero shot">
</picture>

itu trailing <img src> adalah not optional. Google states ini directly: per bagian 4.8.1 dari HTML Standard, “make sure that you provide an img element as a fallback with a src attribute when using the picture element.” (terjemahan) “pastikan itu Anda menyediakan sebuah undefined element sebagai sebuah fallback dengan sebuah undefined attribute when menggunakan undefined element.” ini adalah file older browser dan crawler fall back untuk — dan, again, one Google indeks.

melakukan srcset help SEO? Direct vs. indirect

Here’s framing setiap competing guide fumbles, so I’ll menjadi blunt.

Directly: no. Responsive-image markup adalah not sebuah sinyal peringkat cara teks alt atau filenames adalah untuk pencarian gambar. menambahkan srcset doesn’t lift Anda positions, dan ini doesn’t perubahan what appears di Google Images. Google indeks image referenced di src; srcset/<picture> variants adalah sebuah delivery mechanism, not separately dapat diindeks assets. pertahankan alt text, filename, dan data terstruktur attached untuk itu primary src image.

Indirectly: yes, dan ini adalah one dari biggest levers Anda memiliki. Right-sized images adalah paling effective concrete fix untuk two Core Web Vitals — LCP dan CLS — which feed Google’s halaman-experience signals. itu’s whole payoff. ini adalah yang sama shape sebagai WebP/AVIF story pada Image SEO hub: no boost untuk format itself, win adalah speed.

Responsive images dan LCP

Oversized images adalah one dari paling umum causes dari sebuah slow Largest Contentful Paint, dan responsive images adalah Lighthouse’s recommended fix. Lighthouse “Properly size images” (terjemahan) “Properly size images” audit lists “all images in your page that aren’t appropriately sized, along with the potential savings” (terjemahan) “semua images di Anda halaman itu aren’t appropriately sized, along dengan potential savings” — anything bigger daripada ini perlu untuk menjadi “just results in wasted bytes and slows down page load time.” (terjemahan) “hanya hasil di wasted bytes dan slows down pemuatan halaman time.” -nya fix, verbatim: “With responsive images, you generate multiple versions of each image, and then specify which version to use in your HTML or CSS using media queries, viewport dimensions, and so on.” (terjemahan) “dengan responsive images, Anda generate multiple versi dari setiap image, dan lalu specify which versi untuk gunakan di Anda HTML atau CSS menggunakan media kueri, viewport dimensions, dan so pada.”

Two specifics worth knowing:

  • ** failure threshold adalah 4KiB.** Lighthouse hanya flags sebuah image when “the rendered size is at least 4KiB smaller than the actual size.” (terjemahan) “ rendered size adalah setidaknya 4KiB smaller daripada actual size.” kecil overshoots don’t count; serving sebuah 3000px file ke sebuah 400px slot melakukan.
  • sebuah tooling shortcut. Google recommends RespImageLint, “a helpful bookmarklet for identifying the optimal srcset and sizes values for your images.” (terjemahan) “sebuah helpful bookmarklet untuk identifying optimal undefined dan undefined nilai untuk Anda images.” Run ini sebelum Anda hand-calculate breakpoints.

Don’t lazy-muat LCP image

ini adalah aturan people break sebagian besar. srcset pada Anda hero adalah fine — tetapi tidak pernah pair ini dengan lazy memuat pada LCP element. largest above—fold image seharusnya muat eagerly dengan fetchpriority="high", exactly sebagai Image SEO hub covers. dan when Anda bangun responsive <picture> hero, pertahankan swap logic native: Google warns itu ini “won’t load content that requires user interactions” (terjemahan) “won’t muat konten itu memerlukan pengguna interactions” like swiping atau clicking, so sebuah JS scheme itu hanya memuat nyata image setelah interaction hides ini dari Google.

Responsive images dan CLS

Here’s trap: srcset alone melakukan nothing untuk layout shift. Resolution switching decides which file memuat; ini doesn’t reserve space untuk ini. Left without dimensions, per web.dev’s mengoptimalkan CLS guide, “as images load, text shifts down the page to make room for them” (terjemahan) “sebagai images muat, text shifts down halaman untuk membuat room untuk them” — because space “can’t be allocated for it until the browser starts to download it and can determine its dimensions.” (terjemahan) “dapat’t menjadi allocated untuk ini until browser starts untuk download ini dan dapat determine -nya dimensions.”

fix adalah explicit dimensions, dan ini composes dengan responsive markup:

  • Set width dan height attributes pada <img>. Modern browser “set the default aspect ratio of images based on an image’s width and height attributes,” (terjemahan) “atur default aspect ratio dari images berdasarkan sebuah image’s undefined dan undefined attributes,” so itu two angka reserve right box sebelum apa pun srcset variant downloads. (web.dev)
  • Pair them dengan height: auto di CSS untuk fluid containers. ini adalah bagian itu looks contradictory tetapi isn’t: web.dev’s own guidance adalah untuk “use CSS to resize the image to the width of the container” (terjemahan) “gunakan CSS untuk resize image untuk width dari container” dan “set height: auto; to avoid using a fixed value for the image height.” (terjemahan) “set undefined untuk hindari menggunakan sebuah fixed nilai untuk image height.” HTML attributes atur intrinsic aspect ratio; CSS lets image scale fluidly. Together mereka mencegah CLS dan stay responsive.
  • atau gunakan CSS aspect-ratio untuk reserve space when Anda dapat’t set attributes.

Which kills sebuah persistent myth: width/height attributes don’t break fluid layouts. Attributes + height: auto adalah correct combo, not sebuah conflict. I walk melalui full layout-shift mechanics di my Ahrefs CLS artikel; pendek versi adalah “reserve the space so that there’s no shift” (terjemahan) “reserve space so itu there’s no shift” dan let image fill ini.

pengindeksan mobile-pertama implications

Google indeks primarily mobile versi dari Anda halaman, so two aturan penting more daripada mereka digunakan untuk when Anda’re serving responsive variants:

Google’s own summary dari why untuk bother adalah plain: “designing responsive web pages leads to better user experience, since people can access them across a plethora of device types.” (terjemahan) “designing responsive halaman web leads untuk better pengguna experience, since people dapat access them di seluruh sebuah plethora dari device jenis.”

What tentang Bing?

There’s no Bing-spesifik guidance pada srcset, sizes, atau <picture> element — Bing’s public documentation doesn’t address responsive-image markup di semua. So don’t justify responsive images dengan sebuah Bing algorithm hook; case adalah performa dan UX, which Bing (like Google) treats sebagai halaman-experience quality alih-alih something Anda dapat poin di sebuah documented responsive-images aturan untuk.

umum mistakes

  • Forgetting sizes dengan w descriptors — browser assumes 100vw dan dapat fetch Anda biggest file untuk sebuah kecil slot.
  • Dropping fallback src pada <picture> — sebuah spec violation Google explicitly flags, dan ini adalah URL crawler indeks.
  • Mismatched width/height ratio vs. actually-disajikan image — masih causes shift.
  • Lazy-memuat LCP hero — delays LCP untuk no alasan; muat ini eagerly dengan fetchpriority="high".
  • Regenerating image URLs per permintaan — breaks Google’s caching dan mobile-pertama “stable URL” (terjemahan) “stable URL” aturan.
  • Reaching untuk <picture> when srcset/sizes akan melakukan — unnecessary complexity; web.dev says sebagian besar situs don’t perlu ini.

Where ini sits

ini adalah sizing-dan-delivery deep dive di bawah broader Image SEO hub — performa half dari image SEO, parallel untuk how teks alt guide handles image-search half. untuk layout-shift mechanics di full, see my Ahrefs CLS artikel; untuk LCP, fetchpriority, dan memuat strategy, itu’s Core Web Vitals berfungsi wearing sebuah image hat.

Add an expert note

Pin an expert quote

New person? Create their unclaimed profile at /admin/experts/ → Pin a quote first.