Image for post
Image for post

Cropping Away Negative Impacts of Line Height

Getting rid of pesky space above and below HTML text

Kevin Powell
Mar 22, 2018 · 5 min read

How it works

I’ve added a red border around this paragraph to illustrate the white space I want to remove.

Image for post
Image for post
Image for post
Image for post
Preserve internal line height but crop top and bottom white space

Finding the formula

By experimenting at multiple font sizes and noting the offsets needed to crop the text at each size, a formula started to emerge:

// Top crop:$top-crop + ($desired-line-height - $line-height-with-crop) * ($font-size-with-crop / 2)), 0) / $font-size-with-crop;// Bottom crop:$bottom-crop + ($desired-line-height - $line-height-with-crop) * ($font-size-with-crop / 2)), 0) / $font-size-with-crop;
Image for post
Image for post

Making it work for any font

The mixin didn’t work when applied to different typefaces and I couldn’t figure out why. After months of head scratching and experimentation, I ran across this excellent article explaining Font Metrics on the web. In the article, Vincent De Oliveira describes the Em Square.

Image for post
Image for post
Image for post
Image for post

Cropping Caveats

Cropping the em square with this technique isn’t a silver bullet. Turns out typefaces (and em squares) render differently across operating systems. Cropping the em square to perfection on a Mac may be too much cropping or not enough on a Windows browser. Even on my local computer I’ve seen the crop be off by a pixel or two depending on whether I’m viewing on a regular resolution or retina display.

Image for post
Image for post
Vertically centering icons and text in a button and providing consistent internal padding.
Image for post
Image for post
Vertically aligning media and accompanying text in a Media Object component.

Three tips for text cropping

Here are some tips for using any typeface-specfic mixin you generate from the Text Crop Tool

* {
@include text-crop;
}

Better solutions?

I hope this tool can save you some time. I experienced a lot of “Google Drought” as I tried to find solutions for this problem. If you have a better way of doing this or a way to improve upon this technique I’d love to hear it. Shoot me a message on twitter or find me in the Design Systems Slack (@kevinmpowell) if you want to chat!

EightShapes

A collection of stories, studies, and deep thinking from…

Kevin Powell

Written by

Husband & Father, Builder of Design Systems @eightshapes.

EightShapes

A collection of stories, studies, and deep thinking from EightShapes

Kevin Powell

Written by

Husband & Father, Builder of Design Systems @eightshapes.

EightShapes

A collection of stories, studies, and deep thinking from EightShapes

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store