DEV Community

Impeccify
Impeccify

Posted on

CSS Units Explained: PX vs REM vs EM vs VW (With Free Converter Tools)

If you've ever gotten confused about when to use px vs rem vs em in CSS, you're not alone. This is one of the most common sources of confusion for developers at every level.

The Quick Answer

  • px → Fixed size, doesn't scale. Use for borders, box shadows.
  • rem → Relative to root font size (16px default). Use for font sizes, spacing.
  • em → Relative to parent font size. Use with caution, compounds with nesting.
  • vw/vh → Relative to viewport size. Use for full-screen layouts, fluid typography.

Deep Dive: When Each Unit Makes Sense

PX - Use for Fixed Elements

border: 1px solid #ccc;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
Enter fullscreen mode Exit fullscreen mode

Never use px for font sizes - it overrides user browser font size preferences, breaking accessibility.

REM - Your Default for Most Things

REM is relative to the <html> element font size. Browser default is 16px.

/* 16px → 1rem */
/* 24px → 1.5rem */
/* 14px → 0.875rem */

font-size: 1rem;    /* 16px */
padding: 1.5rem;    /* 24px */
margin-bottom: 2rem; /* 32px */
Enter fullscreen mode Exit fullscreen mode

Pro tip: Set html { font-size: 62.5%; } to make 1rem = 10px so your math is dead simple: 24px = 2.4rem.

EM - For Component-Level Scaling

Use EM when you want the unit to scale with the element's own font size:

/* Button padding scales with button text size */
.btn {
  font-size: 1rem;
  padding: 0.75em 1.5em; /* scales with font-size */
}

.btn-large {
  font-size: 1.25rem;
  /* padding automatically scales up too */
}
Enter fullscreen mode Exit fullscreen mode

VW/VH - For Fluid, Full-Screen Designs

/* Full-screen hero */
.hero {
  width: 100vw;
  height: 100vh;
}

/* Fluid font that scales with screen width */
h1 {
  font-size: clamp(1.5rem, 4vw, 3rem);
}
Enter fullscreen mode Exit fullscreen mode

Quick Conversion Table

PX REM EM (16px)
12px 0.75rem 0.75em
14px 0.875rem 0.875em
16px 1rem 1em
18px 1.125rem 1.125em
20px 1.25rem 1.25em
24px 1.5rem 1.5em
32px 2rem 2em
48px 3rem 3em

For any other conversions, use this free PX to REM converter.

There's also a REM to PX converter, PX to EM converter, and PX to VW converter if you need them.

The Rule I Always Follow

  1. Font sizes → rem
  2. Padding/margin → rem (or em if should scale with element)
  3. Borders, outlines → px
  4. Layout widths → % or fr (in Grid)
  5. Full-screen sections → vw/vh

That's it. You don't need to memorize all the edge cases - just follow this rule 90% of the time.

Top comments (0)