Adrian Perea
Posted on June 17, 2020
The world of CSS is very wild. Let's help each other out by sharing things that give it some order!
I'll start!
You can ditch media queries for dynamically changing font-sizes by use of fluid typography. By using min
, max
, and viewport units
, we can dynamically change the font-size
and constrain so that they don't explode (or shrink).
Let's see an example. Let's say you want your header to be 2rem on mobile and 4rem on larger displays. Here's how you use fluid typography to accomplish that:
h1 {
// font-size: min(max({MIN_SIZE}, 4vw), {MAX_SIZE});
font-size: min(max(2rem, 4vw), 4rem);
}
On most cases, 1rem = 16px, so our minimum font-size is 32px. This means that when the viewport width is less than 800px (0.04 * 800px = 32px), we will always have 32px as our font-size. This is perfect for mobile. When the viewport width is greater than 800px, our font-size will dynamically change along with the viewport, but never exceed 4rem = 64px.
4vw
was just used as an example. You can change it to any value that suits your needs.
To see this in action, try changing the viewport width of the pen below. I changed 4vw
to 8vw
to make the font-size increase faster (font-size acceleration?!):
And that's it! In just one line of code, you can make your font-size responsive!
I hope this simple trick helps you guys out.
Share other awesome tips down below! 🎉
Posted on June 17, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.