Sub topic 9
Controlling Kerning in CSS
This chapter assumes you know what kerning and tracking are, and that you can write basic CSS selectors and properties. You should also know that kerning data is stored inside the font as pair adjustments. Here you will learn how CSS switches that data on and off, and how it relates to letter-spacing.
Kerning is on by default, mostly
Browsers read the kerning pairs from the font file and apply them while laying out text. In most modern browsers, kerning is on for all text by default. Some engines have historically skipped it on small text to save time, so the result can differ between browsers.
CSS gives you two ways to control this:
- The
font-kerningproperty is the simple switch. - The
kernOpenType feature, set throughfont-feature-settings, is the low-level control thatfont-kerningmaps onto.
The font-kerning property
font-kerning accepts three values.
| Value | Behavior |
|---|---|
| `auto` | The browser decides whether to apply kerning. This is the default. |
| `normal` | Kerning from the font is applied. |
| `none` | Kerning from the font is not applied. |
Setting normal explicitly gives you consistent behavior across browsers. Setting none is mainly useful for comparison, or for text where you need every glyph at its default advance width.
body {
font-kerning: normal;
}
.kerning-off {
font-kerning: none;
}A quick way to see the effect is to render the same pair-heavy text twice, once in each state. Words such as "AVATAR", "To", "Wa", and "Ty" show the largest differences, because their shapes leave large gaps without pair adjustments.
The kern feature
OpenType fonts expose kerning as a feature named kern. You can set it directly:
.manual-kern-off {
font-feature-settings: "kern" 0;
}
.manual-kern-on {
font-feature-settings: "kern" 1;
}Prefer font-kerning when you can. It is more readable and does not interfere with other features. A font-feature-settings declaration replaces the whole feature list on that element, so a later rule can silently undo ligature or numeral settings you set elsewhere. Use the feature syntax when you need to target kerning together with other features in a single declaration.
Kerning and letter-spacing together
Kerning and tracking stack. The browser first applies the pair adjustments from the font, then adds the letter-spacing value to every gap. Tracking does not erase the kerning, so a tightly kerned pair such as "AV" stays tighter than its neighbors even when you open up the whole line.
.headline {
font-kerning: normal;
font-size: 4rem;
letter-spacing: -0.02em;
}Here the font's pair adjustments run first, and then each gap loses 0.02 em. At 64 px, that is about 1.28 px per gap.
One related behavior is worth knowing. When letter-spacing is non-zero, browsers do not apply optional ligatures. If you track out all-caps text and want identical results across browsers, turn them off yourself:
.tracked-caps {
text-transform: uppercase;
letter-spacing: 0.1em;
font-variant-ligatures: none;
}Common mistakes
- Turning kerning off globally. Text then loses the font designer's pair work, and gaps around letters like "T" and "V" look uneven.
- Using
font-feature-settings: "kern" 0in a rule and forgetting that it also resets other features set on the same element. - Trying to fix a single bad pair with
letter-spacing. CSS has no per-pair kerning property, so that adjustment needs a font with better kerning data, or a span around the one letter with its own smallletter-spacing, used sparingly.
Exercise
A headline uses font-kerning: normal and letter-spacing: 0.05em. Does the "AV" pair still receive its kerning adjustment? Explain what the browser does.
Show answer
Yes. The browser first applies the font's pair adjustment, which pulls "A" and "V" together. It then adds 0.05 em to the gap. The pair ends up tighter than it would be with tracking alone, but looser than it would be without the added tracking.
Exercise
Write CSS that turns kerning off for an element with the class .compare, using the dedicated property rather than font feature settings.
Show answer
.compare { font-kerning: none; }