Hướng dẫn về Responsive Images (srcset)
Cách serve responsive images với đó srcset và sizes các thuộc tính và đó picture element, khi nào nên dùng mỗi, và cách responsive images cách sửa LCP và CLS không có đang một trực tiếp tín hiệu xếp hạng.
Ngôn ngữ
Responsive images let đó trình duyệt (qua srcset + sizes) hoặc đó tác giả (qua <picture>) serve đó right-sized image theo device. srcset/sizes là resolution switching (giống nhau image, trình duyệt picks — một suggestion); <picture> là art direction hoặc format switching (tác giả-dictated crops/formats — một command). Hầu hết các trang chỉ cần srcset/sizes. Này không phải một trực tiếp tín hiệu xếp hạng và điều này không thay đổi điều gì Google indexes — Google indexes đó src URL — nhưng đây là đó concrete cách sửa Lighthouse khuyến nghị cho oversized images (của nó "Properly size images" _(bản dịch)_ «Properly size images» audit fails tại một 4KiB khoảng trống) và, paired với rõ ràng width/height, đây là cách bạn ngăn CLS. Giữ alt text giống hệt và image URLs ổn định trên breakpoints cho mobile-đầu tiên lập chỉ mục. I reuse đó chính xác srcset + width/height pattern từ my Ahrefs CLS bài viết. Nests dưới đó Image SEO hub.
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 imagesTóm tắt — Responsive images let trình duyệt load right-sized version của picture thay vì một giant file — nhỏ một on phone, big một on desktop. bạn làm nó với
srcsetvàsizescác thuộc tính on của bạn<img>tag (list sizes bạn có; trình duyệt picks). điều này không trực tiếp boost của bạn thứ hạng, nhưng nó làm các trang nhanh hơn và dừng them từ “jumping” as images load, và speed là điều gì đó Google measures.
Điều gì responsive images là
“responsive” image adapts để device looking tại nó. thay vì sending giống nhau huge photo để phone và desktop, bạn cho trình duyệt một vài versions tại khác sizes và let nó grab một đó fits. Phones nhận nhỏ file; big screens nhận big một. ít hơn wasted dữ liệu, nhanh hơn các trang.
có hai ways để làm điều này, và nó worth knowing mà là mà:
srcset+sizeson<img>— phổ biến một. giống nhau picture, khác sizes. bạn list Điều gì bạn có và trình duyệt chọn. Đây là được gọi là resolution switching.- **
<picture>element** — cho Khi bạn muốn genuinely khác image tại khác sizes (chẳng hạn, wide crop on desktop và tall, tight crop on mobile), hoặc modern file format với fallback. Đây là được gọi là art direction, và phần lớn các trang không cần nó.
basic version
Ở đây Điều gì resolution switching looks như:
<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" />srclà của bạn thông thường, luôn-ở đó fallback. giữ nó.srcsetlists versions bạn có và Cách wide mỗi một là (1000w= 1000 pixels wide).sizestells trình duyệt Cách big image sẽ thực ra xuất hiện so nó có thể pick right file trước khi nó loads bất cứ điều gì.widthvàheightreserve space so trang không jump khoảng trong khi image loads.altlà giống nhau mô tả bạn’d luôn ghi. giữ nó giống hệt trên mỗi version.
Vì sao bother ( honest câu trả lời)
Responsive images không earn bạn một xếp hạng “boost.” Điều gì they làm là làm các trang load
nhanh hơn và dừng đó layout từ shifting — và những là điều Google measures as
part of trang experience. Nếu một speed tool (Lighthouse, PageSpeed Insights) là nagging
bạn để “properly size images,” (bản dịch) «properly size images,» srcset/sizes là đó cách sửa đây là asking cho.
Một nhiều hơn điều: điều này không thay đổi Điều gì hiển thị lên trong Google Images. Google indexes
image trong của bạn src thuộc tính — responsive variants là chỉ phân phối. So
giữ của bạn alt text, filename, và main image URL giống nhau không quan trọng mà
version trình duyệt loads.
Muốn precise version — w so với x descriptors, Khi bạn thực ra cần
<picture>, LCP và layout-shift mechanics, và mobile-lập chỉ mục rules —
chuyển để Nâng cao tab.
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 imagesTL;DR — Hai jobs, một syntax family.
srcset/sizeson<img>= resolution switching (giống nhau image, trình duyệt picks best-fit — một suggestion);<picture>= art direction hoặc format switching (tác giả-dictated crops/formats — một command). Hầu hết các trang chỉ cần đó đầu tiên. Responsive images là không một trực tiếp tín hiệu xếp hạng và không thay đổi điều gì Google indexes — Google indexes đósrcURL, so giữalttext, filenames, và image URLs ổn định trên breakpoints (mobile-đầu tiên lập chỉ mục). Đó real payoff là Core Web Vitals: right-sizing images là đó cách sửa behind Lighthouse “Properly size images” (bản dịch) «Properly size images» audit (fails tại một 4KiB khoảng trống), vàwidth/height(hoặcaspect-ratio) — khôngsrcset— là điều gì ngăn CLS. không lazy-load đó LCP image. I reuse đó chính xácsrcset+width/heightpattern từ my Ahrefs CLS piece.
Hai khác jobs, một syntax family
Mọi thứ trong điều này topic collapses vào hai mechanisms, và half confusion out ở đó xuất hiện từ mixing them lên:
- Resolution switching — giống nhau image tại khác sizes hoặc pixel densities.
bạn cho trình duyệt menu với
srcsetvàsizeson<img>, và * trình duyệt* decides mà file để fetch dựa trên viewport và screen density. Đây là phổ biến case. - Art direction — genuinely khác image theo condition: wide crop on
desktop, tight vertical crop on mobile, hoặc khác file format hoàn toàn. Ở đây
bạn dictate lựa chọn với
<picture>element.
Đó web.dev Learn: Responsive images
course frames đó khác biệt chính xác right: với srcset đó trình duyệt nhận
suggestions, whereas “the picture element gives commands.” (bản dịch) «đó element cho commands.»
Và đó phạm vi-setting line worth tattooing on đó wall, cũng từ 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.” (bản dịch) «Bạn probably sẽ không cần để dùng đó element cho hầu hết of của bạn responsive images — đó và các thuộc tính on đó element cover một lot of dùng cases.»
Reach cho <picture> chỉ khi bạn thực ra cần một khác nhau image, không một khác nhau
size of đó giống nhau một.
srcset và sizes — resolution switching
w-descriptors so với. x-descriptors
srcset takes comma-separated list của candidate files, mỗi tagged với
descriptor. có hai kinds:
- Width descriptors (
w) — bạn state mỗi file intrinsic pixel width (puppy-2000.jpg 2000w). trình duyệt combines đó với của bạnsizesgiá trị để hoạt động out mà file best fits space và device pixel density. Đây là flexible option và Điều gì bạn’ll sử dụng phần lớn của time. - Pixel-density descriptors (
x) — bạn state mà file là cho mà device pixel ratio (logo.png 1x, logo@2x.png 2x). sử dụng những điều này cho fixed-size images ( avatar, logo) nơi displayed size không bao giờ thay đổi; bạn không cầnsizesvớixdescriptors.
Rule của thumb: fluid, nội dung-width images → w descriptors + sizes; fixed-size UI
images → x descriptors.
Vì sao sizes matters (và Điều gì happens nếu bạn skip nó)
Với w descriptors, sizes không tùy chọn cosmetics — đây là cách đó trình duyệt knows
cách big đó image sẽ render so điều này có thể chọn trước layout. Theo
web.dev, sizes “tells the browser
what size you expect the image to be displayed at under different conditions,” (bản dịch) «tells đó trình duyệt điều gì size bạn expect đó image để là displayed tại dưới khác nhau conditions,» as một
comma-separated list of media conditions và widths:
sizes="(max-width: 600px) 480px, 1000px"Đọc điều này as: “if the viewport is 600px or narrower, the image will be about 480px
wide; otherwise, about 1000px.” (bản dịch) «nếu đó viewport là 600px hoặc hẹp hơn, đó image sẽ là về 480px wide; nếu không, về 1000px.» Omit sizes và đó trình duyệt assumes đó image fills
đó đầy đủ viewport width (100vw) — so on một wide screen điều này có thể pull của bạn largest file
cho an image đó là thực ra kết xuất tại 400px, âm thầm defeating đó toàn bộ point.
Bị thiếu sizes là đó single hầu hết phổ biến srcset mistake.
không guess tại những điều đó width các giá trị từ layout trong của bạn head — kiểm tra image
thực tế được kết xuất CSS width trong trình duyệt (DevTools → Elements → computed
width của <img> box) tại mỗi breakpoint bạn care về, và đặt sizes để
match. sizes giá trị đó không match thực được kết xuất width vẫn gây ra
trình duyệt để select sai candidate mặc dù markup là syntactically
đúng — syntax validation alone sẽ không catch đó; see currentSrc kiểm tra
dưới để xác nhận Điều gì thực ra loaded.
Worked ví dụ (my reusable pattern)
Đây là chính xác pattern từ my Ahrefs piece,
Điều gì là Cumulative Layout Shift (CLS) & Cách Improve nó,
expanded với 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" />mỗi piece là load-bearing: src là fallback và URL Google indexes;
srcset lists candidates với w descriptors; sizes tells trình duyệt
được kết xuất width; width/height reserve space ( CLS khắc phục — nhiều hơn dưới); alt vẫn giữ
giống hệt không quan trọng mà file loads.
picture element — art direction và format switching
Khi bạn thực ra cần nó
sử dụng <picture> cho hai điều srcset có thể’t làm:
- Art direction — khác crop theo breakpoint. web.dev ví dụ: on hẹp phone bạn có thể phục vụ tall, tight crop; on wide desktop, ngắn, wide một. giống nhau subject, có chủ ý khác cách diễn đạt.
- Format switching — offer AVIF/WebP với JPEG fallback qua
<source type="…">, letting trình duyệt take đầu tiên format nó hỗ trợ. điều này ties straight lại để format hướng dẫn on Image SEO hub.
syntax (và fallback rule)
<picture> element wraps một hoặc nhiều hơn <source> elements và luôn ends với
đơn giản <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>Đó trailing <img src> không phải tùy chọn. Google trạng thái điều này trực tiếp: theo
section 4.8.1 of đó HTML Tiêu chuẩn,
“make sure that you provide an img element as a fallback with a src attribute when
using the picture element.” (bản dịch) «hãy bảo đảm đó bạn cung cấp an element as một fallback với một thuộc tính khi dùng đó element.» đây là đó file older các trình duyệt và các crawler fall lại để —
và, again, đó một Google indexes.
Làm srcset help SEO? Trực tiếp so với. gián tiếp
Ở đây cách diễn đạt mỗi competing hướng dẫn fumbles, so I’ll là blunt.
Trực tiếp: không. Responsive-image markup không phải tín hiệu xếp hạng way alt text hoặc
filenames là cho image tìm kiếm. Thêm srcset không lift của bạn positions, và nó
không thay đổi Điều gì xuất hiện trong Google Images. Google indexes image referenced trong
src; srcset/<picture> variants là phân phối mechanism, không riêng
indexable assets. giữ của bạn alt text, filename, và dữ liệu có cấu trúc attached để
đó chính src image.
Indirectly: có, và nó một của biggest levers bạn có. Right-sized images là phần lớn effective concrete khắc phục cho hai Core Web Vitals — LCP và CLS — mà feed Google trang-experience các tín hiệu. đó toàn bộ payoff. nó giống nhau shape as WebP/AVIF story on Image SEO hub: không boost cho format itself, win là speed.
Responsive images và LCP
Oversized images là một of đó hầu hết phổ biến gây ra of một chậm Largest Contentful Paint, và responsive images là Lighthouse được khuyến nghị cách sửa. Đó Lighthouse “Properly size images” (bản dịch) «Properly size images» audit lists “all images in your page that aren’t appropriately sized, along with the potential savings” (bản dịch) «all images trong trang của bạn đó không appropriately sized, along với đó potential savings» — bất cứ điều gì bigger hơn điều này cần để là “just results in wasted bytes and slows down page load time.” (bản dịch) «chỉ dẫn đến wasted bytes và làm chậm xuống trang load time.» Của nó cách sửa, 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.” (bản dịch) «Với responsive images, bạn generate multiple versions of mỗi image, và thì specify mà version để dùng trong của bạn HTML hoặc CSS dùng media các truy vấn, viewport dimensions, và so on.»
Hai specifics worth knowing:
- Đó failure ngưỡng là 4KiB. Lighthouse chỉ flags an image khi “the rendered size is at least 4KiB smaller than the actual size.” (bản dịch) «đó được kết xuất size là ít nhất 4KiB nhỏ hơn đó thực tế size.» Nhỏ overshoots không count; serving một 3000px file vào một 400px slot làm.
- MỘT tooling shortcut. Google khuyến nghị
RespImageLint,
“a helpful bookmarklet for identifying the optimal
srcsetandsizesvalues for your images.” (bản dịch) «một helpful bookmarklet cho identifying đó optimal và các giá trị cho của bạn images.» Chạy điều này trước khi bạn hand-calculate breakpoints.
không lazy-load LCP image
Này là đó rule mọi người break hầu hết. srcset on của bạn hero là fine — nhưng không bao giờ pair điều này
với lazy loading on đó LCP element. Đó largest trên-đó-fold image nên load
eagerly với fetchpriority="high", chính xác as đó Image SEO hub covers. Và khi bạn
xây dựng một responsive <picture> hero, giữ đó đổi logic native: Google warns đó điều này
“won’t load content that requires user interactions” (bản dịch) «sẽ không load nội dung đó requires người dùng interactions»
như swiping hoặc clicking, so một JS scheme đó chỉ loads đó real image sau
interaction hides điều này từ Google.
Responsive images và CLS
Ở đây đó trap: srcset alone làm không có gì cho layout shift. Resolution
switching decides mà file loads; điều này không reserve space cho điều này. Left không có
dimensions, theo web.dev Optimize CLS hướng dẫn,
“as images load, text shifts down the page to make room for them” (bản dịch) «as images load, text shifts xuống đó trang để làm room cho them» — vì đó space
“can’t be allocated for it until the browser starts to download it and can determine
its dimensions.” (bản dịch) «không thể là allocated cho điều này until đó trình duyệt bắt đầu để download điều này và có thể determine của nó dimensions.»
khắc phục là rõ ràng dimensions, và nó composes với responsive markup:
- Set
widthvàheightcác thuộc tính on đó<img>. Modern các trình duyệt “set the default aspect ratio of images based on an image’swidthandheightattributes,” (bản dịch) «set đó default aspect ratio of images dựa trên an image và các thuộc tính,» so những hai numbers reserve đó right box trước bất kỳsrcsetvariant downloads. (web.dev) - Pair them với
height: autotrong CSS cho fluid containers. Này là đó part đó looks contradictory nhưng không: web.dev own hướng dẫn là để “use CSS to resize the image to the width of the container” (bản dịch) «dùng CSS để resize đó image để đó width of đó container» và “setheight: auto;to avoid using a fixed value for the image height.” (bản dịch) «set để tránh dùng một fixed giá trị cho đó image height.» Đó HTML các thuộc tính set đó intrinsic aspect ratio; đó CSS lets đó image quy mô fluidly. Together they ngăn CLS và stay responsive. - Hoặc dùng CSS
aspect-ratiođể reserve đó space khi bạn không thể set các thuộc tính.
Mà kills một persistent myth: width/height các thuộc tính không break fluid layouts.
Các thuộc tính + height: auto là đó correct combo, không một conflict. I walk qua đó
đầy đủ layout-shift mechanics trong my Ahrefs CLS bài viết; phiên bản ngắn gọn là
“reserve the space so that there’s no shift” (bản dịch) «reserve đó space so đó có không shift»
và let đó image fill điều này.
Mobile-đầu tiên lập chỉ mục implications
Google indexes primarily mobile version của bạn các trang, so hai rules quan trọng nhiều hơn họ được sử dụng để Khi bạn’re serving responsive variants:
- Giữ alt text giống hệt trên breakpoints. Google mobile-đầu tiên lập chỉ mục thực hành tốt nhất chẳng hạn để “make sure that the mobile site has the same alt text for images as the desktop site.” (bản dịch) «hãy bảo đảm đó mobile site có đó giống nhau alt text cho images as đó desktop site.» An art-directed mobile crop không được drop context Google cần.
- Giữ image URLs ổn định. không dùng URLs đó thay đổi mỗi khi đó trang loads cho images. Này dovetails với Google Image SEO hướng dẫn để consistently reference đó image với đó giống nhau URL so điều này có thể bộ nhớ đệm và reuse điều này. Responsive CDNs đó mint một fresh URL theo yêu cầu break này — pin ổn định URLs cho của bạn variants.
Google own summary of vì sao để bother là đơn giản: “designing responsive web pages leads to better user experience, since people can access them across a plethora of device types.” (bản dịch) «designing responsive web các trang dẫn đến tốt hơn người dùng experience, since mọi người có thể access them trên một plethora of device types.»
Điều gì về Bing?
có không Bing-cụ thể hướng dẫn on srcset, sizes, hoặc <picture> element —
Bing công khai tài liệu không address responsive-image markup tại all. So không
justify responsive images với Bing algorithm hook; case là performance và UX,
mà Bing (như Google) xử lý as trang-experience quality thay vì điều gì đó bạn
có thể point tại được ghi lại responsive-images rule cho.
phổ biến mistakes
- Forgetting
sizesvớiwdescriptors — trình duyệt assumes100vwvà có thể fetch của bạn biggest file cho nhỏ slot. - Dropping fallback
srcon<picture>— spec violation Google explicitly flags, và nó URL các crawler chỉ mục. - Mismatched
width/heightratio so với. thực ra-phân phối image — vẫn gây ra shift. - Lazy-loading LCP hero — delays LCP cho không reason; load nó eagerly với
fetchpriority="high". - Regenerating image các URL theo yêu cầu — breaks Google bộ nhớ đệm và mobile-đầu tiên “ổn định URL” rule.
- Reaching cho
<picture>Khisrcset/sizessẽ làm — unnecessary complexity; web.dev nói phần lớn các trang không cần nó.
nơi điều này sits
Đây là sizing-và-phân phối deep dive dưới rộng hơn Image SEO hub —
performance half của image SEO, parallel để Cách alt text hướng dẫn xử lý
image-tìm kiếm half. cho layout-shift mechanics trong đầy đủ, see my Ahrefs CLS bài viết;
cho LCP, fetchpriority, và loading strategy, đó Core Web Vitals hoạt động
wearing image hat.
AI summary
condensed take on Nâng cao version:
- Hai mechanisms, một family.
srcset/sizeson<img>= resolution switching (giống nhau image, trình duyệt picks best-fit — một suggestion).<picture>= art direction hoặc format switching (tác giả-dictated crops/formats — một command). Hầu hết các trang chỉ cầnsrcset/sizes. - Descriptors:
w(width) descriptors cần mộtsizesthuộc tính và cover fluid, nội dung-width images;x(pixel-density) descriptors suit fixed-size UI images và không cầnsizes. sizeslà mandatory vớiwdescriptors — omit điều này và đó trình duyệt assumes100vw, thường fetching đó largest file needlessly. Hầu hết phổ biếnsrcsetmistake.- Không một trực tiếp tín hiệu xếp hạng. Responsive markup không thay đổi điều gì Google
indexes — Google indexes đó
srcURL; variants là phân phối chỉ. Giữ alt text, filenames, và dữ liệu có cấu trúc on đó chínhsrcimage. - LCP payoff: oversized images là một top nguyên nhân of chậm LCP; responsive images là
Lighthouse được khuyến nghị cách sửa (“Properly size images” (bản dịch) «Properly size images» audit; fails tại một 4KiB khoảng trống;
RespImageLint helps pick
srcset/sizes). - CLS:
srcsetalone làm không có gì cho shift — setwidth/heightcác thuộc tính (hoặc CSSaspect-ratio), paired vớiheight: autocho fluid layouts. Các thuộc tính +height: autolà đó correct combo, không một conflict. - Không bao giờ lazy-load đó LCP image; load điều này eagerly với
fetchpriority="high", và giữ responsive swaps native (Google sẽ không load interaction-triggered nội dung). - Mobile-đầu tiên lập chỉ mục: giống nhau alt text và ổn định image URLs trên breakpoints; không mint một fresh URL theo yêu cầu.
- Bing: không dedicated responsive-images hướng dẫn — justify đó hoạt động on performance/UX.
Tài liệu chính thức
Chính-nguồn tài liệu và nhà phát triển hướng dẫn.
Google — Tìm kiếm
- Google Images thực hành tốt nhất —
srcsetnote,<picture>fallback-srcrequirement, giống nhau-URL hướng dẫn, và responsive-design rationale. - Mobile-đầu tiên lập chỉ mục thực hành tốt nhất — giống nhau alt text và ổn định image các URL trên mobile và desktop; không lazy-load chính nội dung on interaction.
Google — web.dev (nhà phát triển hướng dẫn)
- Learn: Responsive images —
srcsetcomplementssrc; cáchsizesvà media conditions hoạt động. - Learn: Đó picture element — khi bạn cần
<picture>, art direction, và “suggestions vs. commands.” (bản dịch) «suggestions so với. commands.» - Optimize Cumulative Layout Shift —
width/heightcác thuộc tính, aspect ratio, vàheight: autocho fluid images. - Fetch Priority API —
fetchpriority="high"cho đó LCP image.
Chrome DevTools / Lighthouse
- Properly size images (dùng-responsive-images) — audit, 4KiB ngưỡng, và RespImageLint bookmarklet.
MDN
- sử dụng responsive images trong HTML — reference cho
srcset,sizes,w/xdescriptors, và<picture>syntax.
Quotes từ nguồn
On—record statements từ tài liệu củ Google và nhà phát triển hướng dẫn. nơi trang exposes text, link là deep link đó jumps để quoted passage.
Google — Image SEO tài liệu
- “The
srcsetattribute allows specifying different versions of the same image, specifically for different screen sizes.” (bản dịch) «Đó thuộc tính cho phép specifying khác nhau versions of đó giống nhau image, cụ thể cho khác nhau screen sizes.» Nhảy đến trích dẫn - “Designing responsive web pages leads to better user experience, since people can access them across a plethora of device types.” (bản dịch) «Designing responsive web các trang dẫn đến tốt hơn người dùng experience, since mọi người có thể access them trên một plethora of device types.» Nguồn
- On giữ image URLs ổn định: “consistently reference the image with the same URL, so that Google can cache and reuse the image.” (bản dịch) «consistently reference đó image với đó giống nhau URL, so đó Google có thể bộ nhớ đệm và reuse đó image.» Nhảy đến trích dẫn
Google — Mobile-đầu tiên lập chỉ mục tài liệu
- “Make sure that the mobile site has the same alt text for images as the desktop site.” (bản dịch) «Hãy bảo đảm đó mobile site có đó giống nhau alt text cho images as đó desktop site.» Nhảy đến trích dẫn
- “Don’t use URLs that change every time the page loads for images.” (bản dịch) «không dùng URLs đó thay đổi mỗi khi đó trang loads cho images.» Nhảy đến trích dẫn
web.dev — srcset so với. picture
- “Where the
srcsetattribute gives suggestions to the browser, thepictureelement gives commands.” (bản dịch) «Nơi đó thuộc tính cho suggestions để đó trình duyệt, đó element cho commands.» Nhảy đến trích dẫn
Me — on CLS khắc phục
- “Reserve the space so that there’s no shift” (bản dịch) «Reserve đó space so đó có không shift» và đó image đơn giản fills điều này. Nhảy đến trích dẫn
<picture> element / srcset cho image SEO để John Mueller, nhưng không chính
transcript với citable wording là locatable, so I’ve left nó out thay vì quote
nó. Không Bing-cụ thể responsive-images hướng dẫn tồn tại để quote. web.dev,
Lighthouse, và Google-doc quotes trên là từ trực tiếp, deep-linkable các trang. Sao chép và dán responsive image patterns
Three patterns đó cover gần như mỗi thực case. tất cả them giữ fallback src,
rõ ràng width/height ( CLS khắc phục), và giống hệt alt text.
1. Resolution switching — đó everyday <img srcset> (my reusable pattern)
<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" />Pair điều này với này CSS so đó image scales fluidly không có causing layout shift:
img {
max-width: 100%;
height: auto; /* with width/height attributes set, this prevents CLS */
}2. Fixed-size image — pixel-density (x) descriptors, không sizes needed
<img
src="logo.png"
srcset="logo.png 1x, logo@2x.png 2x"
width="200" height="60"
alt="Company logo" />3. Đó LCP hero — eager + cao priority, và format fallback qua <picture>
<picture>
<source type="image/avif" srcset="hero.avif">
<source type="image/webp" srcset="hero.webp">
<img
src="hero.jpg"
width="1200" height="675"
fetchpriority="high"
loading="eager"
alt="Product hero shot" />
</picture>Rules baked vào những điều này:
- luôn giữ trailing
<img src>— nó fallback và URL Google indexes. - với
wdescriptors,sizeslà bắt buộc; không có nó trình duyệt assumes100vw. - với
xdescriptors, omitsizes. - không bao giờ thêm
loading="lazy"để LCP image (pattern 3) — đó delays LCP; sử dụngfetchpriority="high"thay vì.
để tìm right srcset/sizes các giá trị thay vì guessing, chạy
RespImageLint,
bookmarklet Google khuyến nghị, over trực tiếp trang.
Mà một làm bạn thực ra cần: srcset/sizes hoặc picture?
Hoạt động qua thực branch bài viết draws giữa resolution switching và art direction / format switching.
srcset/sizes vs. the picture element
phần lớn các trang dừng tại đầu tiên branch và không bao giờ leave srcset/sizes — reach cho
<picture> chỉ Khi image itself cần để thay đổi, không chỉ của nó size.
Điều gì không để làm
Six concrete mistakes bài viết calls out, mỗi với Vì sao nó sai và khắc phục.
- Forgetting
sizesthuộc tính vớiw-descriptors. Vì sao nó sai: không cósizes, trình duyệt assumes image fills đầy đủ viewport width (100vw), so nó có thể fetch của bạn biggest candidate cho slot đó renders nhiều nhỏ hơn. Làm thay vì: luôn pairw-descriptorsrcsetvớisizesgiá trị đó matches Cách image thực ra renders tại mỗi breakpoint. - Dropping fallback
<img src>on<picture>. Vì sao nó sai: nó spec violation Google explicitly flags, và nó URL các crawler và Google Images thực ra chỉ mục — không fallback có nghĩ là không indexable image. Làm thay vì: mỗi<picture>block ends với đơn giản<img src="...">, không bao giờ chỉ<source>elements. - Mismatched
width/heightratio versus thực ra-phân phối image. Vì sao nó sai: các thuộc tính đặt reserved aspect ratio; nếu thực image không match nó, layout vẫn shifts sau khi image loads. Làm thay vì: giữwidth/height(hoặcaspect-ratio) ratio giống hệt để image files bạn’re thực ra serving trên allsrcset/<picture>variants. - Lazy-loading LCP hero image. Vì sao nó sai:
loading="lazy"delays fetch của chính xác image Largest Contentful Paint là timing, pushing LCP sau đó cho không benefit. Làm thay vì: load LCP image eagerly vớifetchpriority="high", không bao giờloading="lazy". - Regenerating image các URL on mỗi yêu cầu. Vì sao nó sai: nó breaks Google image bộ nhớ đệm và violates mobile-đầu tiên lập chỉ mục rule so với các URL đó thay đổi mỗi time trang loads. Làm thay vì: pin ổn định, consistent các URL theo image variant so Google có thể bộ nhớ đệm và reuse them.
- Reaching cho
<picture>Khisrcset/sizessẽ làm. Vì sao nó sai: nó unneeded complexity — web.dev là rõ ràng đó phần lớn các trang không cần<picture>tại all. Làm thay vì: default đểsrcset/sizesresolution switching; chỉ thêm<picture>Khi bạn genuinely cần khác crop hoặc format theo condition.
phổ biến các vấn đề
Symptom-đầu tiên lookup cho hai responsive-image các vấn đề readers thực ra hit.
Layout vẫn shifts (CLS) mặc dù srcset là thiết lập
- Symptom: Lighthouse hoặc PageSpeed Insights vẫn các báo cáo layout shift on
image, hoặc Bạn có thể visually see nội dung jump as image loads, despite có
hoạt động
srcset/sizes. - có khả năng nguyên nhân:
srcsetalone làm không có gì cho layout shift — nó chỉ decides mà file loads, không Cách nhiều space để reserve. image có khôngwidth/heightcác thuộc tính (hoặc CSSaspect-ratio), so trình duyệt có thể’t allocate space cho đến khi file bắt đầu downloading và của nó dimensions là known. - khắc phục + xác nhận: Thêm rõ ràng
widthvàheightcác thuộc tính để<img>(hoặc đặt CSSaspect-ratio), và pair them vớiheight: autotrong CSS so image vẫn scales fluidly. xác nhận by re-đang chạy CLS kiểm tra (Lighthouse hoặc trực tiếp CrUX kiểm tra) và watching cho shift để disappear — reserved box nên hiện tại hold của nó size trước khi image finishes loading.
Lighthouse flags “Properly size images” (bản dịch) «Properly size images» / an oversized image là loading
- Symptom: Đó Lighthouse “Properly size images” (bản dịch) «Properly size images» audit lists một hoặc hơn images với wasted bytes, hoặc một trang feels chậm để reach của nó Largest Contentful Paint ngay cả though đó image itself looks fine.
- Có khả năng nguyên nhân: Đó được kết xuất size of đó image là ít nhất 4KiB nhỏ hơn đó
thực tế file đang phân phối — commonly vì có không
srcsettại all, hoặcsizeslà bị thiếu so đó trình duyệt defaulted để100vwvà pulled đó largest candidate cho một nhỏ slot. - Cách sửa + xác nhận: Thêm (hoặc correct)
srcsetvới appropriately sized candidates và mộtsizesgiá trị matching đó real được kết xuất width; chạy RespImageLint để kiểm tra đó các giá trị thay vì guessing. Xác nhận by re-đang chạy đó Lighthouse “Properly size images” (bản dịch) «Properly size images» audit và kiểm tra đó flagged image không lâu hơn xuất hiện (hoặc của nó potential savings drop dưới đó 4KiB ngưỡng).
Bảng tra nhanh: descriptors, picture so với. srcset, và CLS/LCP rules
| Situation | Dùng | Notes |
|---|---|---|
| Fluid, nội dung-width image đó scales với của nó container | srcset với w-descriptors + sizes | sizes là mandatory — omit điều này và đó trình duyệt assumes 100vw |
| Fixed-size image (logo, avatar, icon) | srcset với x-descriptors | Không sizes needed — displayed size không bao giờ thay đổi |
| Giống nhau image, khác nhau được kết xuất size | srcset/sizes on <img> (resolution switching) | MỘT suggestion để đó trình duyệt — điều này picks đó best-fit file |
| Khác nhau crop/cách diễn đạt theo breakpoint | <picture> với <source media="..."> (art direction) | MỘT command — bạn dictate mà image loads |
| Modern format (AVIF/WebP) với fallback | <picture> với <source type="..."> | Luôn end với một đơn giản <img src> fallback |
| Ngăn CLS | width/height các thuộc tính hoặc CSS aspect-ratio, plus height: auto cho fluid layouts | srcset alone làm không ngăn layout shift |
| Cách sửa chậm LCP từ an oversized image | Right-sized srcset/sizes | Đó Lighthouse “Properly size images” (bản dịch) «Properly size images» cách sửa; fails tại một 4KiB khoảng trống |
| LCP hero image loading strategy | fetchpriority="high", không bao giờ loading="lazy" | Lazy-loading đó LCP element delays LCP cho không reason |
| Điều gì Google indexes | Đó src URL chỉ | srcset/<picture> variants là phân phối, không riêng được lập chỉ mục assets |
Validation các kiểm thử
Truyền/fail kiểm tra đó xác nhận responsive-image thay đổi thực ra took effect.
Properly-sized images ship correctly
Kiểm thử để chạy: Chạy đó Lighthouse “Properly size images” (bản dịch) «Properly size images» audit (Chrome DevTools >
Lighthouse > Performance, hoặc PageSpeed Insights) on đó trang bạn chỉ đã cập nhật.
Dự kiến kết quả: Đó image bạn fixed không lâu hơn xuất hiện trong đó audit flagged
list, hoặc của nó potential savings drop dưới đó 4KiB khoảng trống ngưỡng. Failure
interpretation: Nếu đây là vẫn flagged, either srcset candidates là vẫn cũng
lớn cho đó được kết xuất size, hoặc sizes là bị thiếu/sai và đó trình duyệt là vẫn
dùng mặc định để 100vw. Monitoring window: Immediate — re-chạy right sau
deploying đó thay đổi. Rollback trigger: Đó audit vẫn flags đó giống nhau image với
unchanged potential savings sau confirming sizes matches đó real được kết xuất width.
srcset/sizes các giá trị là thực ra optimal
Kiểm thử để chạy: Chạy
RespImageLint
( bookmarklet Google khuyến nghị) so với trực tiếp trang. Dự kiến kết quả: Không
warnings về oversized candidates hoặc bị thiếu/không đúng sizes giá trị.
thất bại interpretation: warning có nghĩ là either candidate là unnecessarily lớn
cho của nó breakpoint hoặc sizes không match container thực được kết xuất width.
Monitoring window: Immediate, on trực tiếp URL sau khi deploy. Rollback trigger:
RespImageLint giữ flagging giống nhau image sau khi bạn’ve corrected sizes giá trị.
trình duyệt thực ra được chọn candidate bạn dự kiến
Kiểm thử để chạy: Passing đó Lighthouse/RespImageLint kiểm tra xác nhận của bạn markup là
hợp lệ — điều này không xác nhận đó trình duyệt picked đó file bạn dự kiến tại một được cho
viewport. Load đó trang tại đó breakpoint bạn care về, open DevTools, select đó
<img>, và kiểm tra của nó
currentSrc
thuộc tính trong đó console ($0.currentSrc trong Chrome/Firefox DevTools) — MDN’s own
ví dụ làm chính xác này: comparing currentSrc so với đó dự kiến filename để
xác nhận mà candidate loaded. Cross-kiểm tra so với đó Network tab để see mà
file đã là thực ra requested. Repeat tại của bạn hẹp và wide breakpoints, và tại 1x
và 2x device pixel ratio nếu bạn emulate điều này.
Dự kiến kết quả: currentSrc matches đó candidate file bạn’d expect cho đó
viewport width và pixel density, và đó Network tab cho thấy chỉ đó file đã là
fetched. Failure interpretation: Nếu currentSrc trả về một candidate bigger hoặc
nhỏ hơn dự kiến, either đó sizes giá trị không match đó image thực tế
được kết xuất CSS width (see “Why sizes matters” (bản dịch) «Vì sao sizes matters» trong đó Advanced tab) hoặc một srcset
descriptor là sai. Note đó trình duyệt selection là implementation-được định nghĩa — đó
HTML Tiêu chuẩn leaves đó chính xác lựa chọn among hợp lệ candidates để đó trình duyệt
(density, zoom, và network conditions có thể all factor trong), so không expect
byte-giống hệt behavior trên các trình duyệt; kiểm tra cho “reasonable candidate,” (bản dịch) «reasonable candidate,» không một
fixed câu trả lời. Monitoring window: Immediate — kiểm tra theo breakpoint right sau
deploying. Rollback trigger: currentSrc giữ returning an oversized candidate
tại một hẹp viewport sau khi bạn’ve corrected sizes để match đó real được kết xuất
width.
Không layout shift từ image
Kiểm thử để chạy: kiểm tra Cumulative Layout Shift cho trang — Lighthouse CLS score,
hoặc trực tiếp CrUX/PageSpeed Insights trường-dữ liệu kiểm tra cho URL. Dự kiến kết quả:
CLS có thể gán để image drops để (near) zero sau khi width/height (hoặc
aspect-ratio) là đặt. thất bại interpretation: Persistent shift thường có nghĩ là
width/height ratio không match thực tế image là phân phối, hoặc các thuộc tính
là bị thiếu hoàn toàn. Monitoring window: Lab dữ liệu là immediate; trường dữ liệu (CrUX)
cần về 28 days để accumulate trustworthy trend. Rollback trigger: CLS trong
trường dữ liệu không improve sau khi 28 days despite lab kiểm tra passing — revisit
liệu phân phối image ratio thực ra matches reserved box.
Tự kiểm tra: Responsive images
Five nhanh các câu hỏi on srcset, sizes, <picture>, và Cách responsive images
ảnh hưởng SEO. Pick câu trả lời cho mỗi, sau đó kiểm tra.
Nhật ký thay đổi
Đã cập nhật 8 thg 8, 2026.
Tóm tắt biên tập và chi tiết thay đổi đã ghi nhận.Chi tiết thay đổi
-
Ghi chú thay đổi chi tiết hiện chỉ có bằng tiếng Anh.
Không thể so sánh đầy đủ — không có bản lưu trước đó cho lần sửa đổi này.
Đã cập nhật 18 thg 7, 2026.
Tóm tắt biên tập và chi tiết thay đổi đã ghi nhận.Chi tiết thay đổi
-
Ghi chú thay đổi chi tiết hiện chỉ có bằng tiếng Anh.
-
Ghi chú thay đổi chi tiết hiện chỉ có bằng tiếng Anh.
Không thể so sánh đầy đủ — không có bản lưu trước đó cho lần sửa đổi này.