Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS CSS Basics (2014) Understanding Values and Units Em and Rem Units

Arpana Roy
Arpana Roy
2,028 Points

em and rem

difference between and em and rem and em in detail

style.css
/* Complete the challenge by writing CSS below */

header {
  font-size: 1.8em;
}

.title {
  font-size: 1.625rem;
}

h1 {
  font-size: 5.625rem;
}

h2 {
font-size: 3.31rem;
}
index.html
<!DOCTYPE html>
<html>
  <head>
    <title>Lake Tahoe</title>
    <link rel="stylesheet" href="page.css">
    <link rel="stylesheet" href="style.css">
  </head>
  <body>
    <header>
      <span class="title">Journey through the Sierra Nevada Mountains</span>
      <h1>Lake Tahoe, California</h1>
    </header>
    <div class="main-content">
      <p>
        Lake Tahoe is one of the most breathtaking attractions located in California. It's home to a number of ski resorts, summer outdoor recreation, and tourist attractions. Snow and skiing are a significant part of the area's reputation.
      </p>
      <a href="#">Find out more</a>
      <h2>Check out all the Wildlife</h2>
      <p>
        As spawning season approaches, the fish acquire a humpback and protuberant jaw. After spawning, they die and their carcasses provide a feast for gatherings of mink, bears, and Bald eagles.
      </p>
      <a href="#">See the Wildlife</a>
      <h3>From Tents to Resorts</h3>
      <p>
        Lake Tahoe is full of wonderful places to stay. You have the ability to sleep in the outdoors in a tent, or relax like a king at a five star resort. Here are our top three resorts:
      </p>
    </div>
    <footer>
      <p>All rights reserved to the state of <a href="#">California</a>.</p>
      <a href="#top">Back to top &raquo;</a>
    </footer>
  </body>
</html>

2 Answers

Steven Parker
Steven Parker
231,269 Points

The only difference is the reference. The "em" unit is relative to the font size of the parent element, but "rem" is relative to the font size of root element (the document itself).

So if your element was a span inside of a paragraph, and the document font size was 16px, but the paragraph had been set to font size of 24px, the 1em would be the same as 24px, but 1rem would be 16px.

Robert Schaap
Robert Schaap
19,836 Points

It's useful to know that if you do something like...

.class {
  font-size: 3em;
  padding: 1em;
}

... the padding on the class will be relative to the font-size on the class. In this case that makes it easy to set padding or any other property to scale directly with the size of the font. If you later decide the font should be 2em, the other properties will scale proportionally.

That can be very helpful (though I'm a rem-nut), but in cases where you've got multiple layout elements that need to align nicely, it can also be a bit tricky. Combining the two is useful there, i.e something like

.class {
  font-size: 3em;
  padding: 1em 1rem;
}

Now your top and bottom padding is 1em and scales with the font size you've set (or inherited as Steven pointed out), and the left and right padding scale only with the root font size.