🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
HTML MASTER CLASS /// LEARN TAGS /// BUILD STRUCTURE /// SEMANTIC WEB /// HTML MASTER CLASS /// LEARN TAGS ///

sizes: Completing The Resolution-Switching Picture

Understand why the browser's default 100vw assumption is often wrong without sizes, how to write accurate media-condition-based sizes values, and why sizes must be manually kept in sync with actual CSS layout.

Total XP: 0|💻 html XP: 0

Skill Matrix

UNLOCK NODES BY LEARNING NEW TAGS.

sizes Attribute

Display width signaling.


🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.

srcset alone tells the browser what candidates exist; sizes tells it how large the image will actually render at different viewport widths — together, they let the browser make a genuinely informed, efficient choice.

1The Cost Of Omitting sizes

When srcset uses width descriptors but no sizes attribute is present, browsers apply a default assumption: the image displays at 100% of the viewport width (100vw). For a genuine full-bleed hero image, this default happens to be correct. For anything else — a three-column grid item rendering at roughly 33% of viewport width, a sidebar thumbnail, a card image — this default is significantly wrong, causing the browser to select a candidate sized for full-width display when a much smaller file would have sufficed.

This is a subtle but common source of unnecessary bandwidth waste: the srcset attribute may be perfectly implemented, but without an accurate sizes value, its benefit is significantly undermined for any non-full-width image.

<!-- No sizes: assumes 100vw, likely over-downloads for a grid item -->
<img srcset="a.jpg 400w, b.jpg 800w, c.jpg 1200w" src="b.jpg" alt="...">
localhost:3000
⚠ Likely Over-DownloadingA 33%-width grid item without a sizes attribute may load a candidate sized for full viewport width.

2Writing Accurate sizes Values

The sizes attribute is a comma-separated list of media-condition and width pairs, structurally similar to CSS media queries: sizes="(max-width: 600px) 100vw, (max-width: 1200px) 50vw, 33vw" states that below 600px viewport width the image displays at 100% viewport width, between 600px and 1200px at 50%, and above 1200px at 33% (that final, condition-less entry serving as the default fallback).

The browser evaluates this list against the current viewport at load time, combines the resulting display width with the actual pixel widths declared in srcset's w descriptors, and selects the smallest candidate that still satisfies that display requirement at the device's pixel density.

<img
  srcset="a.jpg 400w, b.jpg 800w, c.jpg 1200w"
  sizes="(max-width: 600px) 100vw, (max-width: 1200px) 50vw, 33vw"
  src="b.jpg" alt="...">
localhost:3000
≤600px viewport →
100vw display width
>1200px viewport →
33vw display width

3sizes Must Match Real CSS — Manually

A critical, easy-to-miss detail: the sizes attribute is entirely independent of actual CSS layout rules — the browser doesn't cross-reference it against your stylesheet to verify accuracy. If a CSS refactor changes a grid item's actual rendered width from 33% to 25%, but the sizes attribute isn't updated to match, the browser continues making candidate-selection decisions based on the now-inaccurate, stale sizes value.

This makes sizes a value that requires the same ongoing maintenance discipline as any other piece of markup tightly coupled to a specific layout — worth flagging explicitly in code review whenever a responsive layout's column widths or breakpoints change.

/* CSS changed from 33% to 25% width... */
.grid-item img { width: 25%; }
<!-- ...but sizes wasn't updated to match -->
<img sizes="33vw"> <!-- now stale -->
localhost:3000
⚠ Requires Manual Sync With CSSUpdate sizes values as part of any layout change affecting an image's rendered width.

4Step-by-Step Breakdown

Telling The Browser How Big The Image Will Actually Be. srcset's width descriptors tell the browser how big each candidate file *is*. The sizes attribute tells the browser how big the image will actually be *displayed* at different viewport widths — without it, the browser has to guess, and its default guess is often wrong.

Without sizes, The Browser Assumes 100vw. If srcset uses width descriptors but sizes is omitted, browsers default to assuming the image displays at 100% of the viewport width — a reasonable guess for a full-bleed hero image, but wrong (and wasteful) for anything smaller, like a grid item or sidebar image.

The Default Assumption. An image displays at only 30% of the viewport width in a three-column grid layout, but has no sizes attribute. What does the browser assume?

  • It correctly infers 30vw from the CSS grid layout automatically
  • It defaults to assuming 100vw, likely causing it to over-download a larger file than needed
  • The image fails to load without an explicit sizes attribute

sizes Uses Media Conditions To Describe Layout. The sizes attribute is a comma-separated list of media-condition/width pairs, mirroring CSS media queries: '(max-width: 600px) 100vw, (max-width: 1200px) 50vw, 33vw' describes how the image's display width changes across different viewport breakpoints.

Reading sizes Syntax. In 'sizes="(max-width: 600px) 100vw, 50vw"', what does the final, condition-less '50vw' represent?

  • A fallback value applied when no earlier condition matches
  • Invalid syntax; every entry requires a condition
  • It's ignored entirely by browsers

sizes Must Match The Actual CSS Layout. The sizes attribute is entirely independent of and not automatically derived from actual CSS — if the two disagree (sizes claims 50vw but CSS actually renders the image at 33vw), the browser makes selection decisions based on the incorrect sizes value, potentially choosing a suboptimal candidate.

sizes/CSS Mismatch. If an image's sizes attribute claims 50vw but its actual CSS width is 33vw, what happens?

  • The browser automatically detects and corrects the mismatch
  • The browser may select a larger, less optimal candidate than actually necessary, based on the incorrect sizes value
  • There's no real consequence either way

sizes Attribute Mastered. You now understand why sizes is necessary to avoid the wasteful 100vw default assumption, how to write media-condition-based sizes values, and why keeping sizes accurately synced with real CSS layout is essential for correct browser candidate selection.

Tell The Browser The Rendered Width. The sizes attribute lets the browser pick the right srcset candidate before layout.

Level Up 🚀

Advanced cheat sheets, SEO tricks, and interview prep for this topic.

Browser Support

ChromeSupported

Fully supported.

FirefoxSupported

Fully supported.

SafariSupported

Fully supported.

EdgeSupported

Fully supported.

Accessibility (A11y)

1sizes Has No Direct Accessibility Impact But Supports Overall Performance, Which Benefits All Users

Faster-loading, appropriately-sized images benefit users on constrained connections or older devices broadly, including many users who also rely on assistive technology.

SEO Implications

  • 1

    An Accurate sizes Attribute Is Necessary To Realize srcset's Full LCP Benefit

    srcset without a correct sizes value can still result in over-downloading for non-full-width images, meaning the two attributes should be evaluated together, not srcset alone, when auditing LCP-related image performance.

Best Practices

Update sizes Values Whenever A Layout Change Affects An Image's Rendered Width

Since sizes isn't automatically derived from CSS, it requires the same deliberate maintenance as any other markup value coupled to a specific layout decision.

Default To Full-Width Images Simplifying To Omitting sizes (Relying On The 100vw Default) Only When Genuinely Accurate

For true full-bleed hero images, the 100vw default is already correct, so an explicit sizes attribute in that specific case adds no value — but this should be a deliberate choice, not an oversight.

Frequent Bugs

THE BUG

A grid of thumbnail images is downloading noticeably larger files than their actual small rendered size would suggest.

THE FIX

Add an accurate sizes attribute reflecting the thumbnail's real rendered width (e.g. 33vw for a three-column grid), rather than relying on the incorrect 100vw default.

THE BUG

After a responsive redesign changes column widths, image loading performance regresses despite srcset remaining unchanged.

THE FIX

Update the sizes attribute to match the new actual rendered widths — it wasn't automatically kept in sync with the CSS layout change.

Real-World Examples

A Three-Column Grid With Accurate sizes

A product grid correctly signaling that images render at roughly a third of viewport width above a tablet breakpoint.

<img
  srcset="product-300.jpg 300w, product-600.jpg 600w, product-900.jpg 900w"
  sizes="(max-width: 768px) 100vw, 33vw"
  src="product-600.jpg"
  alt="Blue running shoe, side view"
>

Interview Prep

?Frequently Asked Questions

Pascual Vila

Pascual Vila

Frontend Instructor // Code Syllabus

Common Pitfalls & Errors

The Error //

Omitting sizes for a non-full-width image using srcset width descriptors

<img srcset="..." sizes="(max-width: 768px) 100vw, 33vw" src="..." alt="...">

The Solution //

Add an accurate sizes attribute reflecting the image's real rendered width.

The Error //

Letting sizes go stale after a layout change

<!-- Keep sizes synced with real CSS layout -->

The Solution //

Update sizes values whenever a layout change affects the image's actual rendered width.

Lesson Glossary

[01]sizes

Declares an image's actual rendered display width per viewport.

Code Preview
sizes="(max-width:600px) 100vw, 33vw"

[02]100vw Default

The browser's fallback assumption when sizes is omitted.

Code Preview
Often inaccurate for non-full-width images

[03]Media Condition

A viewport-width test within a sizes value.

Code Preview
(max-width: 600px)

[04]Candidate Selection

The browser's algorithm choosing a srcset file using sizes.

Code Preview
srcset + sizes together

Continue Learning