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.
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.
By using fluid typography, we can make the typography scale more logically between all sorts of different devices.
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.
clamp()
Function Work For Typography?
In short, the clamp function looks like this:
clamp([min-bound], [value-preferred], [max-bound]);
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.
If you want a more in-depth explanation of the math, I suggest you read this post from Adrian Bece “Modern Fluid Typography Using CSS Clamp ”.
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:
clamp(1.44rem, 3.44vw + 0.75rem, 2.81rem)
But with a little help from the sass
function, we can make these font sizes much more readable.
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:
clamp(1rem, [?], 2rem)
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:
@use "sass:math";
@function fluid($min-size, $max-size, $min-breakpoint, $max-breakpoint, $unit: vw) {
}
Now, let’s add the calculation for the slope inside of our function with some sass:math
:
@function fluid($min-size, $max-size, $min-breakpoint, $max-breakpoint, $unit: vw) {
$slope: math.div($max-size - $min-size, $max-breakpoint - $min-breakpoint);
}
To get a value we can work with, we’ll need to multiply our slope by 100
:
$slope-to-unit: $slope * 100;
All that is left is for us to find our intercept to build the equation. We can do this with the following function:
$intercept: $min-size - $slope * $min-breakpoint;
And finally, return our function:
@return clamp(#{$min-size}, #{$slope-to-unit}#{$unit} + #{$intercept}, #{$max-size});
If we call the created sass
function in our scss, we should now get fluid typography:
h1 {
font-size: #{fluid(1rem, 2rem, 25rem, 62.5rem)}
}
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:
h1 {
font-size: #{fluid(1rem, 2rem, 25rem, 62.5rem, vh)}
}
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.
@function px-to-rem($px) {
$rems: math.div($px, 16px) * 1rem;
@return $rems;
}
Now we can update our fluid function to output rem
values even though it gets pixels as input. This is the updated version:
@function fluid($min-size, $max-size, $min-breakpoint, $max-breakpoint, $unit: vw) {
$slope: math.div($max-size - $min-size, $max-breakpoint - $min-breakpoint);
$slope-to-unit: $slope * 100;
$intercept-rem: px-to-rem($min-size - $slope * $min-breakpoint);
$min-size-rem: px-to-rem($min-size);
$max-size-rem: px-to-rem($max-size);
@return clamp(#{$min-size-rem}, #{$slope-to-unit}#{$unit} + #{$intercept-rem}, #{$max-size-rem});
}
Now we can use the following input:
font-size: #{fluid(16px, 32px, 320px, 960px)}
This will result in the following:
font-size: clamp(1rem, 2.5vw + 0.5rem, 2rem);
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:
font-size: #{fluid(16px, 31px, 320px, 960px)}
Output:
font-size: clamp(1rem, 2.34375vw + 0.53125rem, 1.9375rem);
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 DefaultsLet’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:
@function round($number, $decimals: 0) {
$n: 1;
@if $decimals > 0 {
@for $i from 1 through $decimals {
$n: $n * 10;
}
}
@return math.div(math.round($number * $n), $n);
}
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:
@function fluid($min-size, $max-size, $min-breakpoint, $max-breakpoint, $unit: vw) {
$slope: math.div($max-size - $min-size, $max-breakpoint - $min-breakpoint);
$slope-to-unit: round($slope * 100, 2);
$intercept-rem: round(px-to-rem($min-size - $slope * $min-breakpoint), 2);
$min-size-rem: round(px-to-rem($min-size), 2);
$max-size-rem: round(px-to-rem($max-size), 2);
@return clamp(#{$min-size-rem}, #{$slope-to-unit}#{$unit} + #{$intercept-rem}, #{$max-size-rem});
}
Now the same example as before will give us a much cleaner result.
Input:
font-size: #{fluid(16px, 31px, 320px, 960px)};
Output:
font-size: clamp(1rem, 2.34vw + 0.53rem, 1.94rem);
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:
$default-min-bp: 320px;
$default-max-bp: 960px;
@function fluid($min-size, $max-size, $min-breakpoint: $default-min-bp, $max-breakpoint: $default-max-bp, $unit: vw) {
// ...
}
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:
font-size: #{fluid(16px, 31px)};
Will still result in:
font-size: clamp(1rem, 2.34vw + 0.53rem, 1.94rem);
Here is the full function:
@use 'sass:math';
$default-min-bp: 320px;
$default-max-bp: 960px;
@function round($number, $decimals: 0) {
$n: 1;
@if $decimals > 0 {
@for $i from 1 through $decimals {
$n: $n * 10;
}
}
@return math.div(math.round($number * $n), $n);
}
@function px-to-rem($px) {
$rems: math.div($px, 16px) * 1rem;
@return $rems;
}
@function fluid($min-size, $max-size, $min-breakpoint: $default-min-bp, $max-breakpoint: $default-max-bp, $unit: vw) {
$slope: math.div($max-size - $min-size, $max-breakpoint - $min-breakpoint);
$slope-to-unit: round($slope * 100, 2);
$intercept-rem: round(px-to-rem($min-size - $slope * $min-breakpoint), 2);
$min-size-rem: round(px-to-rem($min-size), 2);
$max-size-rem: round(px-to-rem($max-size), 2);
@return clamp(#{$min-size-rem}, #{$slope-to-unit}#{$unit} + #{$intercept-rem}, #{$max-size-rem});
}
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.
Gain $200 in a week
from Articles on Smashing Magazine — For Web Designers And Developers https://ift.tt/rLltAvc