Skip to content

CSS

The Ultimate Guide to CSS Units: Benefits of Using REM and EM

5 min read

The Ultimate Guide to CSS Units: Benefits of Using REM and EM

In the early days of web development, pixel (px) was the undisputed king of CSS sizing. Developers meticulously crafted layouts, ensuring every button, heading, and container was exactly a specific number of pixels wide and tall. This worked perfectly fine when everyone browsed the internet on desktop monitors with similar resolutions.

However, the modern web is a vastly different landscape. Users access websites on massive 4K monitors, tiny smartphones, foldable devices, and smartwatches. Furthermore, web accessibility has rightfully taken center stage, meaning websites must dynamically adapt to users who manually increase their browser's default font size for readability.

This is where relative CSS units—specifically rem (Root EM) and em—become absolutely essential. Relying solely on static pixels creates rigid, inaccessible designs. By mastering rem and em, you unlock the ability to create fluid, scalable, and highly accessible web interfaces.

In this comprehensive guide, we will explore exactly what these units are, how they differ, the profound benefits of using them, and the best practices for implementing them in modern frontend development.


The Problem with Pixels (px)

Before diving into relative units, it's crucial to understand why we need to move away from pixels.

A pixel (px) is an absolute unit of measurement in CSS. When you declare font-size: 16px;, you are telling the browser to render that text at exactly 16 pixels, regardless of the user's screen size or personal preferences.

The Accessibility Issue: The primary problem with pixels is accessibility. Visually impaired users often change their browser's default font size (which is typically 16px) to something larger, like 20px or 24px, to read comfortably. If you hardcode your website's typography in px, you override the user's browser settings. Your website will stubbornly remain at 16px, forcing the user to zoom in manually, which often breaks the page layout.

By contrast, relative units respect the user's preferences, scaling up or down harmoniously.


Understanding REM (Root EM)

The term rem stands for "root em". It is a relative unit of measurement that scales based on the font size of the root element of the document, which is the <html> tag.

By default, in almost all modern web browsers, the root font size is exactly 16px.

Therefore:

  • 1rem = 16px (by default)
  • 2rem = 32px
  • 0.5rem = 8px
  • 1.5rem = 24px

Why is REM so powerful?

The brilliance of rem lies in its predictability and its respect for accessibility.

If a user goes into their browser settings and changes their default font size from 16px to 24px, the root font size changes. Because rem is tied directly to the root, every single element on your website that uses rem will proportionally scale up.

Your 2rem heading, which was previously 32px, will automatically calculate to 48px (2 * 24px). Your website remains perfectly proportioned and, more importantly, perfectly readable for the user.

Best Use Cases for REM:

  • Typography: Always use rem for font-size, line-height, and letter-spacing. This ensures your text is fully accessible.
  • Global Spacing: Use rem for macro-layout properties like grid gaps, main container padding, and section margins. This ensures the whitespace on your site breathes proportionally to the text size.

Understanding EM

While rem is relative to the root <html> element, em is relative to the font size of its direct parent element.

If a parent container has a font size of 20px, and you set a child element's font size to 2em, the child will render at 40px.

The Compounding Effect (The EM Trap)

The most important characteristic of em to understand is that it compounds. Because it looks at its immediate parent, nesting elements with em sizing can lead to exponential, unexpected growth or shrinkage.

Consider this HTML structure:

<div class="parent">
  <div class="child">
    <div class="grandchild">Hello</div>
  </div>
</div>

With this CSS:

.parent { font-size: 1.5em; } /* 1.5 * 16px = 24px */
.child { font-size: 1.5em; } /* 1.5 * 24px = 36px */
.grandchild { font-size: 1.5em; } /* 1.5 * 36px = 54px */

As you can see, the text size balloons out of control rapidly. This compounding effect makes em notoriously difficult to manage for global typography, which is why developers heavily favor rem for font sizes.

The True Power of EM: Modular Components

If em is so tricky, why use it at all? The answer lies in modular, isolated component design.

Because em is relative to the parent's font size, it is incredibly powerful for sizing elements around text, such as padding, margins, and border radii on buttons, badges, and tooltips.

Imagine you are designing a button:

.button {
  font-size: 1rem;
  padding: 0.5em 1em;
  border-radius: 0.25em;
}

.button-large {
  font-size: 1.5rem;
}

By using em for the padding and border-radius, those values are intrinsically linked to the button's font-size. If you want to create a .button-large, you only need to change the font-size. The padding and border-radius will automatically calculate and scale up perfectly to match the larger text. You don't need to manually redefine the padding for every button size variant.

Best Use Cases for EM:

  • Component Padding & Margins: Buttons, alert boxes, and badges where the internal spacing should scale proportionally to the text inside them.
  • Media Queries (Sometimes): While em is used for media queries, standard practice often leans towards px or rem depending on the framework, though em media queries offer high consistency across different browser zoom levels.

REM vs. EM: A Quick Cheat Sheet

To summarize when to use which unit, keep this rule of thumb in mind:

  1. Use REM for Global Sizing: Anything that should remain consistent across the entire page layout should use rem. This includes font sizes, generic spacing (margins between sections), and layout grid definitions. rem protects you from the compounding nightmare.
  2. Use EM for Local Sizing: Anything that should scale proportionally to the text immediately around it should use em. This includes padding inside a button, spacing between an icon and text, or the size of a custom SVG bullet point.
  3. Use PX Sparingly: Pixels should be reserved for things that must never, ever scale. Examples include a 1px solid border, precise box-shadow offsets, or defining the max-width of a highly specific image.

Implementing the "62.5% Trick" (And Why You Should Be Careful)

Historically, calculating rem values was a pain for developers. If you wanted a font size of 14px, you had to divide 14 by 16, resulting in 0.875rem. To make the math easier, a popular hack emerged:

html {
  font-size: 62.5%;
}

Because the browser default is 16px, 62.5% of 16 is exactly 10. By setting the root font size to 10px, the math becomes incredibly simple:

  • 1.4rem = 14px
  • 2.4rem = 24px
  • 3.2rem = 32px

Should you use it? While it makes math easier, modern CSS development relies heavily on preprocessors (SASS/LESS), CSS variables, or utility-first frameworks like Tailwind CSS, which handle the math for you. Furthermore, overriding the root font size with a percentage can sometimes cause unexpected behaviors with third-party plugins that assume the root is 16px.

If you are working on a pure vanilla CSS project, the 62.5% trick is still viable, but for modern stacks, it is generally safer to stick to the default 100% (16px) root size and let your tooling handle the fractional rem values.

Conclusion

Transitioning from pixels to relative units like rem and em is a rite of passage for every frontend developer. While it requires a slight paradigm shift in how you think about dimensions, the payoff is immense.

By strategically combining rem for predictable global typography and layout, and em for modular, self-contained UI components, you will write cleaner, more maintainable CSS. Most importantly, you will create inclusive, accessible web experiences that look and function flawlessly for every single user, regardless of their device or browser settings.

Buy me a coffee