A perforated stamp: drag the width, then switch calc-size() off
CATALUNYA0,85 €

Rajola hidràulica de l’Eixample

BARCELONA · 1900

.stamp {
  box-sizing: border-box;
  width: var(--w);
  display: flex;
  flex-direction: column;
}

/* the fix: whole holes across, whole holes down */
.stamp.fit {
  width: round(down, var(--w), 20px);
  height: calc-size(auto, round(up, size, 20px));
  transition: width 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}

The Penny Black didn’t have perforations. It came off the press in a sheet of 240, and the clerk at the post office had to cut each one out with scissors. Britain only started punching rows of holes between stamps in 1854, and since then collectors have measured them with a gauge that counts how many holes fit in two centimetres, so a “perf 14” stamp has fourteen.

The Penny Black: a black one-penny stamp with Queen Victoria's profile, POSTAGE across the top, ONE PENNY across the bottom and straight, cut edges with no perforations
The Penny Black, 1840 · Wikimedia Commons

That number is a pitch, the same thing a repeating background or mask has in CSS, but a stamp designer gets to choose the size of the stamp around the holes. On the web it’s the content that decides the size, and the holes just have to cope with whatever they’re given.

Your eye runs along a repeating pattern and expects every repeat to be whole, the same way it expects a row of floor tiles to end on a full tile. When the last hole is cut in half, the stamp looks broken, even to someone who couldn’t tell you why.

The stamp in these demos is a hydraulic tile from the Eixample, the same kind I’ve got on the floor at home. It starts with calc-size() on, so drag it wider and narrower and it only ever grows or shrinks by a whole hole, while the text reflows and the tile scales. Then switch it off and drag again: the holes on the right get sliced wherever the width stops, and the bottom corner looks like something chewed it. If you’d rather not drag, the slider and the switch work from the keyboard, and with reduced motion on, nothing glides.

Punching the holes

There’s no image in the stamp at all: the holes are a mask, a grid of transparent circles every 20px pulled back by half a hole so their centres land exactly on the edges of the box. A second, solid layer covers everything except a 7px border, so the circles in the middle stay filled and only the ones along the edge punch through.

Here’s the same stamp taken apart. It starts as its four layers; switch to X-ray to see them drawn flat on top of each other, or to Stamp to put it back together.

The stamp taken apart: layer by layer, x-ray, and finished
/* every layer is the same box, stacked, and glides on transform */
.iso, .layer {
  transform-style: preserve-3d;
  transition: transform 0.9s cubic-bezier(0.16, 1, 0.3, 1);
}

/* layers view: tilt to isometric, then pull each layer up on Z, a beat apart */
.anatomy:has(#layers:checked) .iso {
  transform: rotateX(56deg) rotateZ(-40deg) scale(0.7);
}
.anatomy:has(#layers:checked) .solid { transform: translateZ(110px); transition-delay: 0.05s; }
.anatomy:has(#layers:checked) .paper { transform: translateZ(220px); transition-delay: 0.1s; }
.anatomy:has(#layers:checked) .print { transform: translateZ(330px); transition-delay: 0.15s; }
.stamp {
  --hole: 20px;
  mask:
    /* a hole in every 20px cell, centred on the cell's corner */
    radial-gradient(circle, transparent 0 5.5px, #000 6px)
      calc(var(--hole) / -2) calc(var(--hole) / -2)
      / var(--hole) var(--hole),
    /* solid paper everywhere except the edge */
    linear-gradient(#000 0 0)
      center / calc(100% - 14px) calc(100% - 14px) no-repeat;
}

Because the grid starts at the top-left corner, the top and left edges always come out perfect, but the right and bottom edges land wherever the box happens to end. At 347px wide, the last column of holes sits at 340px, 7px short of the edge, and those holes end up half-covered by the solid layer and floating inside the paper instead of biting into the edge. That’s the chewed look you saw in the demo.

Measure, then round

The height is the hard part, because nobody sets it: it’s whatever the content and the padding add up to, and that changes every time the text wraps. Before calc-size() there was no way to do maths on that number in CSS. You had to measure it with JavaScript after layout and write a height back, which works, but then your layout is waiting on a script every time something resizes.

calc-size() fixes that. You give it a sizing keyword as the first argument, and in the second one you can use whatever that keyword resolves to, as size:

height: calc-size(auto, round(up, size, 20px));

In plain words, the browser works out the auto height like it normally would and then rounds it up to the next multiple of 20px. Drag the width below and watch the two numbers change, with the dashed line marking where auto would have stopped.

What auto measured, and what calc-size() rounded it to
CATALUNYA0,85 €

Rajola hidràulica de l’Eixample

BARCELONA · 1900

auto 0pxrounded 0px0 holes down

The gap between that line and the edge is never more than 19px, and most of the time it’s a lot less. I’d take a few pixels of extra paper over half a hole in the corner any day.

The width is the easy half

You might expect the width to need calc-size() as well, but it doesn’t, because I’m the one setting it. var(--w) is already a length, and plain round() has worked on lengths in every major browser for a while now:

width: round(down, var(--w), 20px);

I round the width down so the stamp never grows past the column it was given. calc-size() only earns its keep when the starting size is something the browser works out for you, like auto, fit-content or min-content, so if you already have the number, plain round() is all you need. It also means the width can transition like any other length, which is why the stamps you can resize on this page glide from one whole hole to the next.

Where the extra pixels go

Rounding up means the stamp usually ends up a few pixels taller than its content, and those pixels have to go somewhere. By default they pile up at the bottom, and the paper margin under the blue print gets fatter than the one above it. You’d spot that on a real stamp straight away.

Without flex: 1, the slack lands in the bottom margin
CATALUNYA0,85 €

Rajola hidràulica de l’Eixample

BARCELONA · 1900

.stamp {
  display: flex;
  flex-direction: column;
}

/* the print soaks up whatever the rounding added */
.print {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

The fix is to make the print a flex item that grows and takes the extra height itself, while justify-content: space-between shares that space out between the blocks inside the print, where nobody can tell. The paper border stays the same on all four sides.

The height jumps, and that’s fine

I tried adding transition: height 0.3s to smooth out the jumps and nothing happened, and it took me a minute to work out why. A transition only fires when an element’s computed value changes, and the computed value here is the whole calc-size(auto, round(up, size, 20px)) expression, which stays exactly the same when the text inside reflows. Only the final pixel height changes, so the browser has nothing to transition from.

I’m fine with that, because a stamp that grew through half a hole on its way to a whole one would bring back the exact problem I’d just fixed.

It’s the same machinery behind animating height: auto. interpolate-size: allow-keywords, which I covered in the entry and exit animations article, works by treating auto as calc-size(auto, size) so the browser can interpolate between it and a length, and transitions work there because you’re the one changing the declared value, from 0 to auto.

Not just stamps

Anything that repeats along an edge has the same problem, and the same fix. Both of these are editable, so type into them, then switch calc-size() off and on.

Two more patterns that should end whole: stitching and a ticket edge

Fet a mà a Barcelona

100% COTÓ · 30 °C
BITLLET SENZILL · RODALIES

BARCELONA SANTS GIRONA

COTXE 4 · SEIENT 12A
/* stitches every 20px: whole stitches across and down */
.label {
  width: calc-size(fit-content, round(up, size, 20px));
  height: calc-size(auto, round(up, size, 20px));
}

/* a ticket: as wide as its text, in whole scallops */
.ticket {
  width: calc-size(fit-content, round(up, size, 24px));
}

The label has a row of stitches 10px in from every edge, one every 20px. Its width comes from the text and its height from the padding and the line heights, so neither lands on a multiple of 20 unless you make it. Switch calc-size() off and the stitching runs out before it reaches the corners. Switch it on and every corner gets its stitch, and typing grows the label a whole stitch at a time.

The ticket does the same along one axis, with a bite out of the top and bottom every 24px. Its width comes from the text too, which is why both widths use fit-content instead of auto. With the switch off, the bite at the right-hand end gets cut short, and the left end, which always starts on the grid, shows you what it should look like. The same line works anywhere a pattern repeats along an edge and the content decides where that edge ends up.

What silently breaks this

Only Chromium has it. calc-size() shipped in Chrome and Edge 129, but Firefox still has an open implementation bug and Safari, iOS 26.5 included, doesn’t support it at the time of writing. In those browsers the whole height declaration is invalid and gets dropped, so you’re back to auto and a chewed corner. The demos on this page fall back to a few lines of JavaScript that do the same rounding after layout, which is enough to use this today:

if (!CSS.supports('height', 'calc-size(auto, size)')) {
  new ResizeObserver(() => {
    stamp.style.height = 'auto';
    stamp.style.height = Math.ceil(stamp.offsetHeight / 20) * 20 + 'px';
  }).observe(stamp.firstElementChild);
}

mask eats your box-shadow. Everything outside the mask disappears, shadows included. Put a filter: drop-shadow() on a wrapper around the stamp instead. It follows the punched outline, which means every hole gets its own little shadow, and honestly it looks better than a box shadow would have.

box-sizing changes what size means. With border-box, size includes the padding, so the box you’re rounding is the same box the mask covers. With content-box you’d be rounding the content and then adding the padding on top. The stamps here have 32px of vertical padding, so the total lands 12px past a whole hole and the bottom edge gets chewed again.

The half-hole offset matters more than it looks. Without the calc(var(--hole) / -2) position, each circle sits in the middle of its cell, 10px in from the edge. The outer edge stays perfectly straight and you get a row of thin slits just inside it, like a stamp nobody has torn off the sheet yet.

The whole stamp

Browsercalc-size()round()
Chrome, Edge129+125+
FirefoxNot yet118+
SafariNot yet15.4+
.stamp {
  --hole: 20px;
  box-sizing: border-box;
  width: round(down, var(--w), var(--hole));
  height: calc-size(auto, round(up, size, var(--hole)));
  transition: width 0.3s cubic-bezier(0.16, 1, 0.3, 1);
  padding: 16px;
  display: flex;
  flex-direction: column;
  background: #f3ede2;
  /* the hole size and the solid band scale with the gauge too */
  mask:
    radial-gradient(circle, transparent 0 calc(var(--hole) * 0.275), #000 calc(var(--hole) * 0.3))
      calc(var(--hole) / -2) calc(var(--hole) / -2)
      / var(--hole) var(--hole),
    linear-gradient(#000 0 0)
      center / calc(100% - var(--hole) * 0.7) calc(100% - var(--hole) * 0.7) no-repeat;
}

.print {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.stamp-shadow {
  filter: drop-shadow(0 10px 14px rgb(70 45 20 / 0.28));
}

The 20px is my gauge, and if you change --hole the stamp rounds itself to the new pitch without you touching any other number.