CSSApril 2026·10 min read

PX, REM & CSS Units: The Complete Guide

CSS has over a dozen length units, but most developers reach for the same two or three out of habit. This guide explains what each unit actually does, how rem and em scaling work, and when to use which — so your layouts stay consistent across devices and respect user preferences.

📏
Try the PX to REM Converter
Convert between px, rem, em, and more — free, no signup
DG
Derek Giordano
Designer & Developer
In this guide
01Absolute vs Relative Units02Pixels (px)03REM Units04EM Units05Viewport Units (vw, vh, svh, dvh)06Character & X-Height (ch, ex)07Percentages08Converting PX to REM09Fluid Typography with clamp()10Which Unit to Use When
⚡ Key Takeaways
  • Understand every CSS unit: px, rem, em, vw, vh, ch, and more.
  • Covers absolute vs relative units.
  • Covers pixels (px).
  • Covers rem units.
  • Covers em units.

Absolute vs Relative Units

CSS units fall into two categories. Absolute units have a fixed size regardless of context — they don't change when the parent element, root font size, or viewport dimensions change. Relative units are calculated based on something else: the parent's font size, the root font size, or the viewport.

pxAbsolute. 1px = 1/96th of an inch (on standard displays)
remRelative to the root element's font size (usually 16px)
emRelative to the parent element's font size
vw1% of the viewport width
vh1% of the viewport height
%Relative to the parent element's size
chWidth of the '0' character in the current font
exHeight of the 'x' character in the current font

In practice, most web work uses px, rem, and em for sizing, with viewport units for layout-level dimensions. The other units are useful in specific situations like constraining line lengths or creating responsive typography.

Pixels (px)

Pixels are the most intuitive CSS unit. One CSS pixel corresponds to one device-independent pixel — which on a 2x Retina display is actually two physical pixels. This abstraction means that 16px text looks the same physical size on a standard monitor and a high-DPI phone screen.

💡 Tip
Always include -webkit-backdrop-filter alongside backdrop-filter for Safari support. Without the prefix, the effect is invisible to roughly 25% of mobile users.

Pixels are predictable and precise, which makes them a natural choice for things like borders, shadows, and fine visual details where you want exact control. However, they have a significant limitation: they don't respond to user font-size preferences. If a user sets their browser default font size to 20px (for accessibility), text set in px stays fixed while text set in rem scales accordingly.

/* Pixels for fine details */ border: 1px solid rgba(255,255,255,0.1); box-shadow: 0 2px 8px rgba(0,0,0,0.3); /* But avoid px for font sizes */ font-size: 16px; /* Won't respect user preferences */

REM Units

REM stands for "root em." One rem equals the font size of the element, which defaults to 16px in all major browsers. This means 1rem = 16px, 1.5rem = 24px, 0.875rem = 14px, and so on.

⚠ Warning
On iOS Safari, backdrop-filter inside a position: fixed element can cause severe scroll performance issues. Test thoroughly on real iOS devices.

The critical advantage of rem is that it scales with user preferences. When a user changes their browser's default font size (a common accessibility setting), every rem-based value adjusts proportionally. This is why rem is now the standard unit for font sizes, spacing, and component dimensions in modern CSS.

html { font-size: 16px; /* Default — usually you don't need this */ } body { font-size: 1rem; /* 16px */ line-height: 1.6; /* Unitless — multiplied by font-size */ } h1 { font-size: 2.5rem; /* 40px at default, scales with prefs */ margin-bottom: 1rem; /* 16px at default */ } .card { padding: 1.5rem; /* 24px at default */ border-radius: 0.75rem; /* 12px at default */ }

The 62.5% trick

Some developers set html { font-size: 62.5% } to make 1rem equal 10px, simplifying mental math: 1.6rem = 16px, 2rem = 20px. While this works, it requires resetting the body font size and can confuse third-party components that assume the default 16px root. Most teams today prefer sticking with the 16px default and using a converter tool for quick math.

EM Units

An em is relative to the font size of the element itself (for font-size) or its parent (for other properties). This makes em useful for component-internal spacing that should scale with the component's text size.

/* Button with em-based padding scales with its font size */ .btn { font-size: 1rem; padding: 0.5em 1em; /* Padding scales with font-size */ } .btn-large { font-size: 1.25rem; /* Padding automatically grows because it's in em */ }

The compounding behavior of em is both its strength and its pitfall. If a parent sets font-size: 1.2em and a child also sets font-size: 1.2em, the child's actual font size is 1.2 × 1.2 = 1.44 times the grandparent's font size. This compounding doesn't happen with rem because rem always references the root, not the parent.

When to use em vs rem

Use rem for global sizing: font sizes, section spacing, layout dimensions. Use em for component-internal spacing that should scale proportionally when the component's font size changes — like padding inside a button, or margins between an icon and its label.

Viewport Units (vw, vh, svh, dvh)

Viewport units express sizes as a percentage of the browser window. 1vw is 1% of the viewport width, and 1vh is 1% of the viewport height. They're commonly used for full-screen hero sections, responsive font scaling, and fluid layouts.

/* Full-viewport hero section */ .hero { min-height: 100vh; width: 100vw; } /* Responsive heading that scales with screen width */ h1 { font-size: 5vw; /* Gets very large on wide screens! */ }

A well-known problem with vh on mobile is that the browser's address bar changes height as you scroll, causing layout jumps. Modern browsers introduced new viewport units to address this: svh (small viewport height — excludes the address bar), lvh (large viewport height — includes it), and dvh (dynamic viewport height — updates in real time as the bar shows/hides).

/* Modern mobile-friendly full-height section */ .hero { min-height: 100dvh; /* Dynamic viewport height */ } /* Fallback for older browsers */ @supports not (min-height: 100dvh) { .hero { min-height: 100vh; } }

Character & X-Height (ch, ex)

The ch unit equals the width of the "0" (zero) character in the current font. It's most useful for constraining line lengths — typography best practices suggest 45–75 characters per line for comfortable reading:

p { max-width: 65ch; /* Roughly 65 characters per line */ }

The ex unit equals the height of the lowercase "x" in the current font. It's occasionally used for vertical alignment relative to text, but it's far less common than ch. Both units change when the font changes, which makes them font-aware in a way that px and rem aren't.

Percentages

Percentages are relative to the parent element's corresponding dimension. width: 50% is half the parent's width. font-size: 120% is 1.2 times the parent's font size (similar to 1.2em). Percentages are essential for fluid grid layouts and are the basis for most responsive design patterns.

One common gotcha: padding and margin percentages are always calculated relative to the parent's width, even for top/bottom values. This is actually useful for creating aspect-ratio boxes (the "padding-top hack"), though the modern aspect-ratio CSS property is now a simpler solution.

Converting PX to REM

The conversion formula is straightforward: divide the pixel value by the root font size (default 16).

/* Formula: rem = px / 16 */ 10px = 0.625rem 12px = 0.75rem 14px = 0.875rem 16px = 1rem 18px = 1.125rem 20px = 1.25rem 24px = 1.5rem 32px = 2rem 48px = 3rem

If you're working from a design file that specifies everything in pixels (as most Figma designs do), you'll need to convert these values when writing CSS. The PX to REM Converter and CSS Unit Converter can handle this instantly, or you can use CSS custom properties to keep the conversion in one place:

:root { --spacing-sm: 0.5rem; /* 8px */ --spacing-md: 1rem; /* 16px */ --spacing-lg: 1.5rem; /* 24px */ --spacing-xl: 2rem; /* 32px */ }

Fluid Typography with clamp()

The clamp() function sets a value that scales fluidly between a minimum and maximum, based on a preferred value (usually in viewport units). It's the modern solution for responsive typography that adapts smoothly without media query breakpoints:

/* clamp(minimum, preferred, maximum) */ h1 { font-size: clamp(1.5rem, 4vw, 3rem); /* Never smaller than 24px, never larger than 48px, but scales fluidly with viewport width between */ }

The preferred (middle) value is typically a viewport-based calculation that determines how quickly the font scales. A common formula combines rem and vw for a smooth curve:

h1 { font-size: clamp(1.5rem, 1rem + 2vw, 3rem); } h2 { font-size: clamp(1.25rem, 0.8rem + 1.5vw, 2rem); } p { font-size: clamp(1rem, 0.9rem + 0.3vw, 1.125rem); }

The CSS Clamp Calculator can generate these values for you — just enter your minimum font size, maximum font size, and the viewport range you want it to scale across.

Clamp isn't limited to font sizes. It works for any CSS property that accepts a length: padding, margins, widths, and more. Fluid spacing paired with fluid typography creates layouts that feel intentionally designed at every viewport width.

Which Unit to Use When

remFont sizes, spacing, component dimensions — anything that should respect user preferences
emComponent-internal padding/margins that should scale with the component's own font size
pxBorders, shadows, outlines, and fine visual details where exact sizing matters
vw/vhFull-viewport layouts, fluid typography (inside clamp), background sizing
%Fluid widths within grid/flex layouts, responsive containers
chMax-width for readable line lengths (45–75ch)
frCSS Grid track sizing — distributes available space proportionally

A practical rule of thumb: if it relates to text or spacing, use rem. If it's a fine visual detail like a border, use px. If it needs to fill or divide the viewport, use vw/vh or %. And when you want responsive scaling without breakpoints, reach for clamp().

Convert CSS units instantly

Use the PX to REM Converter, CSS Unit Converter, and CSS Clamp Calculator to translate design specs into production-ready values.

⚡ PX to REM📐 Unit Converter🔢 Clamp Calc
DG
Derek Giordano
Written by the creator of Ultimate Design Tools. BA in Business Marketing.
📚 References & Further Reading
⚡ Try the free Responsive Type Calculator →