Here's a neat little trick:
You can use the clamp() CSS function to make font-size responsive and fluid!
It works by "clamping", or restricting, a flexible value between a minimum and a maximum range.
Here's how to use it:
- Choose a minimum value: E.g.
16px - Choose a maximum value: E.g.
34px - Choose a flexible value: E.g.
5vw
h1 {
font-size: clamp(16px, 5vw, 34px);
}
In this example, the h1 font-size value will be 5% of the viewport width. But only if that value is bigger than 16px and smaller than 34px.
For instance, if your viewport width is 300px, your 5vw value will be equal to 15px. However, you clamped that font-size value to a minimum of 16px, so that is what is going to be.
On the other hand, if your viewport width is 1400px, you 5vw will be a whooping 70px! But luckily you clamped that maximum value to 34px, so it won't grow past that.
Oh, and it also works with padding! And margin!
And literally any other property that accepts a length, frequency, angle, time, percentage, number, or integer!
Browser support is also pretty good if you don't need to support IE/older Safari versions, look:
I love modern CSS. What are your thoughts on clamp()?
EDIT: Be sure to check Darshak's suggestion in the comments if you need to add an alternative implementation for older Safari!
Cover photo by Matt Artz on Unsplash
Thanks to Madza for the name of this series 😄
Hey, let's connect 👋
Follow me on Twitter and let me know you liked this article!
And if you really liked it, make sure to share it with your friends, that'll help me a lot 😄



Latest comments (23)
Where it really shines is if you do something like
so that you can modify the speed, and domain, of the responsive scaling.
Amazing, few days ago i wrote a web page using css and html, had to add more then 6 media queries because the button and h1 positioned absolute to background pic were going down on smaller viewports 👐
This is genius, never heard of clamp before now.
Thanks!
For older browsers, we could do something like:
This should at least work on older Safari. As for IE, well, it doesn't support
@supports. :|Great suggestion, Darshak! I wish I could pin comments
Hey Vitor, you could edit the post adding this suggestion. Makes sense to increase support with little effort.
Well, it's more a matter of graceful degradation than support increase but your idea is great, I'll do it right now!
Awesome! Thank you for the article and the examples. Much appreciated!
Reading this reminded me of a Github project I saw a couple months ago that allows you to clamp "a line" of text so that the same number of words stay on it regardless of font size.
github.com/tvanc/lineclamp
Damn, this is going to forever change the way I style my content.
Hmmm, can this be pollyfilled in anyway?
EDIT: found a good solution to use it in older browsers via sass bronco.co.uk/our-ideas/creating-a-...
The tricky part is that the browser also needs to support the
vw. Which our old friend IE also doesn't and never will. :(I believe the only way to polyfill it would be by using something with JS, like fittextjs.com/
Thank you so much for sharing this
This will really improve my responsive designing skills
looks nice, but i won't use if because of the low support. Maybe in a few years
It's crazy how powerful these functions get when you mix them in with modern modules, too, like grid.