Fixing a button with CSS and Math.
@diegohaz14K followers
53,252 views
30 Comments
Comments from the original post on X (Twitter)

@diegohaz
Quick (and related) shameless plug: Ariakit Plus is 30% off for Black Friday for the next few days: It'll include styled component recipes for all these design posts, to copy-paste or use with agents via MCP. Planned to launch by the end of the year.
8 likes

@cipriangb
This will soon be a lost art, with AI hardcoding everything... And it's a shame.
6 likes

@diegohaz
This is still useful, but you have to teach your AI agents to do it. Otherwise they will default to `px-4 py-2`
2 likes

@diegohaz
In other words, I'm not posting code because I think people will write it themselves. I'm just giving them context so they can ask their agents to do it.
2 likes

@SundryMill
Years of nudging that arrow 1px left by eye and calling it optical alignment. Nice to finally have the formula
2 likes

@diegohaz
Yeah, I can totally relate. At some point I was like "there should be a formula for that" Luckily, CSS already had the lh and cap units by the time I thought about it. Otherwise, it wouldn't have been possible 😅

@passerby0x16
Great animation to visualize what those settings do! I wonder though if you consider A != B here an imbalance or not?
2 likes

@diegohaz
Text and 1lh icons should be treated differently. It's easier to see why this is more balanced with fully rounded buttons (the first one is using the original post's math):
15 likes

@passerby0x16
Well explained, and indeed the first one just instantly clicks

@ItsRoboki
Great tutorial that would explain why buttons sometimes look great and sometimes they don't
1 like

@souljorje
U gotta have PhD in math to do some CSS nowadays

@fancyPT
Stop overthinking

@diegohaz
I overthought so you can underthink. You’re welcome.
2 likes

@DDMSS1
That's beautiful, and exactly why I both love and hate CSS

@educalvolpz
the margin-inline trick on the icon is the clever part, keeps text, icon and icon-only buttons all driven by the same --py

@meaningmean_
appreciate that!

@popuguy_
Это то, с чем у меня сейчас трудности, с формированием подхода создания компонентов независящих от окружения, т.е контейнеры, а компоненты без isDesktop, isMobile props, и size=xl,lg,md и так далее

@dprophecyguy
man how did you arrive at this ?

@diegohaz
I like CSS and I like math 😅
2 likes

@NotCellium
text-box properties can remove that leading on the text in your buttons if you’d rather just work with padding

