Relative units are the foundation to modern responsive websites. They not only help us write CSS that’s easier to maintain, but also allow us to create accessibility that respects the preferences of our users.
The only problem with relative units is that it’s difficult for a beginner to wrap his (or her) mind around them initially.
So, in this lesson, we’ll be focusing on the basics of relative units. You’ll learn:
- What are relative units
- Why do we use relative units
- How relative units helps us write better code
- When you should not use relative units
Let’s begin.
What are Relative Units
Relative units in CSS are units that inherit its font-size from a parent element. Examples of these units include:
- em (
em), - rem(
rem), - percentages (
%), and - viewport units (
vh,vw,vmin,vmax)
em and rem are the two most common units people size their elements with. Recently, there’s also an emerging trend where developers size typography with the vw unit as well.
Although you need to understand the differences between each relative unit to use them effectively, we will concentrate only on one unit — the em unit — for now, and leave the details to the next lesson.
By doing so, you’ll understand what makes relative units so powerful and how it helps you make better websites.
The Em unit
One em in CSS is a value that is equal to a selector’s currently specified font-size. If you have a selector that has a font-size of 20px, one em will be equal to 20px.
h1 { font-size: 20px; /* 1em = 20px */ margin: 1em; /* 20px */}In the above case, browsers will create a margin of 20px around h1 because 1em equals to 20px.
The em unit is also commonly used to declare font-sizes in addition to other properties. Consider the following:
h1 { font-size: 1em;}Here’s a question: What’s the actual size of this h1 selector here?
To find the actual size of this h1 selector, we have to look at the font-size value it inherited from its parent element. It means we have to look at the parent selector for a font-size property.
So, say you have the following HTML:
<html> <body> <h1>This is some text</h1> </body></html>And you the following CSS:
html { font-size: 16px;}h1 { font-size: 1em;}To calculate the value of h1, we have know the font-size of the body selector. In this case, the body selector isn’t declared, which means it inherits the font-size property from it’s parent selector, html. So, font-size of body is 16px.
Since we know font-size of body is 16px, we know 1em = 16px for the h1 selector. So, the value of h1 is also 16px.
Now, what if we changed the h1 font-size to 2em instead of 1em?
h1 { font-size: 2em;}We still have to follow the same steps above to find out that the pixel value that h1 inherits is 16px.
After getting the pixel value, we multiply it by the em multiple. So, the computed value of h1 in the second case is 16px * 2 = 32px.
html { font-size: 16px;}h1 { font-size: 2em;} /* 16px * 2 = 32px */Following along? Great.
Now, the next part is where most people get confused. Stay with me as I explain.
So far, we’ve seen that the em unit can be used to specify values for the font-size property and properties like margin:
h1 { font-size: 2em; margin: 1em;}Can you guess what the computed value of the margin property is now? (Should it be 16px or 32px? Assume font-size of html is 16px)
Now, consider the following code: what’s the computed value of the margin property this time?
p { font-size: 1em; margin: 1em;}Here’s the answer:
- Margin of
h1: 32px - Margin of
p: 16px
Always remember that 1em is equal to the computed font-size value of the current selector.
The only exception to this rule is when em is used for the font-size property. In this case, we have to calculate the font-size value for the selector by multiplying the inherited font-size with the em multiplier.
Since the font-size property of both h1 and p are set in em, (assuming html is set to 16px), their respective values will be:
h1 { font-size: 32px;}p { font-size: 16px;}Once you know the pixel value of the font-size of the selector, it becomes super clear that 1em in h1 is different from 1em in p.
h1 { font-size: 32px; margin-bottom: 1em; /* 1em = 32px */}
p { font-size: 16px; margin-bottom: 1em; /* 1em = 16px */}What throws people off is that 1em can take on different values in different parts of the code. It can be super confusing at first, but it becomes much easier once you understand the underlying mechanics.
That’s all you need to know about the em unit.
Now, let’s see how em (or any other relative unit) can be used to alleviate the problem we encountered at the end of the previous lesson.
Relative units in responsive typography
We left off the last lesson with the following code (written in Sass):
// Setting body font-sizehtml { font-size: 16px;
@media (min-width: 600px) { font-size: 18px; }
@media (min-width: 1000px) { font-size: 20px; }}
// Setting body font-sizeh1 { // 16px * 1.333 * 1.333 * 1.333 * 1.333 font-size: 50.517px;
@media (min-width: 600px) { // 18px * 1.333 * 1.333 * 1.333 * 1.333 font-size: 56.832px; }
@media (min-width: 1000px) { // 20px * 1.333 * 1.333 * 1.333 * 1.333 font-size: 63.147px; }}
// Set font-sizes for h2, h3 and so on...Relative units can help you dramatically reduce the amount of code you write if there’s a defined pattern across different media queries.
In the case of h1 above, you can see there’s a common pattern (the body font-size * 1.333 * 1.333 * 1.333 * 1.333). Since there’s a pattern, we can write one font-size property with relative units instead of writing three media queries.
The code becomes:
// Setting body font-sizehtml { font-size: 16px;
@media (min-width: 600px) { font-size: 18px; }
@media (min-width: 1000px) { font-size: 20px; }}
// Setting h1 font-sizeh1 { // 1.333 * 1.333 * 1.333 * 1.333 = 3.157 font-size: 3.157em;}We can use a similar pattern to write set sizes for h2, h3 and other elements without writing additional media queries as well:
// h1, h2, h3h1 { font-size: 3.157em;}h2 { font-size: 2.369em;}h3 { font-size: 1.777em;}// ...
Notice how the heading size changes when we resized the browser? :)
The best part about using relative units is that you no longer have to tweak every media query when you change the font-size of your body text (even if you add more breakpoints) in future.
Use relative units for other properties
You’ve seen how relative units make responsive typography easy because of these two reasons:
- you can write lesser code
- you no longer have to tweak properties written in pixels whenever you change your body font-size.
Now, recall that you created a design with Vertical Rhythm previously. When we change our font-size, the vertical rhythm changes to follow suit.
html { font-size: 16px; line-height: 1.4; // line height is 22.4px
@media (min-width: 600px) { font-size: 18px; // line height is 25.2px }}This means can no longer write our margins, paddings and other properties that uses Vertical Rhythm in pixels. We have to change everything into relative units.
Remember you learned that 1em can take different values in different parts of the code? This is where that confusion comes in. Let’s walk through an example and you’ll see why.
Let’s say we have a h1 with a font-size of 3.157em. This h1 selector also has a margin-bottom that should be equal to one baseline. We also have a p selector with a font-size of 1em. This p selector also has a margin-bottom that is equal to one baseline.
Assuming 1em is 16px, the calculated values of h1 and p are 50.512px and 16px respectively
h1 { font-size: 3.157em; // Equals to 50.512px // Has a margin bottom of one baseline}
p { font-size: 1em; // Equals to 16px // Has a margin bottom of one baseline}The margin-bottom value of both selectors should be equal to 22.4px since they’re both one baseline (16px * 1.4 = 22.4px).
Setting the margin-bottom on the p selector is simple. Since the font-size is 1em and line-height is 1.4, we just multiply the two of them to get 1.4em.
p { font-size: 1em; margin-bottom: 1.4em; // 1 baseline}Getting the same margin-bottom value for the h1 selector is more complicated. The font-size is now set at 3.157em instead of 1em.
If you recall, 3.157em is 50.512px given our assumptions above. We now need to get a margin-bottom value of 22.4px from 50.512px because that is how em values are calculated. The correct em value for our h1 selector, is 22.4 ÷ 50.512 = 0.44346em.
This formula can be renamed as: correct em value = target ÷ context, where target is the target value while context is the current font-size we’re looking at.
Of course, we don’t have to revert to pixels to do the calculation. Getting the correct baseline values through em is possible with the same formula: correct em value = 1.4 / 3.157 = 0.44346em
Hence, the code for our h1 selector is:
h1 { font-size: 3.157em; margin: 0.44345898em; // 1.4 ÷ 3.157}Calculating em values can be a pain in the ass. Nobody likes to do it, me neither. There are two values to make our lives easier.
First, we can create functions in Sass to calculate things.
Let’s make one to calculate em for us. It should take in two arguments, the target and the context. This function should be placed at the beginning of your Sass file.
@function em($target, $context) { @return $target / $context * 1em;}With this function, we can now write this instead:
@function em($target, $context) { @return $target / $context * 1em;}
h1 { font-size: 3.157em; margin: em(1.4, 3.157); // results in 0.44346em}Our code became much easier to understand now, isn’t it?
Another way, if you don’t want to create functions, is to use another relative unit called rem. We’ll talk about rem and how to use it in the following chapter.
Relative units for calculating measure
Remember from the first module that you need to keep your measure between 45 to 75 characters? Calculating this measure would be hell if you decided to use pixels. You have to update the measure whenever the font-size changes.
With relative units, you remove this hassle as well. One em is approximately equal to 2 characters. So:
- a measure of 45 characters = 22.5em
- a measure of 75 characters = 37.5em
You can set your width to any number between these two values and you’ll be alright.
.container { width: 30em; /* Anywhere between 22.5 and 37.5em */}(Of course, a step up is to challenge yourself to set a measure that’s derived from Vertical Rhythm :))
Relative units in the HTML selector
It’s a best practice to use relative units for font-sizes that written in the html selector due to accessibility issues.
I’ll cover this part in the next lesson because we usually use percentages instead of em in the html selector. Make sure you read this up because it’s important.
Relative units in Media Queries
You should also use relative units in media queries for accessibility purposes. It’s the same issue as to why we use relative units in the html selector. You’ll find out more about this issue in the next lesson.
There’s only one thing to note, however: you should only use the em unit for media queries because it’s the only unit that performs consistently across major browsers out there. (I did some tests and explain why in this blog post if you’re interested in reading more).
So, from this point onwards, we’ll be writing all media queries in the em unit. You can safely assume that 1em is equal to 16px whenever you use em for media queries. I’ll explain why in the next lesson.
Before we end this chapter, let’s take a look at see when you should not use relative units.
Exceptions to Relative Units
The only case where you should not use relative units is when you need small precise values like 1px (usually for border or box shadows):
.border { border: 1px solid black;}
.box-shadow { box-shadow: 0 0 3px 3px black;}The reason we refrain from using relative units for small pixel values is because it’s highly likely that you get subpixel values when you use relative units. If you somehow created a border of 0.8px, some browsers will eliminate this border entirely, leaving you with a weird design.
Hence, for small pixel values, stick to pixels.
Wrapping Up
In this lesson, you learned what relative units are, and how you can use them to make typography and responsive websites much easier and maintainable.
You also learned how to use the em unit for typography, for other properties and for media queries.
In addition, you learned how to write a Sass function that helps you eliminate the need to manually calculate em values every time.
Finally, you learned that you should not use relative units when you need small precise values (like 1px) in your design.
Exercise
Here’s what you need to do for this exercise:
- Convert all your pixel values into
emvalues. This includesfont-size, other properties, and media query values. - Get comfortable with
emvalues. Create a few nested elements try to make sure you always get the correctemvalues.
Next up
The em unit is just one of many relative units CSS has to offer. In the next lesson, we’ll examine a few more and find out when you should use which unit.
Welcome! Unfortunately, you don’t have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson