Lab 09: CSS Typography

Time: 30 minutes | Level: Practitioner | Docker: docker run -it --rm node:20-alpine sh

Overview

Master modern web typography: variable fonts with font-variation-settings, font-display strategies, system font stacks, fluid type scales, text-wrap: balance, optical sizing, and multi-line truncation.


Step 1: Variable Fonts

Variable fonts encode multiple type styles in a single file, controlled via CSS axes.

/* @font-face for variable font */
@font-face {
  font-family: 'Inter';
  src: url('/fonts/Inter.var.woff2') format('woff2 supports variations'),
       url('/fonts/Inter.var.woff2') format('woff2'); /* fallback */
  font-weight: 100 900;    /* range for variable font */
  font-style: normal;
  font-display: swap;
}

/* Standard axes (registered): */
/* wght — weight: 100 to 900 */
/* wdth — width: condensed to expanded */
/* ital — italic: 0 or 1 */
/* slnt — slant: -90 to 90 degrees */
/* opsz — optical size */

/* font-variation-settings: 'axis' value */
.heading {
  font-family: 'Inter', sans-serif;
  font-variation-settings:
    'wght' 800,    /* bold */
    'opsz' 32;     /* optical size for display text */
}

.body-text {
  font-variation-settings:
    'wght' 400,
    'opsz' 14;
}

/* Prefer font-weight for standard axes (it maps to wght) */
h1 { font-weight: 800; }  /* browser maps to wght 800 */
p  { font-weight: 400; }

/* Custom axes (use 4-char uppercase tags) */
.display {
  font-variation-settings: 'GRAD' 150, 'SOFT' 0;
}

/* Animate variable font */
@keyframes font-pulse {
  0%, 100% { font-variation-settings: 'wght' 400; }
  50%       { font-variation-settings: 'wght' 800; }
}

.animated-text {
  animation: font-pulse 2s ease-in-out infinite;
}

💡 Variable fonts can reduce HTTP requests (one file instead of 4-8 font weights) and save bandwidth. Check support at variablefonts.io.


Step 2: font-display

Controls how fonts behave while loading:

Recommendations:

  • swap — Body text: readability over flash

  • optional — Non-critical display fonts

  • fallback — Balance between appearance and performance


Step 3: System Font Stacks

Use system fonts for zero-latency text rendering:


Step 4: Fluid Type Scale with clamp()


Step 5: text-wrap and Line Control


Step 6: font-optical-sizing


Step 7: Letter Spacing & Advanced Features


Step 8: Capstone — Type Scale Generator

📸 Verified Output:


Summary

Property
Values
Purpose

font-variation-settings

'wght' 800

Variable font axes

font-display

swap, optional

Loading behavior

system-ui

Font keyword

Native system font

clamp()

min, preferred, max

Fluid sizing

text-wrap: balance

Even line distribution

-webkit-line-clamp

integer

Multi-line truncation

font-optical-sizing

auto, none

Size-appropriate letterforms

hyphens: auto

Automatic hyphenation

font-kerning

auto, normal

Letter pair spacing

font-variant-numeric

oldstyle-nums

OpenType numerals

Last updated