Responsive Images, The sizes Attribute, and Unexpected Image Sizes

Mark Root-Wiley
May 14, 2016 · 4 min read

My Outdated [Technically Correct] Assumption About Image Sizes

<img src="example.jpg">
img {
max-width: 100%;
}

Responsive Images Make Things Better (and More Complicated)

<img
src="example.jpg",
srcset="example.jpg 2000w,
example-thumb.jpg 400w,
example-medium.jpg 1000w,
example@2x.jpg 4000w"
sizes="100vw">

Where Things Get Tricky

<img
src="example.gif",
srcset="example.gif 200w"
sizes="(min-width: 400px) 400px, 100vw"
class="logo">
img.logo {
max-width: 400px;
}

The selected source size affects the intrinsic size of the image (the image’s display size if no CSS styling is applied).

Variations on the sizes attribute with a small responsive image. View full demo on CodePen.
<img
src="example.gif",
srcset="example.gif 200w"
sizes="(min-width: 400px) 400px, 100vw"
width="200" /* <=== TA-DA! */
class="logo">

Does This Really Happen?


Footnotes


About The Author

Mark Root-Wiley

Written by

Mark makes WordPress websites for nonprofits and other mission-driven organizations. He blogs at MRWweb.com/blog and runs NonprofitWP.org.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade