0
0
CssHow-ToBeginner · 3 min read

How to Create Fluid Typography in CSS: Simple Guide

To create fluid typography in CSS, use the clamp() function combining a minimum size, a viewport-based size, and a maximum size. This makes text scale smoothly between screen sizes without becoming too small or too large.
📐

Syntax

The main syntax for fluid typography uses the clamp() function in CSS, which takes three values:

  • Minimum size: The smallest font size allowed.
  • Preferred size: Usually a viewport width unit like vw that scales with screen size.
  • Maximum size: The largest font size allowed.

This ensures the font size grows and shrinks smoothly but stays within readable limits.

css
font-size: clamp(1rem, 2vw, 2.5rem);
💻

Example

This example shows fluid typography on a heading that scales between 1rem and 2.5rem based on the viewport width.

css
html {
  font-size: 16px;
}

h1 {
  font-size: clamp(1rem, 5vw, 2.5rem);
  color: #222;
  text-align: center;
  margin-top: 2rem;
  font-family: Arial, sans-serif;
}
Output
A centered heading that grows and shrinks smoothly as you resize the browser window, never smaller than 16px or larger than 40px.
⚠️

Common Pitfalls

Common mistakes when creating fluid typography include:

  • Using only viewport units (vw) without limits, causing text to become unreadably small or huge on extreme screen sizes.
  • Not setting a base font size, which can cause inconsistent scaling.
  • Using fixed units like px alone, which do not scale responsively.

Always use clamp() to set minimum and maximum sizes for better control.

css
/* Wrong: text can get too small or too large */
h1 {
  font-size: 5vw;
}

/* Right: text size is limited and fluid */
h1 {
  font-size: clamp(1rem, 5vw, 2.5rem);
}
📊

Quick Reference

Tips for fluid typography:

  • Use clamp(min, preferred, max) for smooth scaling.
  • Set min and max in rem or em for accessibility.
  • Use viewport units like vw for the preferred size.
  • Test on different screen sizes to ensure readability.

Key Takeaways

Use CSS clamp() to create fluid typography that scales between set minimum and maximum sizes.
Combine fixed units like rem with viewport units like vw inside clamp() for responsive text.
Avoid using only viewport units to prevent text from becoming too small or too large.
Test your typography on various screen sizes to ensure it remains readable and balanced.