CSS Units Complete Guide: PX, REM, EM, VW, VH, %

CSS gives you more than a dozen ways to size an element, and picking the wrong one is one of the most common reasons layouts break on other screens. Units fall into two families โ€” absolute, which always render at the same physical size, and relative, which scale based on something else on the page. Here's what each one actually does and when it earns a place in your stylesheet.

Absolute Units

Absolute units don't change based on any parent element, font size, or viewport. What you set is what you get, on every screen.

Relative Units

Relative units scale based on another value โ€” a parent's font size, the root font size, or the browser viewport. They're what makes responsive design possible in the first place.

How the Absolute Units Relate to Each Other

Every absolute unit in CSS is pinned to the same anchor: the CSS reference pixel, defined as 1/96 of an inch. That means all of them convert to pixels with a fixed multiplier, and none of them depend on the actual screen you're looking at.

UnitDefinitionEquals in px
1in1 inch96px
1cm1/2.54 inch37.795px
1mm1/10 cm3.7795px
1Q1/4 mm (quarter-millimetre)0.945px
1pt1/72 inch1.3333px
1pc12pt = 1/6 inch16px

So 12pt is exactly 16px, 1pc is also 16px, and a 2.54cm box is exactly one CSS inch (96px). If you need these numbers for a specific value, the PX to CM and PT to PX converters handle the decimals for you.

How the Relative Units Resolve: Worked Examples

Relative units are only "relative" until the browser computes them. Every one of them ends up as a pixel value; the question is what it's measured against.

rem: always the root font size

With the default root size of 16px, 1.5rem is 24px and 0.875rem is 14px. If a user sets their browser's default font size to 20px, the same 1.5rem becomes 30px โ€” which is exactly the point. Your spacing and type scale grow with the user's preference.

em: the parent font size, compounding

For font-size, em is relative to the parent's font size, and that compounds through nesting:

.menu      { font-size: 1.2em; }  /* 16px ร— 1.2 = 19.2px */
.menu .sub { font-size: 1.2em; }  /* 19.2px ร— 1.2 = 23.04px */

Two levels deep and the text is already 44% larger than intended. For properties other than font-size โ€” padding, margin, width โ€” em is relative to the element's own computed font size, which is why padding: 0.5em 1em on a button scales neatly with the button's text.

%: depends on the property

Percentages are the least uniform unit. width: 50% uses the parent's content width; height: 50% uses the parent's height (and only works if that height is defined); padding-top: 50% is surprisingly relative to the parent's width; and font-size: 150% behaves like 1.5em. Our PX to Percent converter uses the width rule: px รท parent ร— 100.

vw and vh: the viewport

On a 1440px-wide window, 10vw is 144px. On a 390px phone it's 39px. Read more about how browsers define that box in What Is the Viewport in CSS.

Newer Units Worth Knowing

Quick Reference: Which Unit for Which Job

Use caseRecommended unit
Font size, line heightrem
Padding/margin inside a componentem or rem
Borders, box-shadow offsetspx
Full-height hero sectionsvh (or dvh on mobile)
Fluid container widths% or vw
Readable text blocksch, as a max-width
Print stylesheetscm, mm, or pt

A Practical Rule of Thumb

If it should stay the same size no matter what โ€” a 1px border, a 24px icon โ€” use px. If it should scale with the user's font-size preference or with your design's type scale โ€” headings, spacing, line-height โ€” use rem. If it needs to track the browser window itself โ€” a full-bleed hero, a sidebar that's always 30% of the screen โ€” reach for vw/vh or percentages.

Need to move between any of these quickly? Our PX to REM, PX to VH, and PX to CM converters do the math instantly. CSS colors are worth getting right too โ€” see our Color Converter.

Accessibility: Why the Unit Choice Matters

Browsers offer two ways to make text bigger. Page zoom scales everything, including px values, so it works regardless of unit. The default font-size setting, however, only changes the root font size โ€” and only rem, em, and percentage-based font sizes follow it. A heading locked at font-size: 32px ignores that setting completely. That's the main practical argument for rem on text: it respects a choice the user has already made.

Viewport units have a similar trap. font-size: 3vw on its own doesn't grow with either the font-size setting or, in some browsers, with zoom on a fixed window. If you want fluid type, combine it with rem inside clamp():

h1 { font-size: clamp(1.75rem, 1rem + 3vw, 3.5rem); }

The rem parts keep a floor and ceiling tied to the user's preference, while the vw part supplies the smooth scaling between them.

Common Mistakes

Frequently Asked Questions

Is px an absolute or a relative unit?

The CSS specification lists px as an absolute unit, defined as 1/96 of an inch. In practice the browser maps it to physical screen pixels using the device pixel ratio, so one CSS pixel may be drawn with two or three hardware pixels on a high-density display. It's still treated as fixed for layout purposes.

Should I use rem or em for padding and margins?

Use rem when spacing should follow a global scale, such as gaps between page sections. Use em when spacing should follow the element's own text size, such as padding inside a button or badge, so a larger button automatically gets proportionally larger padding.

What is the default value of 1rem?

1rem equals the root element's font size, which is 16px in all major browsers unless the user or your stylesheet changes it.

Can I mix units in one declaration?

Yes. calc(), min(), max(), and clamp() all accept mixed units, for example width: calc(100% - 2rem) or padding: max(1rem, 3vw). The browser resolves each part to pixels and then does the arithmetic.

Try it yourself: pick a value from your current stylesheet and run it through the CSS unit converters to see its equivalent in rem, em, vw, pt, or cm โ€” it's the quickest way to get a feel for how each unit behaves.