👀 Live Preview
sizes with three srcset candidates. Resize the window to change the layout slot:
Below 600px the slot is 100vw; up to 1200px it is 50vw; above that, 120px. The label on the SVG shows which file the browser selected.

The sizes attribute tells the browser how wide an image will be displayed in the page layout at different viewport widths. It works together with srcset on <img> and <source> so the browser can download a file that matches the rendered size—saving bandwidth and improving performance on phones, tablets, and desktops.
Unlike the form size attribute on <select> and <input>, sizes is part of the responsive images feature set. It does not set CSS dimensions by itself; it is a hint for resource selection when multiple image files are available.
Layout width hints.
Pick the right file.
Breakpoint syntax.
Forms vs images.
On source too.
Dynamic updates.
sizesThe primary purpose of the sizes attribute is to describe the rendered width (the “slot”) of an image in your layout. When you provide several files in srcset with width descriptors such as 400w and 800w, the browser needs to know how large the image will appear on screen to pick the best candidate—especially on high-DPI (Retina) displays.
Without sizes, browsers assume the image spans the full viewport width (100vw), which can cause unnecessarily large downloads when your CSS actually shows a smaller image (for example, a 300px sidebar thumbnail).
sizes is for responsive images on <img> and <source> with srcset. It is unrelated to the integer size attribute on form controls or to CSS width/height properties (though your CSS layout should match what you declare in sizes).
Pair sizes with srcset width descriptors on <img>:
<img
src="photo-800.jpg"
alt="Team photo"
srcset="photo-400.jpg 400w,
photo-800.jpg 800w,
photo-1200.jpg 1200w"
sizes="(max-width: 600px) 100vw,
(max-width: 1200px) 50vw,
300px">(media-query) length — e.g. (max-width: 600px) 100vw.px, vw, vh, em, rem, or % (relative to the viewport for percentages in this context).srcset containing w width descriptors for responsive selection.<source> inside <picture> when that source has srcset.100vw.The sizes attribute accepts a source-size list. Common patterns:
(max-width: 600px) 100vw — Full viewport width on small screens.(max-width: 1200px) 50vw — Half the viewport on medium layouts (e.g. two-column grid).300px — Fixed 300px slot as the final fallback on large screens.(min-width: 900px) 33vw, 100vw — One-third width on wide screens, full width otherwise.auto — On <source>, can defer to intrinsic sizing in some cases (use with care).<!-- Mobile full width, tablet half, desktop fixed -->
sizes="(max-width: 600px) 100vw,
(max-width: 1200px) 50vw,
300px"
<!-- Card grid: 1 col mobile, 3 cols desktop -->
sizes="(min-width: 1024px) 33vw, 100vw"| Topic | Detail | Example |
|---|---|---|
| Used on | <img>, <source> | With srcset |
| Purpose | Rendered width hint | Resource selection |
| Format | Media + length list | (max-width: 600px) 100vw, 50vw |
| Default | 100vw if omitted | Full viewport assumed |
| Pair with | srcset width (w) | photo.jpg 800w |
| JavaScript | img.sizes = "..." | Runtime layout changes |
| Element | Supported? | Notes |
|---|---|---|
<img> | Yes | Most common use; pair with srcset |
<source> | Yes | Inside <picture> with srcset |
<video> | No | Use media on source for format/art direction |
<select>, <input> | No | Use form size instead (different attribute) |
<link> preload | Related | Preload images can include imagesizes (similar idea) |
size vs sizes vs srcset| Feature | size | sizes | srcset |
|---|---|---|---|
| Used on | select, input | img, source | img, source |
| Purpose | Visible rows or char width | Display slot width hint | List of image candidates |
| Value type | Integer | Media + length list | URLs with w or x |
| Works together? | No | Yes — with srcset | Yes — with sizes |
Responsive img with srcset, dynamic JavaScript updates, and picture with source sizes.
sizes with three srcset candidates. Resize the window to change the layout slot:
Below 600px the slot is 100vw; up to 1200px it is 50vw; above that, 120px. The label on the SVG shows which file the browser selected.
Define layout slots so the browser picks an appropriate file from srcset:
<img
src="hero-800.jpg"
alt="Hero banner"
srcset="hero-400.jpg 400w,
hero-800.jpg 800w,
hero-1200.jpg 1200w"
sizes="(max-width: 600px) 100vw,
(max-width: 1200px) 50vw,
300px">The browser evaluates media conditions top to bottom. The first match sets the slot width. It multiplies by device pixel ratio and compares against srcset widths to choose the smallest adequate file.
Update img.sizes when layout changes at runtime:
<img id="dynamicImage" src="photo.jpg" alt="..."
srcset="photo-400.jpg 400w, photo-800.jpg 800w"
sizes="800px">
<script>
var image = document.getElementById("dynamicImage");
function updateSizes() {
if (window.innerWidth < 600) {
image.sizes = "100vw";
} else if (window.innerWidth < 1200) {
image.sizes = "50vw";
} else {
image.sizes = "300px";
}
}
updateSizes();
window.addEventListener("resize", updateSizes);
</script>The sizes IDL attribute reflects the content attribute. Changing it after layout shifts (resize, panel toggle, orientation change) keeps downloads aligned with the actual display size.
Use sizes on a <source> inside <picture> for art direction or resolution switching:
<picture>
<source
media="(min-width: 700px)"
srcset="banner-wide.jpg 800w, banner-wide@2x.jpg 1600w"
sizes="(min-width: 1200px) 800px, 50vw">
<img src="banner-narrow.jpg" alt="Promotional banner">
</picture>media on source picks which candidate set applies. sizes on that same source then guides width-based selection within the matched set.
sizes affects which file loads, not accessibility text. Every <img> still needs a meaningful alt.picture, ensure essential information is available at all breakpoints.width and height (or aspect-ratio in CSS) so images reserve space while loading.alt="" for purely decorative responsive images; sizes works the same.srcset with width descriptors.
sizes media + length list.
Slot × DPR vs srcset widths.
Sharp image, less waste.
The sizes attribute with srcset width descriptors is supported in all modern browsers.
Chrome, Firefox, Safari, and Edge all honor sizes with srcset.
Bottom line: Use sizes with srcset for production responsive images; always keep a fallback src.
sizes to your real CSS layout (grid columns, sidebar width, etc.)sizes with srcset width descriptorssrc for older browserswidth/height to prevent layout shiftsizes with form sizesizes resizes the image visually (CSS does that)sizes when CSS makes images smaller than 100vwsrcset only—offer multiple widthsalt text on responsive imagesThe sizes attribute is a valuable tool for responsive web design. It tells the browser how wide an image will appear so it can choose an efficient file from srcset.
By specifying accurate slot sizes across viewport breakpoints, you deliver sharp images without wasting bandwidth. Combine sizes, srcset, and CSS layout for production-ready responsive images—and remember it is completely separate from the form size attribute.
sizesBookmark these before your next responsive image.
Display width
PurposeWidth descriptors
PairingBreakpoint slots
SyntaxDynamic layout
DynamicForms differ
Comparesrcset.<img> and <source> when used with responsive srcset width descriptors.sizes describes the slot; srcset lists the files to choose from.size on forms controls visible rows or character width. sizes on images describes responsive layout slots.img.sizes when layout changes, such as on resize or when toggling a sidebar.src for older user agents.Practice sizes with srcset in the Try It editor.
5 people found this page helpful