Fluid typography is getting a lot more popular, especially since the clamp()
math function
is available in every evergreen browser. But if we’re honest, it’s
still a lot of mathematics to achieve this. You can use tools such as utopia.fyi
,
which are fantastic. But in large projects, it can get messy pretty
fast. I’m a big fan of readable and maintainable code and always want to
see what my code is doing at a glance. I’m sure there are many more of
you like that, so instead of adding a full clamp()
function inside of our code, maybe we can make this a bit more readable with Sass.
Why Should We Use Fluid Typography?
Usually, when designing for different screen sizes, we use media queries to determine the font size of our typographic elements. Although this usually gives enough control for the more conventional devices, it doesn’t cover all of the screen sizes.
“
This is now possible in all evergreen browsers because of the clamp()
function in CSS. It is perfect for the job and reduces our media query
writing, thus saving us a bit of file size along the way.
How Exactly Does This clamp()
Function Work For Typography?
In short, the clamp function looks like this:
This takes into account three numbers: a minimum bound, preferred value, and a maximum bound. By using rem
values, we can increase the accessibility a bit, but it’s still not 100% foolproof, especially for external browser tools.
However, there is a bit of a problem. When you read those clamp
functions inside your CSS, it’s still hard to see exactly what is
happening. Just imagine a file full of font sizes that look like this:
But with a little help from the sass
function, we can make these font sizes much more readable.
What Do We Want To Achieve With This Simple Sass Function?
In short, we want to do something like this: We have a minimum font size, from the moment our breakpoint is larger than 400px
, we want it to scale it to our biggest font size until the maximum breakpoint is reached.
The minimum and maximum font sizes are covered quite easily. If we want a minimum font size of 16px
(or 1rem
) and a maximum font size of 32px
(or 2rem
), we already have the two parts of our clamp function:
Creating A Basic Automated Fluid Function
This is where things get tricky, and I suggest you follow the article by Adrian Bece, who gives a great in-depth explanation of the math behind this.
In short, the equation is the following:
(maximum font-size - minimum font-size) / (maximum breakpoint - minimum breakpoint)
Let’s get ready to do some mathematics for this to happen in Sass, so let’s create our fluid-typography.scss
function file and start by adding sass:math
and the function with the values we’ll need:
Now, let’s add the calculation for the slope inside of our function with some sass:math
:
To get a value we can work with, we’ll need to multiply our slope by 100
:
All that is left is for us to find our intercept to build the equation. We can do this with the following function:
And finally, return our function:
If we call the created sass
function in our scss, we should now get fluid typography:
A Note About Units #
In
most cases, we will be using a viewport width when it comes to fluid
typography, so this makes a good default. However, there are some cases,
especially when using the clamp()
function for vertical
spacing, where you want to use a viewport height instead of width. When
this is desired, we can change the outputted unit and use a minimum and
maximum breakpoint for the height:
Updating The Function To Make The Calculations Feel More Natural
We
got what we need, but let’s be honest, most of the time, we are
implementing a design, and it doesn’t feel natural to pass our viewports
as rems
. So, let’s update this function to use pixels as a
viewport measurement. While we’re at it, let’s update the font sizes so
we can use pixel values for everything. We will still convert them to rem
units since those are better for accessibility.
First, we’ll need an extra function to calculate our rem
values based on a pixel input.
Note: This won’t work if you change your base rem
value.
Now we can update our fluid function to output rem
values even though it gets pixels as input. This is the updated version:
Now we can use the following input:
This will result in the following:
At
first glance, this seems perfect, but mostly that’s because I’ve been
using very simple values. For example, when clamping to a maximum value
of 31px
instead of 32px
, our rem
values won’t be so rounded, and our output will get a bit messy.
Input:
Output:
If you’re like me and find this a bit messy as well, we could round our values a little bit to increase readability and save some bytes in our final CSS file. Also, it might get a bit tedious if we always have to add the viewport, so why not add some defaults in our function?
Rounding Our Values And Adding Some Defaults
Let’s start by adding a rounding function to our Sass file. This will take any input and round it to a specific amount of decimals:
Now we can update our output values with rounded numbers. Update the function accordingly. I would suggest setting at least two decimals for the output values for the most consistent results:
Now the same example as before will give us a much cleaner result.
Input:
Output:
Adding A Default Breakpoint #
If you don’t feel like repeating yourself, you can always set a default breakpoint to your function. Try updating the function like this:
Now, we don’t need to repeat these viewports all the time. We can still add a custom breakpoint but a simple input such as:
Will still result in:
Here is the full function:
A Final Note: Be A Happy Clamper For All users Out There
If you followed this little tutorial and were amazed by it, you might want to add this clamp()
method for everything, but there is an important side note when it comes to accessibility.
Note: When you use vw
units or limit how large text can get with clamp()
, there is a chance a user may be unable to scale the text to 200% of its original size.
If that happens, it is WCAG failure. As Adrian Bece mentioned, it’s not 100% foolproof. Adrian Roselli has written some examples on this, which you might find interesting.
We can use this method today because of the great browser support. By being smart on the usage, I’m sure it can be a beautiful addition to your upcoming project or as an upgrade to a previous one.
No comments:
Post a Comment