Web Design Basics: Rem vs. Em vs. Px — Sizing Elements In CSS

Elements In CSSIn the days of yore, everything was sized in pixels because that was the only choice we had. Things have moved on. We can now choose between pixels, ems, and the recently introduced rems (and a bunch of others that aren’t often used in web design). The differences between them are considerable, and each has its benefits and shortcomings. In this article, we’re going to take a look at what each unit does and when it’s appropriate to use it.

(NB I’m sizing some elements in px that I would normally use percentages to size for the sake of clarity in my examples.)

What do pixels, ems, and rems have in common?

Each is a unit of length used to define the size of elements on a webpage. You can use them across the board on divs, margins, padding, and so on, but in this article we’re going to concentrate on the sizing of type.

The pixel

You’re looking at a monitor which is comprised of lots of tiny dots that are used to make up an image. These are pixels. Or rather, they’re what people usually mean when they talk about pixels. Pixels (px) in CSS do not mean the same thing as pixels on your monitor —in fact, they’re a non-linear angular measurement. It doesn’t really matter what that means and it seems people like to argue about what exactly it does mean, but you do need to understand that pixels are an absolute unit of measurement. Absolute means that they are the same size regardless of the size of anything else (that will make more sense in second). In practice, they aren’t the same length everywhere because different devices treat them differently, but on each device a pixel is always the same. 16px on your laptop monitor is not the same as 16px on your iPad. Pixels are absolute but not consistent.

The em

Ems are a relative measure of length. The size of an em is relative to the font-size of its parent element. If you have a <div> with the font size is set to 16px, and a <p> element inside that div with a font-size set to 2em, the font-size of text in the <p> will be 32px; set it to 0.5px, and the font-size will be 8px.

This is useful because it lets you change the absolute size of elements like the font-size without changing their size relative to each other. Lets say you set the font-size of <div> to 22px. Inside that <div> you have a <p> with the width set to 200em, the margins set to 3em, and the font-size set to 0.8em. If you wanted to halve the size of that <p>, you could simply change the font-size on the surrounding <div> to 11px and the width, margins, and font-size on the <p> would scale accordingly.

As you might imagine, this is great for responsive design, where you can set breakpoints for device screen sizes with media queries, change the absolute px measure for elements at different break points, and have all the em-measured elements resize for you — it makes responsive CSS much easier to write and maintain.

The rem

Ems have a problem. Because everything is sized relative to its parent element, the meaning of an em changes as elements are nested. If we take the same scenario we discussed above: a <div> with a font-size of 22px containing a <p> with an font-size of 0.5 em but add a <blockquote> inside the <p> with a font size of 0.5em too, the result might not be what we want. The font-size in the <p> will be equal to 11px, but the font-size inside the <blockquote> would be half of that again, because em is relative the immediate ancestor (the paragraph), not the div.

Rems, root ems, are always relative to the font-size of the <html> element. It doesn’t matter how deeply nested an element is, its rem lengths will always be a proportion of the font-size of <html>.

Rems are awesome in theory, and mostly awesome in practice. There are some cases where simple proportional scaling doesn’t work out quite right — some text gets too small or too large relative to the surrounding text. Chris Coyer has an excellent article over on CSS tricks that details how he prefers to use a mix of all three.

Px, rems, and ems are the most commonly used measures of length in CSS, but there are a lot more available to use if you want to (you almost certainly shouldn’t want to), which you can check out in this article.

Matthew Davis is a technical writer and Linux geek for Future Hosting.

Dedicated Server Special

Take advantage of our Double RAM offer on the E3-1230v2 4 x 3.30GHz+HT server! Only $134.95 per month. Managed and Unmanaged options available at checkout.

GET STARTED
  • asaf jaffi

    Should “set it to 0.5px” be “set it to 0.5em”?

    • Ahmad Shiddiq

      if you set the parent 5px, then inside the parent will set 1em = 5px.
      if you set 0.5px = 1em. 0.5em = 0.255px;

  • SatB

    Thank you Matthew Davis for your quick and crisp article. Very helful

  • NRubasingha

    Thanks for the article Matthew!

    I found this calculator recently. It helped to overcome most of the conversion issues.
    http://www.pxemrem.com

  • Astra Rai

    Advanced Styling with Responsive Design via Coursera’s Web Design for Everybody (Basics of Web Development and Coding) Specialization offered by University of Michigan. Your article is a suggested reading for Week 1’s material. It was very helpful.

  • Maple !

    This is a very clear explanation, thanks a lot!

  • Ruxandra Vasilescu

    //The em
    Ems are a relative measure of length. The size of an em is relative to the font-size of its parent element. If you have a with the font size is set to 16px, and a element inside that div with a font-size set to 2em, the font-size of text in the will be 32px; set it to 0.5px, and the font-size will be 8px.//
    On the last line, did you mean “set it to 0.5em instead of 0.5px?