Skip to the stories
AI & ML

CSS Typography Resets Now Handle Wrapping, Fluid Type and Font Metrics

  • Stu Robson
  • 676 reads

A modern CSS reset can move typography fixes into the browser, from container-based sizing to smarter wrapping, with support and font limits to consider.

CSS Typography Resets Now Handle Wrapping, Fluid Type and Font Metrics

A typography reset can now do more than clear default margins and set a line height. CSS has gained tools for sizing type against its component, improving line breaks, trimming line box space and using a font’s own metrics. Together, they can replace small JavaScript fixes and hand-tuned offsets—but browser support and font coverage still determine which rules belong in a project’s baseline.

tidy-typography.css collects these techniques as a set of defaults. Teams can adopt it as a whole or lift individual declarations where they solve a specific layout problem. The practical shift is that more typographic decisions can live alongside the stylesheet, with unsupported declarations ignored by browsers that don’t recognize them.

Set type against the component

Viewport-based fluid typography scales with the browser window, even when a heading sits in a narrow sidebar. Container query units give the scale a more relevant reference: the width of the element’s containing component.

.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);
}

Setting container-type: inline-size makes the wrapper a query container. Its children can then use cqi, which represents a fraction of that container’s inline size. Used in clamp(), it lets a heading scale within an article column or a 300px sidebar without a component-specific media query. The full reset defines five size steps, from --step-0 through --step-4, with minimum and maximum values to keep the scale bounded.

That changes the unit of design: type responds to the space it occupies, rather than treating every component as if it were the viewport. It’s a useful fit for reusable layouts, provided the container is defined at the right level of the component tree.

Rendering, fallbacks and vertical alignment

The reset also addresses how text is rendered when the intended font or face isn’t available. font-synthesis: none prevents the browser from fabricating bold or italic styles when the chosen family lacks those faces. Antialiasing and text-rendering: optimizeLegibility act as rendering hints, primarily for WebKit, and their effects vary by platform.

Disabling text-size-adjust prevents mobile browsers from inflating text in a way that can disrupt a deliberately sized layout after rotation. The companion font-size-adjust: from-font uses font metrics to reduce apparent size shifts when a fallback font is replaced by a webfont. These choices help control rendering, but they don’t make different fonts metrically identical.

Line boxes include space above capital letters and below the baseline, known as half-leading. That extra space can frustrate tight alignment between text and neighboring elements. text-box-trim: trim-both removes it at both ends, while text-box-edge: cap alphabetic measures from the cap height to the alphabetic baseline. The shorthand is text-box: trim-both cap alphabetic. It offers a font-aware alternative to adjusting individual elements with negative margins.

The proposed :heading selector would match heading elements and elements with role="heading", but it hasn’t landed in browsers. Any use should sit behind @supports selector(:heading). The complete reset instead groups the six heading elements with :is().

Improve line breaks and hyphenation

Short final lines can make headings look unbalanced, while a paragraph ending in a single stranded word can be distracting. text-wrap: balance distributes text more evenly across lines and is best suited to shorter blocks, such as headings, blockquotes and captions. text-wrap: pretty improves paragraph endings without trying to balance the whole block. Both can replace JavaScript orphan-handling scripts.

Editable text has a different requirement: inserting each new word shouldn’t continually rearrange the paragraph. Applying text-wrap: stable to [contenteditable="true"] keeps wrapping steadier as someone types. The reset also gives paragraphs, list items and description terms a maximum width of 65ch.

Automatic hyphenation depends on the browser’s dictionary for the document language. A page needs an accurate lang attribute, such as <html lang="en-GB">, or the browser won’t have the language information it needs. Even with that attribute, default hyphenation may leave awkward fragments or create several hyphenated lines in a row.

CSS can limit the smallest word that may break, the minimum characters on either side of a break, and the number of consecutive hyphenated lines. hyphenate-limit-zone reduces how readily words break near the line edge; hyphenate-limit-last: always keeps the final word of a paragraph intact. Safari also needs the older prefixed hyphenation properties for equivalent controls, so a stylesheet supporting it should declare both sets. Code, variables, sample text and headings are set to hyphens: manual: splitting a code token can alter its meaning, and a broken heading is usually unwelcome.

Polish punctuation and font details

hanging-punctuation can let opening or closing punctuation sit just outside a text block’s edge. The reset applies first, allow-end and last to paragraphs and blockquotes, reducing the visual indentation caused by a quotation mark at the start of a line and avoiding a forced wrap for punctuation at the right edge.

Link underlines get three font-aware settings: text-decoration-thickness: from-font uses the font’s intended underline thickness, text-underline-offset provides room for descenders, and text-decoration-skip-ink: auto leaves gaps where a descender crosses the line.

For variable fonts, font-optical-sizing: auto lets a font adjust its optical size axis to the rendered text size. The reset also enables common and contextual ligatures and font kerning. In dark mode, it sets a grade axis, GRAD, to -50. Light text on a dark background can appear heavier than the same font weight on a light background; a grade axis can reduce that perceived weight without changing glyph width or shape. Fonts without the axis ignore the setting.

The size-adjust descriptor in @font-face addresses another metric mismatch. It scales a font’s rendered output without changing the declared font-size, which can help a monospace font sit more comfortably beside prose when their x-heights differ.

Choose number, emoji and quote styles by context

Tables benefit from fixed-width digits that align in columns. The reset uses tabular-nums lining-nums for tables and elements marked .tabular. In prose, oldstyle-nums proportional-nums lets numerals sit more naturally among lowercase letters.

font-variant-emoji makes the choice between color and text-style emoji explicit. Applying emoji or text to a targeted container avoids imposing one presentation across the entire page, where platform defaults may otherwise differ.

Quotation marks vary by locale. The quotes property can set the expected pairs for English, British English and French, while the native q element inserts them using open-quote and close-quote. Rules based on :lang() let the same markup render according to its language. Wrapping them in :where() gives the defaults zero specificity, making them easier to override.

Browsers often create subscript and superscript appearances by shrinking text and shifting its baseline. Where a font supplies purpose-built glyphs, font-variant-position: sub or super can use them instead. The accompanying resets return font size, line height and vertical alignment to neutral values, so those properties don’t fight the glyph’s design. Many fonts provide only numeral forms, so check the selected family before applying this across general text.

Keep defaults easy to override

A reset is a collection of opinions, and specificity controls how costly those opinions are to change. :where() always has zero specificity, which makes it useful for baseline styles such as heading margins. :is() serves a different purpose: it shortens repeated selector lists, such as the six heading levels, but takes the specificity of its most specific argument.

The full tidy-typography.css stylesheet combines these rules with a centered 800px body, fluid type variables, line-height settings, wrapping, hyphenation, numeric styles, locale-aware quotes, native subscript and superscript glyphs, and ink-aware link underlines. Its declarations are intended to degrade gracefully where unsupported, and the stylesheet can be adopted in parts.

Support remains uneven, particularly for newer wrapping and text-box features. A reset only helps if its fallback rendering is acceptable, and different pages make different demands: editable interfaces, long-form reading and short marketing pages won’t necessarily want the same defaults. The useful next step is to choose rules against the project’s fonts, content and browser range, then revisit the newer properties as support improves.

Source: Stu Robson · css-tricks.com

From the same set