DEV Community

Cover image for Write Fewer Media Queries With the Clamp() CSS Function
Vitor Paladini
Vitor Paladini

Posted on • Updated on • Originally published at paladini.dev

Write Fewer Media Queries With the Clamp() CSS Function

Here's a neat little trick:

You can use the clamp() CSS function to make font-size responsive and fluid!

Gif depicting a clamp() example with font-size

It works by "clamping", or restricting, a flexible value between a minimum and a maximum range.

Here's how to use it:

  1. Choose a minimum value: E.g. 16px
  2. Choose a maximum value: E.g. 34px
  3. Choose a flexible value: E.g. 5vw
h1 {
  font-size: clamp(16px, 5vw, 34px);
}
Enter fullscreen mode Exit fullscreen mode

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!

Gif depicting a clamp() example with padding

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:

Browser support table

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 ๐Ÿ˜„

Discussion (23)

Collapse
dar5hak profile image
Darshak Parikh

For older browsers, we could do something like:

h1 {
  font-size: 16px;
}

@supports (font-size: clamp(16px, 5vw, 34px)) {
  h1 {
    font-size: clamp(16px, 5vw, 34px);
  }
}
Enter fullscreen mode Exit fullscreen mode

This should at least work on older Safari. As for IE, well, it doesn't support @supports. :|

Collapse
vtrpldn profile image
Vitor Paladini Author

Great suggestion, Darshak! I wish I could pin comments

Collapse
erickpetru profile image
Erick Eduardo Petrucelli

Hey Vitor, you could edit the post adding this suggestion. Makes sense to increase support with little effort.

Thread Thread
vtrpldn profile image
Vitor Paladini Author

Well, it's more a matter of graceful degradation than support increase but your idea is great, I'll do it right now!

Collapse
jeremysawesome_86 profile image
Jeremy Smith

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

Collapse
clifton893 profile image
Clifton Long Jr.

Dang, that's awesome.

It's wild seeing how CSS is evolving.

Collapse
vtrpldn profile image
Vitor Paladini Author

Yeah! My first experiences with CSS were full of floats and clearfixes so I'm glad to have modern tools like CSS functions, custom properties and Flexbox now ๐Ÿ™‚

Collapse
qm3ster profile image
Mihail Malo • Edited

Where it really shines is if you do something like

* {
  font-size: clamp(1rem, calc(5vw+0.8rem), 4rem)
}
Enter fullscreen mode Exit fullscreen mode

so that you can modify the speed, and domain, of the responsive scaling.

Collapse
keizzmann profile image
keizzmann๐Ÿ‘ป๐Ÿ’€๐Ÿ˜Žโšก๐ŸŽŽ๐ŸŽน

Thank you so much for sharing this

This will really improve my responsive designing skills

Collapse
peterwitham profile image
Peter Witham

This is genius, never heard of clamp before now.
Thanks!

Collapse
chin98edwin profile image
chin98edwin

Damn, this is going to forever change the way I style my content.

Collapse
scriptedpixels profile image
๐Ÿ‘จ๐Ÿพโ€๐Ÿ’ป Kam Banwait • Edited

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-...

Collapse
vtrpldn profile image
Vitor Paladini Author

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/

Collapse
ggenya132 profile image
Eugene Vedensky

It's crazy how powerful these functions get when you mix them in with modern modules, too, like grid.

Collapse
draer profile image
Michaล‚ Murawski

Damn! Some time ago I needed to implement this kind of thing. I only found JS libraries that did not meet my expectations. I wonder how would it have been with this feature :)

Collapse
vtrpldn profile image
Vitor Paladini Author

I believe you're talking about fittextjs.com, right? I also had to do something similar in the past and got super bummed out with the fact that I had to use JS for such a small thing

Collapse
dailydevtips1 profile image
Chris Bongers

Nice article Vitor!
Must really start using this!

Collapse
vtrpldn profile image
Vitor Paladini Author

Thanks, Chris. You should, yeah! It is so simple and works so well that makes you wonder why we didn't have it before, haha

Collapse
rcsnellink profile image
Ramon Snellink

Woo, that's cool! Surely going to try this on my next project.

Collapse
deta19 profile image
mihai

looks nice, but i won't use if because of the low support. Maybe in a few years

Collapse
green600 profile image
Liegeois Jules

Thank you !!!! <3

Collapse
vtrpldn profile image
Vitor Paladini Author

Happy to help! ๐Ÿ˜„

Collapse
uzair004 profile image
Muhammad Uzair • Edited

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 ๐Ÿ‘