Optical padding
Lab / Optical padding
Give a button the same padding on every side and it still looks taller than it is wide. This page is about why, and about a few lines of CSS that fix it with two newer units: lh, the height of a line, and cap, the height of a capital letter.
The technique is Haz's. He first shared the formula on Bluesky in January 2026, then built a button and a box with an icon of any size on it. This page walks through those patterns with live demos.
Every demo is real CSS. The overlay measures, in your browser, the gap from the edge of each box to the ink inside it, in pixels. The dashed box is a line of text (1lh tall) and the solid box is its capital letters (1cap tall).
The problem
Text doesn’t fill its line. A line is 1lh tall, but the capitals in it are only 1cap tall, and the rest is empty space shared above and below them. So padding: 0.5em puts 0.5em between the edge and the line box, not between the edge and the letters. Sideways nothing is in the way, so the left and right gaps really are 0.5em. Up and down, the empty part of the line adds to them, and the first button below looks fatter than it is wide. Turn up the line height and watch it get worse.
The formula
The empty space between the edge of the line box and the capitals is about (1lh - 1cap) / 2 on each side. Add that to the horizontal padding, and both directions measure from the letters. That’s the second button above: change the line height or the font and its sides follow, because lh and cap are worked out by the browser for whatever font and line height the button has.
.button {
font-size: 16px;
line-height: 1.5;
--py: 0.5em;
--px: calc(var(--py) + (1lh - 1cap) / 2);
padding: var(--py) var(--px);
gap: var(--px);
}
Pattern 1: an icon in a button
Make the icon 1lh square, as tall as the line it sits in. An icon is solid ink to its edges, so it doesn’t need the extra horizontal padding the letters do. A negative inline margin of var(--py) - var(--px) takes it back, and the icon ends up with exactly --py on every side, including the gap to the text. The text keeps --px on its own sides. With no text at all, the button comes out square.
.button {
line-height: 1.5;
--py: 0.50em;
--px: calc(var(--py) + (1lh - 1cap) / 2);
display: inline-flex;
padding: var(--py) var(--px);
gap: var(--px);
}
.button .icon {
width: 1lh;
height: 1lh;
margin-inline: calc(var(--py) - var(--px));
}
Pattern 2: a callout with an icon and wrapping text
The same two variables work for a box. align-items: start keeps the icon on the first line while the text wraps under it, and since the padding, the gap and the icon’s margins all come from --py, the spacing holds at any width and any --py. Narrow the box and the text wraps, but the gaps don’t move. (The right gap of wrapped text is measured to the edge of its space, since each line ends where its last word does.)
Your changes are saved on this device. Sign in to keep them in sync with your other devices, or keep working offline and sign in later.
.box {
--py: 0.75em;
--px: calc(var(--py) + (1lh - 1cap) / 2);
display: flex;
align-items: start;
gap: var(--px);
padding: var(--py) var(--px);
}
/* The icon from the button, unchanged */
.box .icon {
width: 1lh;
height: 1lh;
margin-inline: calc(var(--py) - var(--px));
}
Pattern 3: an icon of any size, lined up with the first line
A 1lh icon fits its line exactly. For any other size, two margins do the work. margin-block: calc((1lh - var(--size)) / 2) centers the icon on the first line, so it stays lined up with line 1 however the text wraps (it goes negative when the icon is taller than a line, and the icon reaches out of the line evenly). And margin-inline: calc((1cap - var(--size)) / 2) makes the icon take up only 1cap of width, the same as text does, so the box’s padding treats it like a letter.
Why the icon’s gaps match at any size:
left gap = px + (1cap - size) / 2
= py + (1lh - 1cap) / 2 + (1cap - size) / 2
= py + (1lh - size) / 2
= top gapFry has been asleep in the sunbeam for three hours. The sunbeam has moved twice and he has followed it both times.
.box {
--py: 0.75em;
--px: calc(var(--py) + (1lh - 1cap) / 2);
display: flex;
align-items: start;
gap: var(--px);
padding: var(--py) var(--px);
}
.box .icon {
--size: 1.25em;
width: var(--size);
height: var(--size);
margin-block: calc((1lh - var(--size)) / 2);
margin-inline: calc((1cap - var(--size)) / 2);
}
Pattern 4: pill buttons, with a bit of π
Round the ends all the way and two things go wrong, as Haz pointed out. The curve cuts into the corners, so the space at each end of the text looks smaller than the space above and below it, even though it measures the same. And if you add room to make up for it, the icon gets pushed in from the round end and stops looking centered in it.
The fix treats the two differently. The text gets a little extra inline padding: as much as the curve takes away. A square end of a button 2r tall is 2r² of space and a round one is πr²/2, so the curve removes r(1 - π/4) of width on average, about a fifth of the radius. CSS has pi built into calc(), so that goes straight into a variable. The icon gets that same amount back as negative margin, so its center stays at the center of the round end, the same distance from the top, the bottom and the end. On its own, the button is a circle.
In the overlay the dashed circles are the round ends. The readout counts the text’s side gaps without the curve’s share, since that share is there to make up for the corners.
Where the π comes from, for an end of radius r (half the button’s height):
square end = 2r × r = 2r²
round end = πr² / 2
taken back = 2r² - πr²/2
spread down the end's height of 2r:
= r - πr/4 = r(1 - π/4) ≈ 0.21r.pill {
--py: 0.50em;
--px: calc(var(--py) + (1lh - 1cap) / 2);
/* What the round ends take back: r(1 - π/4) */
--curve: calc((1lh + 2 * var(--py)) / 2 * (1 - pi / 4));
border-radius: 999px;
padding: var(--py) calc(var(--px) + var(--curve));
gap: var(--px);
}
/* The icon stays centered in the round end */
.pill .icon {
width: 1lh;
height: 1lh;
margin-inline:
calc(var(--py) - var(--px) - var(--curve))
calc(var(--py) - var(--px));
}
/* On its own, it's centered in both ends: a circle */
.pill .icon:only-child {
margin-inline: calc(var(--py) - var(--px) - var(--curve));
}
Haz’s video has his own CSS for this. This is my working of the same two ideas from what he describes, so the numbers may differ from his.
Why it works
Each formula measures from the visible ink, the height of the capitals, instead of the invisible line box, so one --py controls every edge.
Caveats
(1lh - 1cap) / 2assumes the capitals sit about in the middle of the line box. That’s close for most interface fonts, but each font is drawn differently. Switch the first demo to Georgia or Cascadia Code and the measured top and bottom drift apart by a pixel or so. Haz has said that pairing the formula withtext-box-trimcould make sense when the capitals need to be dead center.capcomes from the first available font, so the spacing can shift a little while a web font is loading and the fallback font is standing in.- The measurements here stop at the baseline and the top of the capitals. Descenders (g, p, y) and accents hang into the padding, the same way they do in any well-set type.
Browser support
lh and cap both work in every current major browser: Chrome and Edge 118, Firefox 120 and Safari 17.2 are the first versions with both. For older ones, mind how the fallback is written. A plain declaration before the calc() one only saves you when the units are in that line itself. Through a custom property like --px the browser can’t tell the value is bad until it’s too late, and drops the padding altogether. So put the balanced version behind @supports:
.button {
padding: 0.5em 0.75em; /* every browser */
}
@supports (padding: 1lh) and (padding: 1cap) {
.button {
--py: 0.5em;
--px: calc(var(--py) + (1lh - 1cap) / 2);
padding: var(--py) var(--px);
}
}
Credit
This is Haz’s technique, from his posts on Bluesky: “How to get even margins without ignoring line-height” (January 2026), “Fixing a button with CSS and Math” and “Stop centering icons with align-items: center” (November 2026), and the pill buttons from “You (and Claude) have been styling pill buttons wrong all along”. The demos, the measuring overlay and any mistakes are mine.