I spent way too much time on this, so I’ll just share my results.
That eightshapes.com tool, it doesn’t actually seem to solve the problem - it offers parameters to adjust the top/bottom cropping manually, on top of the already-adjusted values, which tells you they knew it doesn’t really work. By which I mean, it doesn’t work for different line-heights. It seems really complicated for what it does.
I then discovered the newer lh
unit, which supposedly gives you a unit that is relative to the font-size and line-height. From my testing, it does change relative to both font-size and line-height, but it doesn’t do so at the correct rate for the line-height. (go figure.)
I finally opted for just using negative margins on the text elements - you can see this working here:
So you have to manually find first the margin-bottom, and then the margin-top, for a given font at a specific line-height.
It’s a pain, but it is relatively simple, as you can see.
In practice, you would probably want to create mixins for your font-family, line-height, and margin combinations, so you can reuse them.
(At the bottom of my fiddle, you can also find a commented-out version using lh
rather than em
units. I opted for em
in the end, because as mentioned, lh
units, while they do respond to line-height, they do not appear to do so at the correct rate. Please experiment with this, if you think I’ve made a mistake? I feel like there is something I don’t understand about the lh
unit and perhaps it ought to work, but I couldn’t figure it out…)