Skip to Side A
SIDE A AI & ML

Modern CSS Typography Reset: Container-Based Type, Better Wrapping and Font Controls

Track #25 Date Artist Stu Robson Plays 455

A modern typography reset can tie type to its container, improve line wrapping and use font metrics for details. Browser support and font coverage still matter.

Modern CSS Typography Reset: Container-Based Type, Better Wrapping and Font Controls

A typography reset can now handle details that once needed JavaScript, font-specific offsets or component-level fixes. CSS can balance headings, reduce stranded words, size text against its container and trim extra space from line boxes. The practical value of tidy-typography.css is in bringing those choices together as defaults that teams can adopt selectively.

That flexibility comes with a qualification: browser support varies, and some of the newest text controls remain experimental. The stylesheet’s strongest idea is therefore its modularity. Use the whole set where it fits, or lift individual declarations into a project after checking how they behave with its fonts, content and browser requirements.

Set sensible rendering and fallback behavior

The baseline rules target font rendering and the transition between a webfont and its fallback:

html {
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  font-synthesis: none;

  -moz-text-size-adjust: none;
  -webkit-text-size-adjust: none;
  text-size-adjust: none;
}

body {
  font-size-adjust: from-font;
}

font-synthesis: none prevents a browser from fabricating bold or italic styles when the chosen family lacks those faces. The smoothing and rendering declarations act as hints, with results that vary by platform and browser. They shouldn't be treated as a guarantee of identical text rendering across devices.

Disabling text size adjustment prevents mobile browsers from inflating type in ways that can disrupt a carefully sized layout, including after a device rotates. Meanwhile, font-size-adjust: from-font uses font metrics to make fallback text appear closer in size while the webfont loads. Together, these rules aim to reduce layout surprises, though the fallback face still matters.

Let type respond to its component

Viewport-based fluid sizing has a blind spot: it responds to the screen, not the element containing the text. A heading in a narrow sidebar can grow as if it were in a wide article column. Container query units address that mismatch:

.article-container {
  container-type: inline-size;
}

:root {
  --step-0: clamp(1rem, 0.9rem + 1cqi, 1.125rem);
  --step-3: clamp(2rem, 1.6rem + 3cqi, 2.75rem);
}

Once an element establishes an inline-size query container, its descendants can use cqi, a unit based on that container’s width. In combination with clamp(), it gives text a minimum, a fluid middle value and a maximum. The same scale can then respond to an article column or a 300px sidebar without separate media queries for each component.

The complete stylesheet defines five custom properties, from --step-0 through --step-4. It applies the smallest step to body text, the largest to h1, and an intermediate step to other headings and selected text blocks. Because these values use container units, the wrapper needs to be configured as a container wherever the scale is expected to respond.

Trim line boxes and improve wrapping

CSS line height includes space around the visible letterforms, known as half-leading. That can make text harder to align with nearby elements. text-box-trim and text-box-edge offer a metrics-based way to trim the extra space, measuring from capital letter tops to the alphabetic baseline:

:is(h1, h2, h3, h4, h5, h6), p, blockquote, figcaption, li {
  text-box: trim-both cap alphabetic;
}

This can replace negative-margin adjustments that compensate for a particular font’s metrics. The declaration is part of a newer set of text controls, so projects need to judge whether the untrimmed result remains acceptable in browsers that ignore it. A proposed :heading selector would match heading elements and elements with a heading role, but it has not landed in browsers; any use should sit behind an @supports selector(:heading) check.

Line wrapping gets a similar set of targeted defaults. text-wrap: balance distributes text more evenly across lines and is suited to headings, blockquotes and captions, especially blocks of up to about six lines. For paragraphs and list items, text-wrap: pretty aims to avoid a lonely final word without balancing every line in the block. Both can reduce the need for JavaScript that rearranges text to fix orphans.

:is(h1, h2, h3, h4, h5, h6), blockquote, figcaption {
  text-wrap: balance;
}

p, li, dd {
  text-wrap: pretty;
  max-width: 65ch;
}

[contenteditable="true"] {
  text-wrap: stable;
}

Editable text has a different constraint: as someone types, a rebalanced paragraph can keep reshuffling. text-wrap: stable is intended to prevent that behavior in editable regions. These rules are useful defaults, but each depends on the kind of text being displayed.

Control hyphenation by language and context

Automatic hyphenation depends on knowing the language. A document should declare it, for example with <html lang="en-GB">, so the browser can select the relevant dictionary. Without that information, hyphens: auto has no language basis for choosing breaks.

Even with a language set, default hyphenation may create tiny fragments or a run of consecutive broken lines. The stylesheet tunes those limits and includes prefixed declarations for Safari:

p {
  hyphens: auto;
  hyphenate-limit-chars: 6 3 3;
  hyphenate-character: "‐";
  hyphenate-limit-lines: 2;
  hyphenate-limit-zone: 8%;
  hyphenate-limit-last: always;

  -webkit-hyphens: auto;
  -webkit-hyphenate-limit-before: 4;
  -webkit-hyphenate-limit-after: 3;
  -webkit-hyphenate-limit-lines: 2;
}

pre, code, var, samp, kbd, :is(h1, h2, h3, h4, h5, h6) {
  hyphens: manual;
}

The character limit sets the minimum word length and the minimum characters on either side of a break. Line and zone limits reduce repeated or frequent hyphenation, while the last-word setting protects the final word of a paragraph. Code and headings use manual hyphenation: splitting a code token can change its meaning, and a broken heading can look like a mistake.

Use font features for visual detail

Hanging punctuation addresses a small but visible alignment problem: an opening quotation mark can make the first line look indented compared with the lines below. Applying hanging-punctuation: first allow-end last lets selected opening and closing marks extend slightly beyond the text block. For links, text-decoration-thickness: from-font uses the font’s underline thickness, text-underline-offset provides space around descenders, and text-decoration-skip-ink: auto interrupts the underline where it crosses letter shapes.

Variable fonts add another layer of control. font-optical-sizing: auto lets a font use its optical-size axis to adapt its drawing to the rendered size; some fonts use thinner strokes at display sizes and sturdier ones at smaller sizes. The stylesheet also enables common and contextual ligatures and normal kerning. These settings depend on the font containing the relevant features.

For dark appearances, the stylesheet sets a GRAD grade axis to -50, with a default of zero. Where a font includes that axis, grade can reduce perceived stroke weight without changing glyph width or shape as a weight change might. Fonts without the axis ignore the setting. A separate size-adjust descriptor at the @font-face level can tune a font’s rendered size, which is useful when a monospace face looks mismatched beside prose at the same declared font size.

Make numbers, emoji and quotes intentional

Numerals need different treatment depending on their job. Tables can use tabular-nums lining-nums to give digits equal widths and full cap height, helping columns align. Prose can use oldstyle-nums proportional-nums, letting digits sit more naturally among lowercase letters.

The font-variant-emoji property provides an explicit choice between color and text-style emoji. It can be applied through dedicated classes to the parts of an interface where a particular style is needed, rather than imposed across the whole page. Rendering can still depend on font and platform availability.

Quotation marks can also follow language metadata instead of being hardcoded into content. The stylesheet sets quote pairs for English, British English and French with :lang(), then uses the native q element with open-quote and close-quote. Wrapping language rules in :where() gives them zero specificity, making project overrides easier.

The same principle applies to superscripts and subscripts. Browser defaults often shrink and shift the text, rather than using purpose-built glyphs. Where a typeface provides them, font-variant-position: sub and font-variant-position: super select native forms. The associated rules reset size, line height and vertical alignment so those defaults don't interfere. Font coverage varies, and many families provide proper numerals but not letter forms, so this treatment needs a font-specific check.

Keep reset rules easy to override

Specificity determines how much effort it takes to change a default. :where() contributes zero specificity, which makes it useful for opinionated reset rules that projects should override with ordinary classes. :is() serves a different purpose: it groups selector lists such as all six heading levels, but takes the specificity of its most specific argument.

The stylesheet combines these ideas with progressive enhancement: unsupported declarations are ignored, and the rules can be adopted in pieces. Its larger contribution is a shift in where typography work happens. Container units can replace some component-specific sizing rules; text wrapping can reduce orphan-fixing scripts; and font metrics can replace one-off spacing adjustments.

That doesn't make every declaration a universal default. An editorial page, a marketing site and an editable interface have different needs, and a reset that improves one can frustrate another. Start with the font and content the project actually uses, then check newer wrapping and text-box behavior in the browsers it supports. The best baseline is the one whose fallback remains readable when its newest features aren't available.

Source: Stu Robson · css-tricks.com
MORE

Same Mixtape