Skip to the code

Evaluate a sizes attribute

sizes tells the browser how wide an image will be before any CSS has loaded, so it can choose from srcset as soon as it sees the <img>. Write evaluateSizes(sizes, viewportWidth). Given a sizes value (or null for an image without one) and the viewport's width in CSS pixels, return the image's width in CSS pixels, the way the HTML Standard's "parse a sizes attribute" works it out.

Split the value on commas into entries, trim each one, and go through them in order. An entry is a media condition followed by a size, like (max-width: 600px) 100vw, or a size on its own, like 50vw. The first entry whose condition is true gives the width. An entry without a condition counts as true: the standard says only the last entry should be one, but it takes a bare size wherever it finds one, so nothing after it is read.

A size is a number (like 600, 33.5, or .5) and a unit: px, vw (a hundredth of the viewport's width), or em, which is 16 px here: in sizes and in media queries, em is the browser's default font size, not the page's. 0 with no unit is a size too. Anything else where the size should be, such as a percentage, a negative length, another number without a unit, or a word, makes the entry invalid, and so does an empty entry; the standard skips an invalid entry and goes on to the next. Skip auto as well: it's for a lazy-loaded <img> that has been laid out, and here there's no image.

A condition is one or more of (max-width: L) and (min-width: L), joined by and, where L is a number in px or em. max-width is true when the viewport is L wide or narrower, and min-width when it's L or wider; with and, every part has to be true. Spaces inside the parentheses are optional. The tea shop's templates use no other conditions, so treat anything else as a condition that doesn't parse, and skip the entry. A condition that starts with a media type, like screen and (min-width: 600px) or print, never parses in a browser either: sizes takes a media condition, not a whole media query.

When no entry gives a width, the standard's answer is 100vw, the whole viewport. That's also the answer when sizes is null, empty, or only spaces.

const sizes = "(max-width: 600px) 100vw, (max-width: 1200px) 50vw, 400px";
evaluateSizes(sizes, 375); // 375: a phone, where the photo is as wide as the screen
evaluateSizes(sizes, 900); // 450: two photos side by side
evaluateSizes(sizes, 1440); // 400: the last entry has no condition

What the tests check

  1. the first entry whose condition is true gives the width
    const sizes = "(max-width: 600px) 100vw, (max-width: 1200px) 50vw, 400px";
    expect(evaluateSizes(sizes, 375)).toBe(375);
    expect(evaluateSizes(sizes, 900)).toBe(450);
    expect(evaluateSizes(sizes, 1440)).toBe(400);
    
  2. max-width and min-width include the width they name
    expect(evaluateSizes("(max-width: 600px) 100vw, 50vw", 600)).toBe(600);
    expect(evaluateSizes("(max-width: 600px) 100vw, 50vw", 601)).toBe(300.5);
    expect(evaluateSizes("(min-width: 1024px) 300px, 100vw", 1024)).toBe(300);
    expect(evaluateSizes("(min-width: 1024px) 300px, 100vw", 1023)).toBe(1023);
    expect(evaluateSizes("(min-width:1024px) 300px, 100vw", 1100)).toBe(300);
    
  3. and needs every part of the condition
    const sizes = "(min-width: 600px) and (max-width: 1023px) 50vw, 300px";
    expect(evaluateSizes(sizes, 800)).toBe(400);
    expect(evaluateSizes(sizes, 500)).toBe(300);
    expect(evaluateSizes(sizes, 1100)).toBe(300);
    
  4. em is 16px, in conditions and in sizes
    expect(evaluateSizes("(min-width: 40em) 20em, 100vw", 640)).toBe(320);
    expect(evaluateSizes("(min-width: 40em) 20em, 100vw", 639)).toBe(639);
    expect(evaluateSizes("(max-width: 37.5em) .5em, 0", 600)).toBe(8);
    
  5. a size without a condition ends the list wherever it is, and a trailing comma changes nothing
    expect(evaluateSizes("50vw, (max-width: 600px) 100vw", 400)).toBe(200);
    expect(evaluateSizes("(max-width: 600px) 100vw, 50vw,", 800)).toBe(400);
    
  6. falls back to 100vw when nothing applies, and when sizes is empty or missing
    expect(evaluateSizes("(max-width: 600px) 300px", 800)).toBe(800);
    expect(evaluateSizes("", 800)).toBe(800);
    expect(evaluateSizes("   ", 800)).toBe(800);
    expect(evaluateSizes(null, 800)).toBe(800);
    
  7. skips entries with an invalid size, or none, and goes on to the next
    const sizes = "(max-width: 600px) 50%, (max-width: 600px) -10px, (max-width: 600px) 100, 50vw (max-width: 600px), (max-width: 600px), , 320px";
    expect(evaluateSizes(sizes, 500)).toBe(320);
    expect(evaluateSizes("(max-width: 600px) 0, 100vw", 500)).toBe(0);
    
  8. a media type or a condition without parentheses doesn't parse, so its entry is skipped
    const sizes = "screen and (max-width: 600px) 100vw, print 50vw, max-width: 600px 25vw, 300px";
    expect(evaluateSizes(sizes, 500)).toBe(300);
    
  9. skips auto, since there's no laid-out image
    expect(evaluateSizes("auto, (max-width: 600px) 100vw, 50vw", 400)).toBe(400);
    expect(evaluateSizes("auto, 300px", 900)).toBe(300);
    expect(evaluateSizes("auto", 900)).toBe(900);
    

Learn more

solution.jsSaved in this browser

Run tests runs every test on your code, here in your browser. The problem is solved when they all pass.

Ctrl + Enter runs the tests (⌘ + Enter on a Mac).