clamp() magicTatiana Fokina, Tbilisi JS Meetup #7
clamp() magicTbilisi JS Meetup #7, 16/08/2025 📍
Change the size of the heading so it adapts smoothly and consistently across all breakpoints.
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 🙂
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;}}
Approach #2: Change the heading size not in a responsive way but in a fluid one.
calc() and the vw/vh unitsclamp().
clamp() definitionThe CSS function for setting up the range of values for some CSS properties.
clamp() anatomyminValue)prefValue)maxValue).
clamp(minValue, prefValue, maxValue);
A preferred value enables smooth text size adjustments.
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.
html {font-size: 15px;}h1 {font-size: 1rem;}
The max font size must be less than or equal to 2.5 times the min font size 🤯
Text with inappropriate sizing doesn’t scale well because it won’t achieve a proper increase due to overlap between zoom and scaling functions.
h1 {font-size: 3rem;}@media (width >= 460px) {h1 {font-size: 3.2rem;}}…
h1 {font-size: clamp(3rem, 5vw + 1.5rem, 7rem);}
Final touches: Round the numbers for better code readability ✨
h1 {font-size: clamp(3rem, 5vw + 1.5rem, 7rem);font-size: clamp(3rem, round(down, 5vw + 1.5rem, 2px), 7rem);}
round()
Fluid typography doesn’t replace responsive typography. The choice depends on the specific case.
Any heading, especially large ones, and texts with noticeable size differences.