How does the sizes attribute work with srcset in HTML5?
The sizes attribute is used in conjunction with the srcset attribute to help the browser select the most suitable responsive image according to the page layout size and device characteristics; 1. srcset provides multiple image files and width (w) or pixel density (x) descriptors; 2. sizes inform the browser of the layout width of the image under different viewports through media query and viewport units (such as vw); 3. The browser calculates the rendering width of the image based on the device pixel ratio, and selects the smallest suitable image to meet the layout requirements from the srcset based on the device pixel ratio; 4. When srcset uses the w descriptor, sizes must be provided, otherwise the browser cannot judge the layout size; 5. If srcset only uses x descriptors (such as 1x, 2x), sizes do not need sizes; 6. sizes should reflect the CSS layout, such as "(max-width: 600px) 100vw, 50vw" means the full width of the small screen and the half width of the large screen; in short, sizes provide layout information, and srcset provides resource options, and the two jointly achieve efficient and intelligent image loading.

The sizes attribute works together with the srcset attribute in HTML5 to help browsers choose the most appropriate image from a set of responsive images, based on the layout size of the image on the page and the device characteristics like screen width and pixel density.

Here's how they work together:
Understanding srcset and sizes
The srcset attribute lists multiple image files along with descriptors—either width ( w ) or pixel density ( x )—so the browser can pick the best one. When you use width descriptors ( w ), the sizes attribute becomes necessary to tell the browser how much space the image will take up on the page at different viewport sizes.

Example:
<img src="/static/imghw/default1.png" data-src="small.jpg" class="lazy"
srcset="small.jpg 500w, medium.jpg 1000w, large.jpg 1500w"
sizes="(max-width: 600px) 100vw, (max-width: 900px) 50vw, 33vw"
alt="Responsive image">How the browser selects the image
The browser checks the
sizesattribute to determine the rendered width of the image based on the current viewport.- If the viewport is 500px wide →
100vw= 500px - If the viewport is 800px wide →
50vw= 400px - If the viewport is 1200px wide →
33vw≈ 396px
- If the viewport is 500px wide →
It uses this layout width to evaluate which image in the
srcsetbest matches that size, considering device pixel ratio (eg, Retina screens may prefer higher-density images).
It selects the smallest image in
srcsetthat is at least as wide as the calculated layout size (after accounting for pixel density). This minimizes download size while maintaining quality.
Key points about sizes
-
sizescontain a list of media conditions and length values . - Each media condition (like
(max-width: 600px)) is optional. If none match, the last length is used. - The length can be in
vw,px,%, etc., butvwis most common for responsive layouts. - It does not control styling—it only informs the browser about layout size for image selection.
When you don't need sizes
If
srcsetuses pixel density descriptors (1x,2x), like:srcset="image.jpg, image-2x.jpg 2x, image-3x.jpg 3x"
Then
sizesis not required , because the browser picks based on screen resolution, not layout width.If you're using
srcsetwithwdescriptors, always includesizes, or the browser won't know how to choose.
Practical tip
Use sizes to reflect your CSS layout:
<!-- On mobile: full width; on desktop: half width --> sizes="(max-width: 600px) 100vw, 50vw"
This tells the browser: “This image will be the full viewport width on small screens, and half on larger ones.”
So, in short:
sizes tells the browser how big the image will be in the layout.
srcset tells the browser what image files are available .
Together, they enable smart, efficient image loading.
Basically, without sizes , the browser can't make good decisions when you're offering different-width images.
The above is the detailed content of How does the sizes attribute work with srcset in HTML5?. For more information, please follow other related articles on the PHP Chinese website!
Hot AI Tools
Undress AI Tool
Undress images for free
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undresser.AI Undress
AI-powered app for creating realistic nude photos
ArtGPT
AI image generator for creative art from text prompts.
Stock Market GPT
AI powered investment research for smarter decisions
Hot Article
Popular tool
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
20519
7
13632
4
How to create a progress bar for file uploads in HTML5? (Progress tag)
Mar 06, 2026 am 02:22 AM
Why can't the tag directly display the upload progress? It is a read-only visual component. It does not listen to network requests and is not automatically bound to the upload process of XMLHttpRequest or fetch. If you put it in and don't update the value manually, it will always stop at 0%. What really drives it is the event monitoring in the upload logic you write yourself. A common mistake is to only monitor onload (upload completed) but miss upload.onprogress. XMLHttpRequest (not fetch) must be used to obtain real-time upload progress, because fetch does not expose the max attribute of the event in the upload phase and must be set to the file size (file.size
How to create a tooltip using only HTML5? (Title attribute)
Mar 06, 2026 am 12:23 AM
The title attribute is not a tooltip component, but an accessibility prompt mechanism implemented by the browser. The behavior, style, and interaction are uncontrollable and are only suitable for simple scenarios such as pure information supplement.
How to center an image vertically in HTML5? (Layout techniques)
Mar 07, 2026 am 02:05 AM
Flexbox is the most stable for centered images. The key is to set display:flex and align-items:center in the parent container and specify the height; using place-items:center for Grid is more concise; absolute positioning requires top:50% with transform:translateY(-50%); vertical-align is invalid for block-level centering.
How to create a simple offline web app with HTML5? (Application Cache)
Mar 06, 2026 am 02:16 AM
ApplicationCache has been completely abandoned and will be removed from Chrome 61, Firefox 72, and Safari 11.1. ServiceWorker must be used instead; the latter requires HTTPS, manual registration and cache control, and the path, scope, and life cycle must match exactly.
How to create a contact form with validation in HTML5? (Required attribute)
Mar 06, 2026 am 02:06 AM
required only verifies that it is not empty, not the format; type="email" or pattern must be used together; native verification is only triggered when submitting, not real-time; checkbox/radio/select/textarea has special behavior; the server must re-verify and clean empty values.
How to create a collapsible details section in HTML5? (Summary tag)
Mar 06, 2026 am 02:25 AM
Use and implement native folding area. HTML5 natively supports folding and expansion. It can work without JS. It is a container and a click area. The browser adds a small triangle by default. Click it to expand/collapse the content. A common mistake is to write a closing tag (for example), which must have a start and end tag, and must be the first child element of , otherwise the folding logic will fail. The default is the collapsed state; add the open attribute to expand it by default: you can put text, icons, and even text inside, but don't nest another one - some browsers have inconsistent behavior and do not support IE. Edge79, Chrome12, Firefox49, and Safari6 have good support.
How to use the dialog element for modals in HTML5? (Native popups)
Mar 06, 2026 am 01:26 AM
The element does not render by default, does not occupy the layout, and is not recognized by screen readers. It must explicitly set the open attribute or call showModal()/show() to activate; to close, you need to call close(), click mask or press Esc to monitor manually; the old version of Safari needs to be polyfilled or downgraded; form submission will not automatically close and must be intercepted and manually controlled.
How to preload video or audio content in HTML5? (Preload attribute)
Mar 06, 2026 am 01:59 AM
What are the values of the preload attribute, and what are their effects? The preload of HTML5 is a suggestive attribute. The browser is not forced to comply with it, but it will affect the initial resource loading strategy. It has only three legal values: auto, metadata, none. auto: It is recommended that the browser downloads the entire media file (including audio and video data) as soon as possible, which is suitable for scenarios where users are likely to play it, such as carousel videos on the homepage; but it will waste bandwidth, especially on mobile terminals or under weak networks. metadata: only prefetch meta information such as duration, size, frame rate, cover image, etc., without loading the actual audio and video frames, which is a safe choice in most cases none: explicitly tell the browser "don't load it yet" and wait until the user triggers playback (such as clicking) before starting it






