Skip to content

Commit 641d6ef

Browse files
committed
bonus content
1 parent d755cd0 commit 641d6ef

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

content/posts/2025-07-16-font-size-adjust.dj

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,79 @@ the meaning of font size. So, the guideline I'd use is:
7474
> into your CSS reset, right next to `box-sizing: border-box`.
7575

7676
Why `0.53`? That's the invariant ratio for Helvetica, but any number in that vicinity should work!
77+
78+
## Bonus Content: Line Height
79+
80+
While `font-size-adjust` fixes the size of the glyph relative to em-square, it doesn't fix
81+
_position_ of the glyph. This can create line height problems. Consider these two paragraphs that
82+
are styled with `line-height: 24px`, but where the second paragraph is using monospace font for
83+
`coreutils`:
84+
85+
```=html
86+
<style>
87+
.bonus-content p { margin-bottom: 24px; }
88+
.bonus-content p, .bonus-content code {
89+
font-size: 16px;
90+
line-height: 24px;
91+
}
92+
.bonus-content {
93+
background-image: repeating-linear-gradient(
94+
transparent 0 23px,
95+
#ba3925 23px 24px
96+
);
97+
}
98+
.vertically-aligned * { vertical-align: bottom; }
99+
</style>
100+
```
101+
::: bonus-content
102+
You are supposed to use coreutils to solve this problem. \
103+
You are supposed to use coreutils to solve this problem. \
104+
You are supposed to use coreutils to solve this problem. \
105+
You are supposed to use coreutils to solve this problem. \
106+
107+
You are supposed to use `coreutils` to solve this problem. \
108+
You are supposed to use `coreutils` to solve this problem. \
109+
You are supposed to use `coreutils` to solve this problem. \
110+
You are supposed to use `coreutils` to solve this problem. \
111+
:::
112+
113+
```css
114+
.bonus-content p { margin-bottom: 24px; }
115+
.bonus-content p, .bonus-content code {
116+
font-size: 16px;
117+
line-height: 24px;
118+
}
119+
.bonus-content {
120+
background-image: repeating-linear-gradient(
121+
transparent 0 23px,
122+
#ba3925 23px 24px
123+
);
124+
}
125+
```
126+
127+
In the first paragraph, each line is indeed 24 pixels high, but in the second paragraph each line is
128+
slightly larger, despite the line-height being set to 24px explicitly. How can this be? The full
129+
answer is in:
130+
131+
<https://iamvdo.me/en/blog/css-font-metrics-line-height-and-vertical-align>
132+
133+
The TL;DR is that `line-height` doesn't actually set the the height of the line (who would have
134+
thought!). Instead, it sets the height of each individual span of the text on the line. So, both
135+
"supposed" and "`coreutils`" have a 24 pixels high box around them. But because relative position of
136+
glyphs inside the em-box is different between the two fonts, the boxes are shifted relative to each
137+
other to align the baselines. You can see that by adding `vertical-align: bottom`:
138+
139+
{.bonus-content .vertically-aligned}
140+
You are supposed to use `coreutils` to solve this problem. \
141+
You are supposed to use `coreutils` to solve this problem. \
142+
You are supposed to use `coreutils` to solve this problem. \
143+
You are supposed to use `coreutils` to solve this problem.
144+
145+
If we align the boxes, than baselines are not aligned. It follows that when we align the baselines,
146+
the boxes are misaligned, and the line-height ends up larger than the height of any box, because
147+
boxes stick out!
148+
149+
I don't know a great solution here. A hack is to say something like
150+
[`p > code { line-height: 0px; }`]{.display}
151+
such that text set in monospace font doesn't affect line height calculation. Counter-intuitively,
152+
this will work even if the line is entirely monospace (see `strut` in the abovelinked article).

0 commit comments

Comments
 (0)