StealThis .dev

Cookbook — Serving scaler (½× · 2× quantities)

A live serving-size scaler for a printed-style recipe card. Pick ½×, 1×, 2×, or 3×, or nudge a servings stepper, and every ingredient quantity recomputes in place with a soft saffron flash. The math snaps to cook-friendly fractions like ½, ⅓, ¼, and ¾, handles ranges such as twelve to fourteen ounces of tomatoes, and leaves non-scalable items — basil to taste, a single pan — exactly where they are. The servings count is announced via aria-live, and the layout prints cleanly.

Open in Lab
html css vanilla-js
Targets: JS HTML

Code

Serving scaler (½× · 2× quantities)

An editorial recipe card — charred tomato and saffron orzo — with a scaling control bolted onto its ingredient list. A segmented ½× · 1× · 2× · 3× radiogroup and a numeric servings stepper drive the same underlying factor, so choosing a multiplier and tapping the stepper stay perfectly in sync. A small readout spells out the base yield, the new target, and the active multiplier.

Every change live-recomputes each quantity in place and snaps the result to cook-friendly fractions — ½, ⅓, ¼, ¾ and friends — rather than spitting out raw decimals. Ranges scale at both ends (twelve to fourteen ounces becomes six to seven at ½×), large amounts round to whole numbers, and tiny amounts fall back to a clean decimal so nothing ever reads as zero. Items marked to taste or fixed — fresh basil, shaved pecorino, the single skillet — are flagged and never scaled.

Each updated quantity gets a brief saffron flash so the eye can follow what changed, the servings count is wrapped in an aria-live region for screen readers, the multiplier buttons are arrow-key navigable, and a Reset to base action returns the card to its original four servings. The whole layout collapses to a single column by 720px and hides its controls under @media print.

Illustrative UI only — recipes & nutrition data are fictional, not dietary advice.