Stepped Sizing in CSS: A Trick, Not a Feature

CSS does ship a steps() function, but it belongs exclusively to animation. There is no way to declare, say, that an element may grow in height only in increments of 10px. Whether that ever changes is anyone's guess — the author suggests there would need to be clear use cases that things like background-repeat: space || round; don't already cover.

One alternative approach is sequential media queries.

@media (max-width: 1500px) { body { font-size: 30px; }}
@media (max-width: 1400px) { body { font-size: 28px; }}
@media (max-width: 1300px) { body { font-size: 26px; }}
@media (max-width: 1200px) { body { font-size: 24px; }}
@media (max-width: 1000px) { body { font-size: 22px; }}
@media (max-width: 900px) { body { font-size: 20px; }}
@media (max-width: 800px) { body { font-size: 18px; }}
@media (max-width: 700px) { body { font-size: 16px; }}
@media (max-width: 600px) { body { font-size: 14px; }}
@media (max-width: 500px) { body { font-size: 12px; }}
@media (max-width: 400px) { body { font-size: 10px; }}
@media (max-width: 300px) { body { font-size: 8px; }}

That approach is admittedly strange, and fluid typography would usually be the better pick — but the goal here is stepped resizing, not smooth fluidity.

A different technique surfaced in a StackOverflow answer by John Henkel, which the author later learned Star Simpson had also pointed out. It is a ridiculous hack and, by the author's own warning, should never be used — but as a CSS trick it was too good not to share.

The calc function uses double precision float. Therefore it exhibits a step function near 1e18… This will snap to values 0px, 1024px, 2048px, etc.

calc(6e18px + 100vw - 6e18px);

The behavior is odd, and it stems from an unspecced implementation detail — which means it shows up only in Chrome and Safari.

The calculation can be tuned and applied to whatever property you like; the author turns it down considerably and applies it to font-size instead. Resizing the result in Chrome or Safari reveals the stepping.