Code & Dev
Free CSS Unit Converter Online
Convert CSS units: px, rem, em, %, vw, vh and pt. Set your root font size for accurate rem conversion.
Absolute vs relative CSS units
CSS units fall into two fundamental categories. Absolute units are fixed and do not change based on context — px, pt, cm, mm, and in all produce the same visual size regardless of parent elements, viewport, or user preferences. Relative units — rem, em, %, vw, vh — scale based on some reference: a font size, a parent's dimension, or the viewport.
In CSS, even "absolute" units like px are device-independent. On a high-DPI (HiDPI/Retina) display, one CSS px might equal two or more physical device pixels. The browser's devicePixelRatio determines this relationship. This is why px is called a reference pixel — it represents a pixel at 96 DPI regardless of the actual display density.
The choice between absolute and relative units has significant implications for accessibility and responsive design. Browsers allow users to set their preferred base font size — a user who sets 20px as their base benefits from rem-based layouts but not from px-based ones. Modern best practice is to use px only for fine details (borders, box shadows) and rem for everything else.
When to use rem vs px vs em — a practical guide
Scales with root font size. Respects user's browser preference. Consistent across nested components. 1rem = 16px by default.
Pixel-perfect control for fine details. Does not scale with font preferences — intentional for borders and decorative elements.
Relative to the element's own font-size, not the root. Useful when a spacing value should grow proportionally with the component's text.
Relative to parent's dimension. For width/height, references parent's size. Note: padding and margin % always reference parent's width, even for vertical values.
html { font-size: 16px; } /* keep browser default */
body { font-size: 1rem; } /* inherit from root */
/* Use rem everywhere else */
.heading { font-size: 2rem; } /* 32px */
.subheading { font-size: 1.25rem; } /* 20px */
.body-text { font-size: 1rem; } /* 16px */
.small { font-size: 0.875rem; } /* 14px */The cascade and how em units accumulate
The most important thing to understand about em units is that they compound through the DOM. Each em value is calculated relative to the computed font size of the element itself (for font-size) or the element's own computed font size (for other properties). This creates a compounding effect in nested elements.
/* Root: 16px */
.parent { font-size: 1.5em; } /* 1.5 × 16 = 24px */
.child { font-size: 1.5em; } /* 1.5 × 24 = 36px */
.grandchild { font-size: 1.5em; } /* 1.5 × 36 = 54px */
/* With rem — no compounding */
.parent { font-size: 1.5rem; } /* 1.5 × 16 = 24px always */
.child { font-size: 1.5rem; } /* 1.5 × 16 = 24px always */
.grandchild { font-size: 1.5rem; } /* 1.5 × 16 = 24px always */This compounding is precisely why rem was introduced in CSS3. With rem, every value is anchored to the document root, making it predictable regardless of nesting depth.
Em is still useful when you want proportional scaling within a component. For example, a button's padding set in em will scale proportionally as the button's font size changes — making it trivial to create small, medium, and large button variants by only changing font-size.
Viewport units — vw, vh, dvh, svh, lvh
Viewport units measure percentages of the browser viewport. The classic vw and vh units have been available since CSS3, but mobile browsing created a fundamental problem: the browser's address bar and toolbar take up space, causing vh to produce an inaccurate measurement on mobile devices.
vw1% of viewport width. Stable — width doesn't change with UI chrome. Safe to use everywhere.vh1% of viewport height. On mobile, this is calculated from the "large" viewport (browser UI hidden). Can cause elements to be taller than visible screen.svhSmall viewport height — calculated with all browser UI visible. Most conservative: an element sized to 100svh will always fit on screen.dvhDynamic viewport height — updates as the browser UI shows/hides. Best for full-screen mobile layouts, but can cause layout reflow during scrolling.lvhLarge viewport height — same as the original vh behavior. Browser UI is excluded from the measurement.For full-screen mobile layouts, the modern recommendation is: min-height: 100svh as a safe fallback, with min-height: 100dvh for the ideal dynamic behavior where supported.
Print units — pt, cm, mm, in
Physical units are primarily used inside @media print rules to create accurately-sized printed documents. On screen, the browser resolves them at 96 DPI.
| Unit | Full name | In pixels (96 DPI) | Use case |
|---|---|---|---|
| pt | Point | 1.333px | Font sizes in print (12pt body, 14pt headings) |
| pc | Pica | 16px | 1pc = 12pt. Legacy typographic unit |
| cm | Centimetre | 37.795px | Margins, page gutters in print |
| mm | Millimetre | 3.7795px | Fine-grained print measurements |
| in | Inch | 96px | US letter/legal page dimensions |
Responsive design strategies and fluid typography with clamp()
Modern CSS provides powerful tools for fluid layouts that adapt continuously across viewport sizes, without discrete breakpoint jumps.
/* Syntax: clamp(minimum, preferred, maximum) */
h1 {
font-size: clamp(1.5rem, 4vw, 3rem);
/* minimum: 24px, fluid between, maximum: 48px */
}
p {
font-size: clamp(1rem, 1rem + 0.5vw, 1.25rem);
/* Stays 1rem on small screens, grows slightly */
}clamp() eliminates the need for font-size media queries. The middle value (preferred) is typically a vw-based expression. Use rem for min and max to ensure accessibility.
:root {
--space-s: clamp(0.75rem, 2vw, 1rem);
--space-m: clamp(1rem, 3vw, 1.5rem);
--space-l: clamp(1.5rem, 5vw, 2.5rem);
}
.card {
padding: var(--space-m);
gap: var(--space-s);
margin-bottom: var(--space-l);
}Define a fluid spacing scale in :root using clamp(), then reference those custom properties throughout your CSS. Everything scales proportionally with viewport size.
html {
font-size: 62.5%; /* makes 1rem = 10px */
}
body {
font-size: 1.6rem; /* = 16px — restore readable default */
}
/* Now rem math is trivial */
h1 { font-size: 3.2rem; } /* = 32px */
.card { padding: 2.4rem; } /* = 24px */Setting 62.5% makes rem values map cleanly to px: 1.6rem = 16px, 2.4rem = 24px, 4.8rem = 48px. Controversial because it overrides the user's font size preference — consider if accessibility is a priority.
CSS custom properties and unit best practices
Design systems increasingly encode their spacing, typography, and sizing scales as CSS custom properties. This approach separates the scale's values from their application, making global adjustments (like changing the base spacing unit) a one-line change.
FAQ
Common questions
What is the difference between px and rem?
px is an absolute unit — 1px is always one CSS pixel regardless of context. rem (root em) is relative to the root element's font size (typically 16px by default). If the root font size is 16px, then 1rem = 16px. Unlike px, rem scales with the user's browser font size preference, making it better for accessibility and responsive design.
When should I use rem instead of px?
Use rem for font sizes, spacing, and most layout measurements where accessibility matters. Users can override the browser's base font size (common for low-vision users), and rem units respect that override. Use px for things that should not scale with font size: borders (1px), box shadows, and precise icon sizes.
What is the difference between em and rem?
Both are relative units, but rem is relative to the root (html) element's font size, while em is relative to the nearest ancestor's font size. This means em units compound: if a parent has font-size: 1.5em and a child has font-size: 1.5em too, the child's effective size is 1.5 × 1.5 = 2.25× the base. rem avoids this compounding by always referencing the root.
What are vw and vh units?
vw (viewport width) and vh (viewport height) are percentages of the browser window's dimensions. 1vw = 1% of the viewport width, so 100vw = full width. They are ideal for full-screen layouts, hero sections, and fluid typography. On mobile, vh is notoriously tricky because the browser UI (address bar) affects the viewport height — use svh (small viewport height) or dvh (dynamic viewport height) for mobile-first designs.
What is 1rem in pixels?
By default, browsers set the root font size to 16px, so 1rem = 16px. However, this is configurable — users can change it in browser settings, and developers can change it with html { font-size: 62.5% } (making 1rem = 10px for easier math). This converter lets you set a custom base font size.
What are CSS print units (pt, cm, mm, in)?
Point (pt), centimeter (cm), millimeter (mm), and inch (in) are physical units meaningful for print stylesheets (@media print). 1in = 96px = 72pt = 2.54cm = 25.4mm. On screen, these units are resolved to pixels at 96 DPI. Use them in @media print rules for accurate printed document layout, not for screen layouts.
What does % mean as a CSS unit?
% (percent) is relative to the parent element's corresponding value. For font-size, 100% equals the inherited font-size. For width, 100% equals the parent's content width. For margins and padding with the % unit, they are calculated relative to the parent's width, even for top/bottom values — a common source of confusion.
What is the ch unit?
ch (character) equals the width of the "0" character in the current font. It's useful for sizing text inputs and code blocks: width: 80ch creates an input that fits 80 characters, matching the traditional terminal width. It approximates average character width for proportional fonts but is exact for monospace fonts.
How do I do responsive typography without breakpoints?
Use the CSS clamp() function: font-size: clamp(1rem, 2.5vw, 2rem) sets a minimum, a fluid middle, and a maximum. No media queries needed. The browser interpolates the font size between the min and max as the viewport grows. Combined with rem as min and max and vw as the fluid value, you get accessible, responsive typography in one line.
More in Code & Dev