A few days ago,
I wrote about some typography tips
. We discussed the importance of using the REM unit and how to make our fonts responsive using Media Queries. With that, you can already create impeccable web projects in terms of font size. But I promised to talk about making fonts responsive without using Media Queries, and a promise is a debt. So, here we go!
When we adapt font sizes using media queries, our fonts remain the same size until they reach the breakpoint, and only then they change.
Open the CodePen below and adjust the screen width to see the sudden change in font size.
Using the fluid typography technique, our font will always adjust to the screen size. For every pixel the screen increases, there will be an increment in font size.
"Caio, what kind of witchcraft is this? How is that possible?"
It's not witchcraft, but a powerful CSS function: clamp().
Getting to know 'clamp()'
With clamp(), we can define a minimum value, an ideal value, and a maximum value.
Let's say we want our font to be 16px on a screen width of 320px and 32px on a screen width of 1920px. Our clamp() would look something like this:
clamp(1rem,valorideal,2rem);
You noticed that we're using REM, right?
No PX for fonts, remember?
What about the ideal value? Well, since we want our font to adapt to the screen size, we'll use one of the viewport units, VW. 100vw is the size of the screen, and 1vw is 1% of the screen width. On a 320px screen, 100vw is 320px, and 1vw equals 3.2px.
With this, our font still has those minimum and maximum limits of 16px and 32px (based on the browser's default font), but with 5vw as the ideal value, it will always try to be 5vw in size.
Let's see some examples:
On a 300px screen: 5vw would be 15px. 15px is less than 16px - our minimum font size. In this case, the font would be 16px.
On a 320px screen: 5vw would be 16px. The font used would be 16px.
On a 500px screen: 5vw would be 25px. 25px is greater than 16px, and it's less than the maximum limit of 32px. So, the font used would be 25px.
On a 1000px screen: 5vw would be 50px. Since this value is greater than our limit, the font used would be 32px.
See this applied in the example below:
We have two problems here:
1) Our font is growing too quickly in this example. It reached our limit at a screen width of 640px, but we wanted it to vary fluidly up to 1920px. It becomes static for 1280px!
2) Using a font based solely on viewport units poses an accessibility problem. Try going back to the previous CodePen and zoom in on the screen. Users cannot zoom in on the font, which remains frozen since it's based on the screen size. You'll notice that the text in the center of the screen doesn't change in size, while the screen and font size counter in the upper-left corner increases.
A technique that helps with these two problems is to define the ideal value, the one in the middle of the clamp(), not just in VW, but as a sum of VW and REM.
"Caio, you're a genius! How did you come up with that value?"
I hate to disappoint you, dear reader, but I didn't do this calculation in my head! There is a formula to calculate this ideal value, and all the explanations for that will be in one of the articles I'll leave in the references. In this example and in my daily work, I use a tool that calculates it for me.
Here, we can define the minimum and maximum screen widths and the minimum and maximum font sizes — let's just ignore the Type Scale option for now.
The tool provides the clamp() values for you. Then, just add them to your code, and you're good to go.
I know, I know. The example with only one font size was to make things simple. But there's no catch 22 here. Let's apply this logic to our first example, the one with fonts using Media Queries.
There, we had the following font model:
Level 1 -> 16px to 18px;
Level 2 -> 20px to 24px;
Level 3 -> 25px to 32px;
Level 4 -> 31px to 42px;
Now we just need to use our calculator for each of these ranges!
As developers, we often don't take part in the font size selection process. Designers usually lead that decision. "And how do they do that?", you might be wondering.
That's a science of its own. There are different systems: the 4-point grid, the 8-point grid, Google's Material Design, and many others. None of these systems can do without the trained eyes of design professionals: systems are not magic and often require adaptations.
One of these systems is modular scale typography. In this system, we start with a font size and increase or decrease it based on a multiplication factor. For example, let's say our smallest font is 10px, and the multiplication factor is 2. Our font system could have the following values: 10px, 20px, 40px, 80px, and so on. This example is exaggerated for didactic purposes, of course.
This methodology adds objectivity to font size selection and helps make our typography hierarchy more consistent.
The Type Scale field in our calculator helps generate fonts using this type of methodology. Let's say I have a project with 4 font levels (4 different sizes), and the smallest font should be 14px on mobile and 20px on monitors.
Here, I'm applying a growth rate of 1.25 on 320px screens while applying a growth rate of 1.414 on 1900px screens. The result is as follows:
Note that font sizes grow much faster on the 1900px screen because we used a higher rate. I invite you to read the excellent articles on modular typography scales that will be in the references of this article to better understand when to choose which rate for your scale. I also encourage you to generate and test different scales and compare the results. What happens when the scale rate is higher on larger screens than on smaller screens? What happens when the rate is the same? And when it's smaller?
We've learned how to apply the fluid typography technique using CSS. Fluid typography is an elegant solution that brings visual integrity to your project on various screens.
Always remember that accessibility is a priority. To ensure your website is accessible, always test it with a 200% zoom.
Senior Full-stack .Net Developer who has Created and optimized scalable web applications for over 8 years ,leveraged troubleshooting and
debugging expertise to contribute to the successful development
I enjoyed your blogs a lot, very informative thank you!
I have a question however and I hope you can shed some light into it. If we use rem and media queries for spacing wouldn't that conflict with clamp if we also use rem in it and even break the formula?
If rem is defined as 62.5% initially then through media queries decreased to adapt for mobile devices for instance, wouldn't that affect the clamp formula seeing that it has rem in it?
Built on Forem — the open source software that powers DEV and other inclusive communities.