Debug panel

Close debug panel
Roma’s Unpolished Posts

lh is Not The Actual Line Height

Published on:
Categories:
Cap unit 2, CSS 101, Typography 12
Current music:
Mice Parade — 
Gulliver’s Travels
Current drink:
Peppermint Tea

A week ago, in my “Dropped Line Indentation” post, I mentioned a 2017 article “Deep dive CSS: font metrics, line-height and vertical-align” by Vincent De Oliveira.

There, he goes into many details over how line-height and related concepts work, but also he describes the “line-box” concept, which I want to quote:

Each line is made up of one or many inline elements (HTML tags or anonymous inline elements for text content) and is called a line-box. The height of a line-box is based on its children’s height. The browser therefore computes the height for each inline elements, and thus the height of the line-box (from its child’s highest point to its child’s lowest point). As a result, a line-box is always tall enough to contain all its children (by default).

One place Vincent does not mention there is the lh unit — and if we quote this MDN description of it:

[lh is] Equal to the computed value of the line-height property of the element on which it is used, converted to an absolute length. This unit enables length calculations based on the theoretical size of an ideal empty line. However, the size of actual line boxes may differ based on their content.

What all of this means? We can’t use lh anywhere where we want to adjust something to the line-box. We might think that we are adjusting things to the line-height, but the line-height is just one of the things that can impact the line-box.

While this might be an edge-case that might not appear in practice, there is always a chance. And I often see people mentioning using lh as a way to align things to a line without acknowledging the line-box problem.

For example, in his recent “Better Icon and Label Alignment” article, Ahmad Shadeed proposed a way to align an icon to the first line of some wrapped text by offsetting it from the top based on the lh unit. That works for his cases, but as soon as something will appear that will increase the line-box, that method will break.

This example looks fine, right?

Some text that could wrap on several lines.

.flex {
	display: flex;
	align-items: start;
	width: 15em;
	max-width: 100%;
	gap: 0.5rem;
}

.icon {
	--icon-size: 1rem;
	width: var(--icon-size);
	height: var(--icon-size);
	background: var(--PINK);
	flex-shrink: 0;

  	--offset: calc((1lh - var(--icon-size)) / 2);
  	transform: translateY(var(--offset));
}
<p class="flex">
	<span class="icon"></span>
	<span>
		Some text that could wrap on several lines.
	</span>
</p>

But let’s add an element to the first line that increases the line-box!

Some text that could wrap on several lines.

.padded {
	display: inline-block;
	padding: 0.5em;
	border-radius: 50%;
	border: 1px solid var(--PINK);
}

Oh no!

A fix would be to use align-items: baseline, and the cap unit, if we want to align things to the cap-height — all as I described in my old “Cap-Height Vertical Align” post:

Some text that could wrap on several lines.

.fixed {
	align-items: baseline;

	& .icon {
		transform: none;
		margin-block-start: calc(1ex - 1cap);
	}
}

Now, regardless if the line-box is increased, we are actually aligning the baselines of our elements first, and then shifting the position of our icon relative to that baseline!

Just in case: this is not just on Ahmad (sorry I used your article as an example) — I saw others making the same mistake — trying to align things to the wrong place in the wrong way.

In many cases, this is fine, but occasionally, an incorrect technique could backfire.

Please share your thoughts about this on Mastodon!