Course contentsShow
← Web Development
Lesson 1361 of 2,98224. CSS Architecture and MaintainabilityFree lesson

Spacing and Typography Theme Tokens

Define spacing scales and typography systems using custom properties to maintain visual consistency across your application.

Spacing and Typography Theme Tokens

You'll learn how to establish consistent spacing and typography scales using CSS custom properties, creating a reusable design system that keeps your interface harmonious and maintainable.

The Core Idea

While color tokens handle visual aesthetics, spacing and typography tokens govern structure and readability. Instead of scattering arbitrary values like margin: 23px or font-size: 17px throughout your CSS, you define a systematic scale of custom properties that everyone (including future-you) can reference consistently.

Think of it like musical notes: rather than every musician inventing their own pitch system, everyone agrees on the same scale. When you need "a medium gap between elements," you reach for var(--space-md) rather than inventing 28px on the spot.

Setting Up Your Spacing Scale

:root {
  /* Spacing tokens - often based on multiples */
  --space-xs: 0.25rem;  /* 4px */
  --space-sm: 0.5rem;   /* 8px */
  --space-md: 1rem;     /* 16px */
  --space-lg: 1.5rem;   /* 24px */
  --space-xl: 2rem;     /* 32px */
  --space-2xl: 3rem;    /* 48px */
}

Typography System

:root {
  /* Type scale tokens */
  --font-size-sm: 0.875rem;   /* 14px */
  --font-size-base: 1rem;     /* 16px */
  --font-size-lg: 1.125rem;   /* 18px */
  --font-size-xl: 1.5rem;     /* 24px */
  
  /* Line height tokens */
  --line-height-tight: 1.2;
  --line-height-normal: 1.5;
  --line-height-loose: 1.8;
}

Applying Tokens

.card {
  padding: var(--space-lg);
  margin-bottom: var(--space-md);
  font-size: var(--font-size-base);
  line-height: var(--line-height-normal);
}

Key Takeaway: Spacing and typography tokens create a shared vocabulary for layout and text styling, ensuring visual consistency while making it trivial to adjust your entire design system from a single source of truth.