Vanilla Breeze v0.7.4

Vanilla Breeze v0.7.4 released

0.7.4 gives the Ambient pack weather. A page names one feed URL on its own site, and the pack sets data-weather from it, uses its sunrise and sunset for dawn and dusk, and never shows a stale observation. Helpers for producing the feed from Open-Meteo are included. Ambient p…

Read the release notes

data-responsive

Responsive image helper: measures an image's rendered width into sizes, generates srcset from a widths shorthand, and shows an optional dev badge naming the selected source.

Overview

data-responsive on an <img> helps with the two parts of responsive images that are easy to get wrong: a sizes value that matches the real layout, and a long srcset. It measures the image's rendered width and writes it to sizes, and a list of widths (data-responsive="400,800,1200") generates the srcset from the src file name.

Usage

The best baseline needs no JavaScript at all: write the srcset in markup and, for lazy images, let the browser measure with sizes="auto" (Chromium 126+; the value after the comma is the fallback for other browsers).

Add data-responsive when the image is eager (above the fold, where sizes="auto" is not allowed) or when you want a measured sizes in every browser:

Options

Attribute Value Description
data-responsive empty, or comma-separated widths Empty: measure sizes only. Widths (e.g. "400,800,1200"): also generate srcset from src.
data-responsive-debug boolean Show a dev badge beside the image with the selected file name and its natural width. Remove it for production.

Measured sizes

  • Only images whose srcset uses width descriptors (400w) are measured; x descriptors do not use sizes.
  • An authored sizes is respected. The exception is auto: on a loading="lazy" image it is left to the browser; on an eager image (where auto is invalid) the width is measured instead.
  • sizes only grows. When the slot gets narrower the larger candidate is already downloaded, and re-selecting on every resize could feed back into the layout (a new source changes the box, which changes the width again).
  • An image that fails to load is no longer measured.

Srcset shorthand

The shorthand inserts -{width} before the extension of the last path segment; the query string and hash are kept. /img/photo.jpg?v=2 with 400,800 produces /img/photo-400.jpg?v=2 400w, /img/photo-800.jpg?v=2 800w (as absolute URLs).

  • An authored srcset wins: the shorthand is ignored.
  • A file name without an extension (https://images.example.com/photos/abc) is left alone, with a console warning.
  • Every generated file must exist. The shorthand cannot check, and a missing candidate is a broken image.

What it cannot do

The script runs after the HTML parser has already requested src. So:

  • The shorthand costs a second request when the chosen candidate differs from src. Write the srcset in markup when that matters.
  • loading and fetchpriority must be in the markup to have any effect. The script only adds decoding="async" when it is missing.

Events

None.

Styling

The debug badge is a span[data-responsive-badge] inserted before the image (or before its <picture>, never inside it). It is absolutely positioned at the inline-start corner; its parent gets position: relative through CSS while the badge is present.

Accessibility

  • The attribute changes which file loads, not what the image means: write the same alt you would without it.
  • Give images width and height attributes so space is reserved before they load (no layout shift).
  • The debug badge is aria-hidden.

Examples

Above-the-fold hero

Eager images cannot use sizes="auto", so this is where measuring helps most. Put fetchpriority in the markup:

Dev badge

Related

  • <picture> — art direction and format fallbacks; data-responsive works on the <img> inside it.
  • <img> — the native srcset, sizes and loading attributes.
  • table[data-responsive="card"] — the unrelated table card layout that shares this attribute name.
  • data-visible — showing and hiding content by viewport width.
  • srcset — the native attribute data-responsive generates.
  • loading — lazy loading and fetchpriority for the same images.
↑