HTML sizes Attribute

Beginner
⏱️ 6 min read
📚 Updated: Jun 2026
🎯 3 Examples
Media & Images

Introduction

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.

What You’ll Learn

01

Display slots

Layout width hints.

02

With srcset

Pick the right file.

03

Media lists

Breakpoint syntax.

04

vs size

Forms vs images.

05

picture

On source too.

06

JavaScript

Dynamic updates.

Purpose of sizes

The 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).

💡
Not for videos or form fields

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).

📝 Syntax

Pair sizes with srcset width descriptors on <img>:

sizes.html
<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">

Syntax Rules

  • Value is a comma-separated list of media condition + length pairs, plus a final default length.
  • Each entry: (media-query) length — e.g. (max-width: 600px) 100vw.
  • The last item has no media query and acts as the fallback slot size.
  • Lengths can use px, vw, vh, em, rem, or % (relative to the viewport for percentages in this context).
  • Use with srcset containing w width descriptors for responsive selection.
  • Also valid on <source> inside <picture> when that source has srcset.
  • If omitted, the default is 100vw.

💎 Values

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).
sizes-values.html
<!-- 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"

⚡ Quick Reference

TopicDetailExample
Used on<img>, <source>With srcset
PurposeRendered width hintResource selection
FormatMedia + length list(max-width: 600px) 100vw, 50vw
Default100vw if omittedFull viewport assumed
Pair withsrcset width (w)photo.jpg 800w
JavaScriptimg.sizes = "..."Runtime layout changes

Applicable Elements

ElementSupported?Notes
<img>YesMost common use; pair with srcset
<source>YesInside <picture> with srcset
<video>NoUse media on source for format/art direction
<select>, <input>NoUse form size instead (different attribute)
<link> preloadRelatedPreload images can include imagesizes (similar idea)

size vs sizes vs srcset

Featuresizesizessrcset
Used onselect, inputimg, sourceimg, source
PurposeVisible rows or char widthDisplay slot width hintList of image candidates
Value typeIntegerMedia + length listURLs with w or x
Works together?NoYes — with srcsetYes — with sizes

Examples Gallery

Responsive img with srcset, dynamic JavaScript updates, and picture with source sizes.

👀 Live Preview

sizes with three srcset candidates. Resize the window to change the layout slot:

Demo responsive image with sizes attribute

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.

Example — Responsive img with sizes

Define layout slots so the browser picks an appropriate file from srcset:

sizes.html
<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">
Try It Yourself

How It Works

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.

Dynamic Values with JavaScript

Update img.sizes when layout changes at runtime:

dynamic-sizes.html
<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>
Try It Yourself

How It Works

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.

Example — picture and source

Use sizes on a <source> inside <picture> for art direction or resolution switching:

picture-sizes.html
<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>
Try It Yourself

How It Works

media on source picks which candidate set applies. sizes on that same source then guides width-based selection within the matched set.

♿ Accessibility

  • Always include alt textsizes affects which file loads, not accessibility text. Every <img> still needs a meaningful alt.
  • Do not hide content in smaller files — If you serve different crops via picture, ensure essential information is available at all breakpoints.
  • Respect user zoom — Layout slots should account for text zoom and high-DPI screens; test with increased font size.
  • Avoid layout shift — Set width and height (or aspect-ratio in CSS) so images reserve space while loading.
  • Decorative images — Use alt="" for purely decorative responsive images; sizes works the same.

🧠 How sizes Works

1

Author lists candidates

srcset with width descriptors.

srcset
2

Author defines slots

sizes media + length list.

sizes
3

Browser picks a file

Slot × DPR vs srcset widths.

Selection
=

Right-sized download

Sharp image, less waste.

Browser Support

The sizes attribute with srcset width descriptors is supported in all modern browsers.

HTML5 · Fully supported

Responsive images everywhere

Chrome, Firefox, Safari, and Edge all honor sizes with srcset.

98% Browser support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
sizes attribute 98% supported

Bottom line: Use sizes with srcset for production responsive images; always keep a fallback src.

💡 Best Practices

✅ Do

  • Match sizes to your real CSS layout (grid columns, sidebar width, etc.)
  • Always pair sizes with srcset width descriptors
  • Provide a sensible fallback src for older browsers
  • Set explicit width/height to prevent layout shift
  • Test on mobile, tablet, and desktop plus 2x displays

❌ Don’t

  • Confuse sizes with form size
  • Assume sizes resizes the image visually (CSS does that)
  • Omit sizes when CSS makes images smaller than 100vw
  • Use one giant image in srcset only—offer multiple widths
  • Forget meaningful alt text on responsive images

Conclusion

The 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.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about sizes

Bookmark these before your next responsive image.

5
Core concepts
🔗 02

Needs srcset

Width descriptors

Pairing
📱 03

Media list

Breakpoint slots

Syntax
⚙️ 04

.sizes JS

Dynamic layout

Dynamic
📋 05

Not size

Forms differ

Compare

❓ Frequently Asked Questions

It tells the browser how wide an image will be displayed at different viewport widths, so it can pick an appropriate file from srcset.
<img> and <source> when used with responsive srcset width descriptors.
Yes for meaningful responsive selection. sizes describes the slot; srcset lists the files to choose from.
No. size on forms controls visible rows or character width. sizes on images describes responsive layout slots.
Yes. Set img.sizes when layout changes, such as on resize or when toggling a sidebar.
Yes in all modern browsers. Always provide a fallback src for older user agents.

Build responsive images the right way

Practice sizes with srcset in the Try It editor.

Try responsive img →

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.

5 people found this page helpful