CSS Units Explained: PX vs REM vs EM vs VW vs VH in 2026
A complete guide to CSS units. Learn when to use pixels, rem, em, vw, and vh with practical examples and conversion tips for responsive web design.
Why CSS Units Matter
Choosing the right CSS unit can make or break your website's responsiveness. If you have ever resized a browser window and watched elements overlap or text become unreadable, the wrong unit choice is usually the culprit. In this guide, we will cover every major CSS unit, explain when each one makes sense, and give you practical rules you can follow on every project.
There are two broad families of CSS units: absolute units and relative units. Absolute units like pixels (px) always represent the same physical size on screen. Relative units like rem, em, vw, and vh scale based on some reference value such as the root font size or the viewport dimensions.
Pixels (PX): The Familiar Default
Pixels are the unit most developers learn first. One pixel corresponds to one dot on a standard-density display. On high-DPI screens (Retina displays, for example), the browser handles the scaling automatically, so 16px of text still looks the same physical size whether you are on a 1x or 2x display.
Pixels are predictable. If you set a box to 300px wide, it will always be 300px wide regardless of parent elements, font sizes, or viewport dimensions. That predictability is both their strength and their weakness.
When to use PX
- Borders and box shadows where you want a consistent thin line (1px border)
- Small, fixed-size elements like icons or decorative elements
- Media query breakpoints (though rem-based breakpoints are gaining traction)
- When pixel-perfect alignment with a design mockup is required
When NOT to use PX
- Font sizes (breaks accessibility when users change their browser font size)
- Spacing and layout that should scale with user preferences
- Any dimension that needs to adapt to different screen sizes
REM: The Accessibility Champion
REM stands for root em. It is relative to the root element's font size, which is usually the html element. By default, browsers set the root font size to 16px. So 1rem equals 16px, 2rem equals 32px, and 0.5rem equals 8px.
The biggest advantage of rem is accessibility. When a user goes into their browser settings and changes the default font size from 16px to 20px (common for users with visual impairments), everything sized in rem scales up proportionally. Text, spacing, containers, everything grows together and remains readable.
/* Base: 16px = 1rem */
h1 {
font-size: 2rem; /* 32px */
margin-bottom: 1rem; /* 16px */
}
p {
font-size: 1rem; /* 16px */
line-height: 1.75rem; /* 28px */
}Quick conversion: To convert px to rem, divide by 16. So 24px / 16 = 1.5rem. Or use our free PX to REM converter tool to do it instantly.
Free PX to REM Converter
Convert pixels to rem units instantly with adjustable base font size.
Try it freeEM: Context-Sensitive Scaling
The em unit is relative to the font size of its parent element, not the root element. This makes it useful for components that need to scale based on their own context. For example, if a button has a font size of 18px and you set its padding to 1em, the padding will be 18px.
The tricky part about em is compounding. If a parent has font-size: 1.2em and a child also has font-size: 1.2em, the child's actual font size is 1.2 times 1.2 times the root, which is 1.44 times the root. Nesting multiple levels of em-based font sizes can quickly lead to unexpectedly large or small text.
When to use EM
- Component-level padding and margins that should scale with the component's font size
- Button padding (so padding automatically adjusts if you make the button text larger)
- Icon sizes inside text (so the icon scales with the surrounding text)
Free PX to EM Converter
Convert pixels to em units based on parent font size.
Try it freeVW and VH: Viewport-Based Units
VW (viewport width) and VH (viewport height) are relative to the browser viewport dimensions. 1vw equals 1% of the viewport width, and 1vh equals 1% of the viewport height. A full-screen hero section set to 100vh will always fill the entire screen height.
These units are powerful for creating fluid, responsive layouts that adapt to any screen size without media queries. A heading set to 5vw will be large on a desktop monitor and smaller on a phone, scaling smoothly across every viewport width.
.hero {
height: 100vh;
display: flex;
align-items: center;
}
.hero-title {
font-size: clamp(2rem, 5vw, 4rem);
}The mobile 100vh problem
On mobile browsers, 100vh can be taller than the visible screen because the address bar takes up space. The modern fix is to use 100dvh (dynamic viewport height) instead.
Free PX to VW Converter
Convert pixels to viewport width units for fluid responsive layouts.
Try it freePercentage: Relative to Parent
Percentage units are relative to the parent element's size. If a parent div is 600px wide and a child is set to 50%, the child will be 300px. Percentages are the foundation of fluid grid systems and remain useful for max-width constraints and image sizing.
Quick Reference Table
| Unit | Relative To | Best For | Avoid For |
|---|---|---|---|
| px | Nothing (absolute) | Borders, shadows, small fixed elements | Font sizes, spacing |
| rem | Root font size (16px) | Font sizes, spacing, layout | Rare cases where parent context matters |
| em | Parent font size | Component padding, button sizing | Font sizes (compounding issue) |
| vw | Viewport width | Fluid typography, full-width elements | Small text (too small on mobile) |
| vh | Viewport height | Full-screen sections, hero banners | Mobile layouts (address bar issue) |
| % | Parent element size | Grid columns, image widths | When parent size is unpredictable |
Practical Recommendations for 2026
- Use rem for all font sizes. This guarantees accessibility and consistent scaling.
- Use rem for spacing (margins, paddings) to maintain proportional layouts.
- Use em for component-internal padding where scaling with font size is desired.
- Use vw sparingly for fluid typography, always with clamp() to set min and max sizes.
- Use px only for borders, outlines, box shadows, and tiny decorative details.
- Use percentage for grid column widths and max-width constraints.
- Use dvh instead of vh for full-screen mobile layouts.
Converting Between CSS Units
If you are working with a design file that specifies everything in pixels and need to convert to rem or other units, we have built a complete set of free CSS unit converters. Each one handles the math instantly so you can focus on building rather than calculating.
- PX to REM Converter for font sizes and spacing
- PX to EM Converter for component-level scaling
- PX to VW Converter for fluid responsive values
- PX to VH Converter for viewport-height elements
- PX to Percent Converter for fluid grid layouts
- PT to PX Converter for print-to-screen conversion
Related Articles
PX to REM Conversion: Why You Should Stop Using Pixels in CSS
Learn why rem units are better than pixels for accessible, responsive web design. Includes conversion formula, examples, and a free PX to REM converter tool.
CSSCSS to Tailwind Migration: A Complete Step-by-Step Guide
Migrate your CSS codebase to Tailwind CSS with this practical guide. Covers class mapping, responsive design conversion, and a free automated converter tool.
CSSWhat is REM in CSS? A Practical Guide with Examples
Understand the rem unit in CSS and why it matters for responsive, accessible web design. Practical examples, browser support, and conversion tips included.