HTML sizes Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The sizes attribute tells the browser the intended display width of an <img> (or <source>) when srcset uses width descriptors (500w, 1000w, …).

Write a comma-separated list of media condition + length, ending with a default length (for example (max-width: 600px) 100vw, 800px). Default if omitted: 100vw. Not the same as the form size attribute.

Baseline · Widely available.

Not deprecated. Stable with responsive images across modern browsers (widely since about March 2016).

Only with w descriptors.

MDN: put sizes when srcset uses width descriptors. Density descriptors (1x/2x) do not use sizes.

HTML Syntax

syntax.html
<img srcset="…" sizes=" source-size-list" …>

A source-size list is comma-separated entries like (max-width: 600px) 100vw, ending with a bare length such as 50vw or 800px.

Attribute Values

Common patterns:

PatternDescriptionTry it
(max-width: …) length, lengthMedia conditions, then a default widthTry it
100vw / 50vwRelative to the viewport widthTry it
800pxFixed layout slot (common for max content width)Try it
auto, …Use layout width (with lazy load); then fallbacksTry it

Live Preview

Change the sizes hint. The labeled SVG candidates show which srcset file the browser picked (currentSrc). Resize the window and re-apply to explore.

Demo uses SVG data-URLs as 500w / 1000w / 1500w candidates. DPR also affects the pick.

Image
Responsive sizes demo strip

HTML Examples

Short study snippets. Use Try It Yourself for a live editor.

Example 1 — Media Conditions + Default

Full-bleed on small screens; fixed max slot on larger ones.

HTML
<img
  src="hero-1500.jpg"
  srcset="hero-500.jpg 500w, hero-1000.jpg 1000w, hero-1500.jpg 1500w"
  sizes="(max-width: 600px) 100vw, 800px"
  width="1500"
  height="800"
  alt="Hero banner">
Try It Yourself

How It Works

  1. srcset lists files by intrinsic width (w).
  2. sizes says: under 600px viewport, image is ~full viewport; otherwise ~800px wide.
  3. Browser multiplies that slot by device pixel ratio, then picks a candidate.
  4. width/height help reserve layout space and aspect ratio.

Example 2 — Half the Viewport

When the image always sits in a ~50% column.

HTML
<img
  src="card-1000.jpg"
  srcset="card-500.jpg 500w, card-1000.jpg 1000w, card-1500.jpg 1500w"
  sizes="50vw"
  width="1000"
  height="600"
  alt="Article card image">
Try It Yourself

How It Works

  1. sizes="50vw" means “about half the viewport wide.”
  2. Match your CSS — if the column is really 50%, the hint is accurate.
  3. Wrong sizes → browser may download a file that is too large or too soft.
  4. Keep src as a sensible fallback for older browsers.

Example 3 — sizes="auto" with Lazy Loading

Let layout decide the slot width; add fallbacks for older browsers.

HTML
<img
  src="photo-1500.jpg"
  srcset="photo-500.jpg 500w, photo-1000.jpg 1000w, photo-1500.jpg 1500w"
  sizes="auto, (max-width: 600px) 100vw, 800px"
  loading="lazy"
  width="1500"
  height="1000"
  alt="Photo in a CSS-sized container">
Try It Yourself

How It Works

  1. auto uses the concrete layout width after CSS.
  2. loading="lazy" — MDN expects lazy loading so layout exists before selection.
  3. Fallbacks after auto help browsers that do not understand auto.
  4. width/height still reserve space and reduce layout shift.

Edge Cases & Pitfalls

Most mistakes are wrong layout hints or mixing descriptor types.

pair

sizes needs w descriptors

If srcset only has 1x/2x, skip sizes. Do not mix w and x in one srcset.

layout

Match real CSS width

If CSS shows the image at 400px but sizes says 100vw, the browser may over-fetch.

dpr

Device pixel ratio multiplies the slot

A 400px slot on a 2× screen often needs an ~800w file. That is expected — not a bug.

name

sizes ≠ size

size is for form controls. sizes is for responsive images.

auto

auto needs lazy + fallbacks

Use loading="lazy" and list classic sizes after auto for older browsers. Support for auto is newer than basic sizes.

Browser Support

sizes is an HTML attribute for responsive img/source selection with srcset width descriptors.

✓ Baseline · Widely available

sizes

Widely available since about March 2016 for classic source-size lists. The auto keyword is newer and intended with lazy loading.

96% Global support (approx.)
Google Chrome All modern
Full support
Mozilla Firefox All modern
Full support
Apple Safari All modern
Full support
Microsoft Edge All modern
Full support
Opera All modern
Full support
Samsung Internet Modern
Full support
Internet Explorer No support
Not supported
sizes Widely supported

Bottom line: Safe for responsive images with w descriptors. Keep a src fallback; add sizes that match your real CSS layout.

Key Takeaways

  • Role: layout-width hint for srcset w candidates.
  • Syntax: media condition + length, then a default length.
  • Default: omitted sizes behaves like 100vw.
  • Accuracy: mirror your CSS layout or you waste bandwidth.

One line: srcset lists files — sizes says how wide they will look.

Frequently Asked Questions

With srcset width (w) descriptors, sizes tells the browser how wide the image will display under different layout conditions so it can pick the smallest adequate file.
Primarily img (and source inside picture) when srcset uses w descriptors. link can also use sizes with icon imagesrcset. It is not the form size attribute.
Usually no. Pixel-density descriptors (1x, 2x) do not use sizes. Width descriptors (500w, 1000w) should be paired with sizes.
The default is 100vw — the browser assumes the image is as wide as the viewport when choosing from srcset.
auto lets the browser use the image’s expected layout width after CSS. It is intended with loading=lazy. Add fallback sizes after auto for older browsers.
No. size is for input/select visible control size. sizes is for responsive image layout width with srcset.

Pair with srcset

Learn width and density descriptors for candidate images.

srcset tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful