CSS Length Units

Relative to root font size

The rem length unit is a relative length unit where the base length is calculated from the width of the character M of the document's root element: the <html> element. We remember from the em section that all major web browsers set the font size of the <html> and <body> tag to 16px.

rem units do not have the same nesting complexity as em units since 1.0rem is the same absolute length regardless of context and depth.

When to use rem

Everywhere else where scalability within surrounding context is not desirable. There are of course still situations where we want percentage lengths and absolute pixel lengths, but we should try to use rem as much as possible.

Code example

<style>
#blue-box {
width: 250px;
height: 250px;
background-color: blue,
color: white;
font-size: 1.0rem;
}
#red-box {
background-color: red,
color: black;
font-size: 1.5rem;
}
</style>
<div id="blue-box">
I am a blue box with a font size of 1.0rem == 16px
<div id="red-box">
I am a red box with a font size of
1.5 rem == (1.5 x 16px = 24px)
</div>
</div>

which produces

I am a blue box with a font size of 1.0rem == 16px
I am a red box with a font size of 1.5 rem == (1.5 x 16px = 24px)

Test yourself

Description

Change the font-size of the <nav> element to

18px

All of the rules are defined for you. You only need to modify the correct declarations.

Notice how setting the <nav>'s font size does nothing to the child elements. Compare this to the result we got in the previous section where we used em units.

Tests

  1. 1 Your nav element should have the font size 18px.
Editor
Preview