🎉 75% of content is free forever — Unlock Premium from $10/mo →
CW
NEWSLIVESearch All Content
đŸ’ŧ Servicesâ„šī¸ Aboutâœ‰ī¸ ContactView Pricing Plansfrom $10

CSS Typography: Scales, Fonts and Rhythm

Web StylesđŸŸĸ Free Lesson

CSS Typography: Scales, Fonts and Rhythm

Typography carries more of the interface than any other visual decision: hierarchy, rhythm, density and tone all read through letterforms before a word is understood. CSS gives you precise control over every lever — size, leading, tracking, font loading, numeric alignment — and the modern platform adds fluid scaling and text balancing that used to require a designer's hand. This lesson assembles a durable type system: a modular scale that interpolates with the viewport, font loading that does not shift layout, vertical rhythm that holds across components, and the OpenType features that turn good text into professional text. Treat the system as shared infrastructure: once the scale and rhythm exist, every new page typesets itself correctly by pulling from the same tokens.

Building a modular type scale

A modular scale is a ratio applied to a base size — 1.2 for a tight interface scale, 1.25 or 1.333 for more editorial contrast. Instead of fixed pixel steps, wrap each step in clamp() so the type grows smoothly between a readable floor and a sensible ceiling as the viewport changes. Define the steps once as tokens on :root, then assign them to elements.

:root {
  --ratio: 1.25;
  --size-step--2: 0.64rem;
  --size-step--1: 0.8rem;
  --size-step-0: 1rem;
  --size-step-1: 1.25rem;
  --size-step-2: 1.563rem;
  --size-step-3: 1.953rem;
  --size-step-4: 2.441rem;

  --step--2: clamp(0.72rem, 0.68rem + 0.2vw, var(--size-step--2));
  --step--1: clamp(0.86rem, 0.82rem + 0.2vw, var(--size-step--1));
  --step-0: clamp(1rem, 0.95rem + 0.25vw, var(--size-step-0));
  --step-1: clamp(1.18rem, 1.1rem + 0.4vw, var(--size-step-1));
  --step-2: clamp(1.4rem, 1.28rem + 0.6vw, var(--size-step-2));
  --step-3: clamp(1.68rem, 1.5rem + 0.9vw, var(--size-step-3));
  --step-4: clamp(2rem, 1.72rem + 1.4vw, var(--size-step-4));
}

body {
  font-size: var(--step-0);
  line-height: 1.6;
  font-family: system-ui, sans-serif;
  text-wrap: pretty;
}

h1, h2, h3, h4 {
  line-height: 1.15;
  font-weight: 650;
  text-wrap: balance;
  margin-block: 0 0.5em;
}

h1 { font-size: var(--step-4); letter-spacing: -0.02em; }
h2 { font-size: var(--step-3); letter-spacing: -0.015em; }
h3 { font-size: var(--step-2); }
h4 { font-size: var(--step-1); }

p, li { max-inline-size: 68ch; }

.lede {
  font-size: var(--step-1);
  color: #475569;
  margin-block-end: 1.5em;
}

.caption {
  font-size: var(--step--1);
  color: #64748b;
}

Two platform features do real work here. text-wrap: balance asks the browser to even out line lengths in headings, which kills the widow problem where the last line contains a single word; text-wrap: pretty applies a gentler version for body paragraphs. Negative letter-spacing at large display sizes is the classic typographic adjustment that keeps big headings from looking loose.

Font loading without the flash

Web fonts are the largest performance liability in typography. The browser will not paint text in a face it has not downloaded, so a naive @font-face produces either invisible text (FOIT) while the file loads or a flash of fallback text swapping out (FOUT) once it arrives. font-display: swap chooses FOUT deliberately — fallback text renders immediately, and the real face replaces it when ready — which is almost always the right trade for body copy.

@font-face {
  font-family: 'Brand Sans';
  src: url('/fonts/brand-sans-var.woff2') format('woff2-variations');
  font-weight: 400 700;
  font-style: normal;
  font-display: swap;
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+2013-2014, U+2026;
}

@font-face {
  font-family: 'Brand Sans';
  src: url('/fonts/brand-sans-italic-var.woff2') format('woff2-variations');
  font-weight: 400 700;
  font-style: italic;
  font-display: swap;
}

@font-face {
  font-family: 'Brand Mono';
  src: url('/fonts/brand-mono.woff2') format('woff2');
  font-weight: 500;
  font-display: optional;
}

:root {
  --font-sans: 'Brand Sans', system-ui, sans-serif;
  --font-mono: 'Brand Mono', ui-monospace, monospace;
}

body { font-family: var(--font-sans); }

/* Size-adjusted fallback so the swap moves almost nothing */
@font-face {
  font-family: 'Brand Sans Fallback';
  src: local('Arial');
  size-adjust: 104%;
  ascent-override: 90%;
  descent-override: 22%;
  line-gap-override: 0%;
}

.hero {
  font-size: var(--step-4);
  font-weight: 700;
  text-wrap: balance;
}

pre, code, kbd {
  font-family: var(--font-mono);
  font-size: 0.925em;
}

Three details separate production font loading from the tutorial version. Variable fonts with a declared font-weight range let one file serve every weight, cutting requests. A size-adjusted fallback face reshapes the system font so the swap moves pixels as little as possible. And font-display: optional — used above for the mono face — tells the browser to skip the swap entirely if the font is not ready within the first few moments, which is right for faces that appear only in secondary UI.

Preload the font actually used in the first viewport, and only that one. Every preloaded font competes for bandwidth with your hero image, and LCP suffers when you preload three weights of a face that appears only in the footer.

Vertical rhythm with logical margins

Vertical rhythm — consistent spacing multiples across headings, paragraphs, lists and figures — is what makes a page feel typeset rather than assembled. The modern approach uses the adjacent-sibling combinator with logical margin properties, so the rhythm survives RTL layouts and flex/grid contexts where margins do not collapse:

.prose {
  --baseline: 1.5rem;
  display: flow-root;
}

.prose > * + * {
  margin-block-start: var(--baseline);
}

.prose > h1 + p,
.prose > h2 + p { margin-block-start: calc(var(--baseline) * 0.5); }

.prose > h2,
.prose > h3 {
  margin-block-start: calc(var(--baseline) * 2);
}

.prose > h2 + ul,
.prose > h3 + ul { margin-block-start: calc(var(--baseline) * 0.75); }

.prose blockquote {
  margin-block: var(--baseline);
  padding-inline-start: 1rem;
  border-inline-start: 3px solid #cbd5e1;
  color: #475569;
  font-style: italic;
}

.prose hr {
  border: 0;
  margin-block: calc(var(--baseline) * 2);
  border-block-start: 1px solid #e2e8f0;
}

.prose figure {
  margin-block: var(--baseline);
  margin-inline: 0;
}

.prose figcaption {
  font-size: var(--step--1);
  color: #64748b;
  margin-block-start: 0.5rem;
}

Because every space derives from one baseline variable, tightening the whole document is a single token change. The display: flow-root on the container contains floats without the clearfix hacks older stylesheets carried.

Truncation, clamping and overflow control

Real interfaces need text to fit fixed regions: card titles on one or two lines, table cells with ellipsis, feed previews capped at three lines. The tools differ subtly, and picking the wrong one silently breaks screen-reader access to the truncated content.

.truncate-one {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.clamp-three {
  display: -webkit-box;
  -webkit-line-clamp: 3;
  line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.card-title {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  min-inline-size: 0;
}

.table-cell {
  max-inline-size: 24ch;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.full-text {
  position: absolute;
  inline-size: 1px;
  block-size: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

Truncation depends on -webkit-box layout, which is a quirk of engine history rather than the standard overflow-wrap model; it works everywhere today but deserves a comment in your stylesheet. When clamped text conveys essential meaning, keep an accessible full version — the visually-hidden pattern above or an expand control — because ellipsis is a visual affordance, not an information strategy. Pair the clamp with a sensible overflow-wrap fallback so long unbroken tokens inside the visible lines break instead of forcing a horizontal scroll inside the card.

OpenType features and numeric alignment

Professional typesetting leans on OpenType features that browsers expose directly. Ligatures join character pairs that would collide, font-variant-numeric: tabular-nums gives every digit identical width so columns of numbers align, small caps and stylistic sets add tonal control:

body {
  font-variant-ligatures: common-ligatures;
  font-feature-settings: 'kern' 1, 'liga' 1;
}

.price-table td,
.price-table th {
  font-variant-numeric: tabular-nums;
  text-align: end;
}

.stat-value {
  font-variant-numeric: tabular-nums proportional-nums;
  font-feature-settings: 'ss01' 1;
}

.eyebrow {
  font-size: var(--step--1);
  text-transform: uppercase;
  letter-spacing: 0.12em;
  font-variant-caps: all-small-caps;
  color: #2563eb;
  font-weight: 600;
}

.drop-cap::first-letter {
  float: inline-start;
  font-size: 3.2em;
  line-height: 0.8;
  padding-inline-end: 0.1em;
  font-weight: 700;
}

Table alignment is the feature that pays for itself immediately: proportional figures look elegant in prose but destroy column alignment in price tables and dashboards, and the switch is one declaration. Verify features against the actual font file — not every web font ships small caps or stylistic sets, and missing features fall back silently. A quick rendered sample of the characters you care about, inspected once per font upgrade, prevents the disappointment of discovering missing glyphs in production after a redesign ships to every template at once.

Measuring line length and readability

Scale and rhythm set the structure; measure finishes it. Body text reads best between roughly 45 and 75 characters per line — the max-inline-size: 68ch cap used earlier is a sane default, tightened for dense interfaces and relaxed for wide editorial layouts. Pair the measure with a line height near 1.5 for body copy, closer to 1.15 for display sizes, and slightly looser for small captions where the eye needs help tracking lines. Font choice matters too: humanist sans faces read well at interface sizes, while serif faces often carry long-form articles better at 18 pixels and above.

Contrast completes the checklist. Body text should clear a 4.5 to 1 contrast ratio against its background, large display text at least 3 to 1, and muted captions must be measured like any other text rather than excused for being decorative. Test the full stack at 200 percent browser zoom and with the OS text size raised, because a scale that only holds at the design viewport is a screenshot, not a system. When the measure, rhythm and contrast survive those checks, typography stops being a per-page decision and becomes shared infrastructure that every new page inherits for free.

Common Mistakes

Preloading every font weight and style used anywhere on the site

Fix: Preload only the face visible in the first viewport; let secondary faces load at normal priority with font-display swap.

Using fixed px sizes at every breakpoint instead of a fluid scale

Fix: Define clamp() steps once on :root and assign them, so type interpolates smoothly with no jump at any width.

Truncating essential text with ellipsis and leaving no accessible full version

Fix: Pair clamped previews with a visually hidden full string or an expand control.

Forgetting tabular-nums on dashboards, so number columns wiggle as values change

Fix: Apply font-variant-numeric: tabular-nums to any table of figures or live counters.

Setting body line-height to 1 and spacing paragraphs with huge margins

Fix: Use line-height near 1.5 and a baseline-based sibling margin so rhythm holds across components.

Accepting FOUT without checking the fallback face metrics

Fix: Use size-adjust and ascent-override on a fallback @font-face so the swap moves as few pixels as possible.

Key Takeaways

  • A modular scale built with clamp() tokens gives the whole site fluid, consistent type without per-breakpoint font rules.
  • font-display swap plus a metric-adjusted fallback minimizes both invisible-text stalls and swap layout shift.
  • Vertical rhythm falls out of one baseline token combined with adjacent-sibling and logical margin rules.
  • Line clamping and ellipsis solve fixed-region text, but essential truncated content needs an accessible full version.
  • OpenType features — tabular numerals, ligatures, small caps — separate polished interfaces from merely adequate ones.
  • Aim for 45–75 characters per line, test at 200 percent zoom, and treat the type scale as shared infrastructure rather than per-page styling.
CW

ChatWhole Learn Editorial Team

Written by experienced data scientists, software engineers, and educators. All content is reviewed by industry professionals to ensure accuracy and relevance.

Last reviewed: 2026-10-12

—
☆☆☆☆☆
0 ratings

Rate & Feedback

Need Expert Web Development Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement