Custom Properties for Fluid Type
What you'll learn: How to store fluid typography calculations in CSS custom properties so you can reuse them consistently across your entire design system.
The Core Idea
You've learned to create fluid typography with clamp() and viewport units. But repeating complex calculations like clamp(1rem, 0.5rem + 2vw, 3rem) throughout your CSS is tedious, error-prone, and hard to maintain. CSS custom properties (also called CSS variables) let you define your fluid type scales once and reference them everywhere.
Think of custom properties as named containers for your fluid typography values. Instead of copying the same clamp() calculation to every heading, paragraph, and button, you store it in a variable and call that variable by name.
How It Works
You define custom properties in your :root selector (making them available globally), then reference them with the var() function:
:root {
--fluid-base: clamp(1rem, 0.9rem + 0.5vw, 1.25rem);
--fluid-h1: clamp(2rem, 1.5rem + 2vw, 3.5rem);
--fluid-h2: clamp(1.5rem, 1.25rem + 1vw, 2.5rem);
--fluid-small: clamp(0.875rem, 0.85rem + 0.25vw, 1rem);
}
body {
font-size: var(--fluid-base);
}
h1 {
font-size: var(--fluid-h1);
}
h2 {
font-size: var(--fluid-h2);
}
Now your entire type scale lives in one place. Need to adjust the fluid behavior? Change the custom property, and every element using it updates automatically.
Why This Matters
Custom properties create a single source of truth for your fluid typography. You maintain consistency, reduce duplication, and make your design system scalable and maintainable.
Key Takeaway: Store fluid typography values in CSS custom properties to create a centralized, reusable type scale that keeps your design system consistent and easy to maintain.