Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save dragontheory/647f024da0d1c3f6079fde767e2a52ee to your computer and use it in GitHub Desktop.

Select an option

Save dragontheory/647f024da0d1c3f6079fde767e2a52ee to your computer and use it in GitHub Desktop.
Quick practical best practices on fluid typography using modern CSS.

Image

Short, practical guidance on fluid typography using modern CSS — focusing on concise principles and minimal, real code examples using clamp(), viewport units, and container queries. References link to authoritative specs and articles; snippets are ready‑to‑use. (Smashing Magazine)


1) Why Fluid Typography?

Instead of fixed font sizes or hardcoded breakpoints, fluid typography lets text scale smoothly based on the viewport or container size — improving readability and reducing maintenance. Modern CSS eliminates many media queries. (Smashing Magazine)


2) clamp() — Core Tool for Fluid Scaling

Concept

clamp(min, preferred, max) sets a font size that:

  • never goes below the minimum
  • scales fluidly via a viewport‑based preferred value
  • never exceeds the maximum This allows smooth scaling between small and large screens without breakpoint logic. (MDN Web Docs)

Basic Fluid Type

h1 {
  font-size: clamp(1.5rem, 5vw, 3rem);
}
p {
  font-size: clamp(1rem, 2.5vw, 1.25rem);
}
  • 1.5rem / 1rem: lower bound for accessibility/readability
  • 5vw / 2.5vw: 5% / 2.5% of viewport width
  • 3rem / 1.25rem: upper bound so text doesn’t grow too large

Fluent Scale Across Viewports

:root {
  --base-min: 1rem;
  --base-max: 1.25rem;
}
body {
  font-size: clamp(var(--base-min), 2vw, var(--base-max));
}

Using CSS variables simplifies consistent type scaling across components. (Smashing Magazine)


3) Viewport Units (vw, vh)

Viewport units allow scaling based on window size. Best practice is to combine viewport units with fixed units inside clamp() so text stays readable on extremes. Example:

h2 {
  font-size: clamp(1.25rem, 4vw, 2rem);
}
  • Without clamp, using just 4vw might produce overly large or tiny text at extremes. (Smashing Magazine)

4) Container Queries — Component‑Scoped Fluidity

Container queries let typography adapt based on container size, not just viewport — great for modular components. (Wikipedia)

Set up a container

.card {
  container-type: inline-size;
}

Fluid inside container

@container (min-width: 30rem) {
  .card-title {
    font-size: 2rem;
  }
}
@container (max-width: 29.99rem) {
  .card-title {
    font-size: 1.5rem;
  }
}

When the .card grows/shrinks, title size adapts based on its space — useful in components that float, stack, or shift layouts.

More advanced patterns combine clamp() with container queries:

@container (min-width: 30rem) {
  .card-title {
    font-size: clamp(1.5rem, 3vw, 2.25rem);
  }
}

5) Best Practices (Practical Rules)

Keep type accessible & readable

  • Start with a meaningful minimum value (1rem or larger). (web.dev)
  • Limit the scaling range (e.g., 1–1.5×) so text remains legible.
  • Use relative units (rem, em) for min/max to respect user preferences and zoom settings. (web.dev)

Minimize breakpoints

  • Prefer fluid scaling (with clamp() and viewport units) over many media queries. (Smashing Magazine)

Combine with semantic structure

  • Use fluid type on headings, body text, and components without repetitive breakpoints.
  • Optionally layer container queries for elements in variable layout contexts. (Wikipedia)

6) Resources & Specs (Authoritative)

  • MDN clamp() reference: specs and examples of min/max/viewport math. (MDN Web Docs)
  • Smashing Magazine: Fluid typography principles and parameter tips. (Smashing Magazine)
  • Container Queries (CSS Conditional Rules Level 5): component‑scoped responsive rules. (Wikipedia)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment