Fluid typography: The clamp() magic

Tatiana Fokina, Tbilisi JS Meetup #7

Fluid typography: The clamp() magic

Tbilisi JS Meetup #7, 16/08/2025 📍

Who am I?

Part 1. Let’s find some problems

Expectations

Reality

Our goal

Change the size of the heading so it adapts smoothly and consistently across all breakpoints.

Part 2. In the search for the right approach

Current styles

				h1 {
				  font-size: 3rem;
				}
				@media (width >= 576px) {
				  h1 {
				    font-size: 4rem;
				  }
				}
				@media (width >= 768px) {
				  h1 {
				    font-size: 7rem;
				  }
				}
			

Approach #1: Add extra breakpoints and suitable values for font-size 🙂

New styles

					h1 {
					  font-size: 3rem;
					}
					@media (width >= 460px) {
					  h1 {
					    font-size: 3.2rem;
					  }
					}
				
					@media (width >= 576px) {
					  h1 {
					    font-size: 4rem;
					  }
					}
					@media (width >= 768px) {
					  h1 {
					    font-size: 6rem;
					  }
					}
				
					@media (width >= 700px) {
					  h1 {
					    font-size: 5rem;
					  }
					}
					@media (width >= 992px) {
					  h1 {
					    font-size: 7rem;
					  }
					}
				

Number of breakpoints

Then 🙂

  • ≥ 576px
  • ≥ 768px

Now 🫠

  • ≥ 460px
  • ≥ 576px
  • ≥ 700px
  • ≥ 768px
  • ≥ 992px

Approach #2: Change the heading size not in a responsive way but in a fluid one.

Responsive font size

Fluid font size

How to make the font fluid?

The clamp() definition

The CSS function for setting up the range of values for some CSS properties.

Global browser support is about 96.33%.

clamp() anatomy

				clamp(minValue, prefValue, maxValue);
			
Минимальное максимальное

A preferred value enables smooth text size adjustments.

схема предпочтительного без калькуляций
схема с калькуляциями
чёрная дыра
предпочтительный размер

Preferred width (vw)

v = 100 ( y 2 y 1 ) x 2 x 1

Relative size (rem)

r = ( x 1 y 2 ) ( x 2 y 1 ) x 1 x 2

Unknowns

мем с кнопками

Approach with the formula

объяснение отличий

Part NaN: Don’t touch it when it works

Success in fluid approach

Problems with pixels

If you set the font size in pixels, the browser’s default text size (16 pixels) will be overridden, and text won’t scale when user settings change.

Reset the root size

				html {
				  font-size: 15px;
				}
				h1 {
				  font-size: 1rem;
				}
			
как устроено промежуточное значение

Ratio between larger and smaller values

The max font size must be less than or equal to 2.5 times the min font size 🤯

Why this ratio?

Text with inappropriate sizing doesn’t scale well because it won’t achieve a proper increase due to overlap between zoom and scaling functions.

Good result of calculations
Problematic result of calculations

Who resizes and rescales the UI?

Part 4: Polishing the demo

Rewrite the code

Then

						h1 {
						  font-size: 3rem;
						}
						@media (width >= 460px) {
						  h1 {
						    font-size: 3.2rem;
						  }
						}
						
					

Now

						h1 {
						  font-size: clamp(3rem, 5vw + 1.5rem, 7rem);
						}
					

Final touches: Round the numbers for better code readability ✨

Rounding the numbers

				h1 {
				  font-size: clamp(3rem, 5vw + 1.5rem, 7rem);
				  font-size: clamp(3rem, round(down, 5vw + 1.5rem, 2px), 7rem);
				}
			

Issues with round()

Part 5. Wrapping up

Fluid typography doesn’t replace responsive typography. The choice depends on the specific case.

Fluid typography pros ➕

Fluid typography cons ➖

Fluid typography use cases

Any heading, especially large ones, and texts with noticeable size differences.

`

Calculators

Further reading

Personal links

Doka Guide QR code
Doka Guide
Blog QR code
Personal blog about a11y
Podcast website QR code
Inclusive Pineapple