CSS Unit Converter
Convert between px, rem, em, %, vw, and vh instantly.
| Pixels | REM | EM | Points |
|---|
📖 Complete Guide to CSS Units and Responsive Typography
Understanding CSS units is fundamental to creating responsive, accessible, and well-designed websites. The choice between pixels, rems, ems, percentages, and viewport units affects not only how your designs scale across different devices but also how accessible your content is to users who adjust their browser settings. This comprehensive guide explores each unit type and provides practical guidance for making the right choices in your projects.
The Two Categories of CSS Units
CSS units fall into two fundamental categories: absolute units that represent fixed physical or pixel measurements, and relative units that scale based on other values in the document. Understanding this distinction is crucial because it determines how your designs respond to different contexts.
Absolute units maintain the same size regardless of surrounding context. If you set an element to 16px, it will be 16 pixels wide or tall on any device (accounting for device pixel ratio). Relative units, in contrast, compute their actual size based on another value—the parent element's font size, the root font size, the viewport dimensions, or other references.
Pixels: The Traditional Baseline
Pixels (px) are the most familiar CSS unit, representing the smallest addressable element on a screen. In the early web, pixels provided reliable, predictable sizing. However, the relationship between CSS pixels and physical pixels has become complicated with the advent of high-DPI displays.
Modern browsers interpret CSS pixels as "reference pixels" rather than physical device pixels. A reference pixel is defined as the visual angle of 1/96th of an inch at arm's length viewing distance. On high-DPI (retina) displays, one CSS pixel might correspond to two, three, or more physical pixels. This abstraction ensures that content sized in CSS pixels appears approximately the same physical size across devices with different pixel densities.
When to Use Pixels
Pixels remain appropriate for elements that should maintain fixed sizes regardless of user preferences: thin borders (1-2px), hairline dividers, box shadows, and some decorative elements. They are also useful during development for establishing initial sizes before converting to relative units for production.
When to Avoid Pixels
Avoid pixels for font sizes and spacing in production code. When users adjust their browser's default font size (for accessibility or preference reasons), pixel-based font sizes do not scale. This creates accessibility barriers for users with visual impairments who rely on larger text. Similarly, pixel-based spacing does not scale with text, potentially creating awkward proportions when users increase their font size.
REM: The Accessibility Champion
The rem unit (root em) represents the font size of the root element—typically the html element. In most browsers, the default root font size is 16px, so 1rem equals 16px unless the user or your stylesheet changes this.
The crucial advantage of rem is accessibility. When users increase their browser's default font size (a common accessibility need), all rem-based measurements scale proportionally. A user who sets their browser to use 20px as the base size will see your 1rem text at 20px rather than the typical 16px. All your spacing and sizing scales with this preference, maintaining design proportions while respecting user needs.
The 62.5% Technique
Some developers set the root font size to 62.5% (which equals 10px on default browsers), making mental calculations easier: 1.6rem equals 16px, 2rem equals 20px. However, this technique has drawbacks. It can confuse developers unfamiliar with the convention, and it may interact unexpectedly with third-party styles. Modern development with this calculator makes the math easy enough that the 62.5% technique is often unnecessary.
Using REM for Spacing
Beyond typography, rem units excel for spacing (padding, margin, gap). When users increase their font size, rem-based spacing increases proportionally, maintaining comfortable proportions between text and surrounding space. This creates a more cohesive experience for users who need larger text.
EM: Context-Sensitive Scaling
The em unit is relative to the font size of the element itself (for font-size) or the computed font size of the parent element (for other properties like padding and margin). This context sensitivity makes em powerful but potentially confusing.
The Compounding Effect
The most important thing to understand about em is that it compounds. If you have a parent element with font-size: 1.5em and a child also with font-size: 1.5em, the child's actual font size is 1.5 × 1.5 = 2.25 times the root font size. This multiplication continues with each level of nesting.
Compounding can be useful—creating naturally scaling typography hierarchies—or problematic, leading to unexpectedly large or small text in deeply nested elements. For this reason, many developers prefer rem for font sizes while using em selectively for component-internal spacing.
When EM Shines
Em units are ideal for spacing that should scale with the local font size. Button padding in em creates buttons where the internal spacing remains proportional regardless of the font size. Icon sizing in em keeps icons visually balanced with adjacent text. Component margins in em maintain proportions when components are resized.
Percentages: Relative to Parents
Percentage units are relative to a parent property value. For width and horizontal margins, percentages are relative to the parent's width. For font-size, percentages are relative to the parent's font-size. For height, percentages are relative to the parent's height (if defined).
Percentages are foundational to fluid layouts. A width of 50% creates an element that always occupies half of its container, regardless of the container's size. This responsiveness is why percentages remain essential for layout even with modern tools like flexbox and grid.
Percentage Typography
For font sizes, percentages work similarly to em: 100% equals the parent font size, 150% equals 1.5 times the parent font size. Like em, percentages compound through nesting. Most developers prefer rem over percentages for typography because rem provides a stable reference point regardless of nesting depth.
Viewport Units: Full-Screen Responsiveness
Viewport units (vw, vh, vmin, vmax) are relative to the browser viewport dimensions. One vw equals 1% of the viewport width; one vh equals 1% of the viewport height. Vmin is the smaller of vw and vh, while vmax is the larger.
Use Cases for Viewport Units
Full-screen sections commonly use 100vh to occupy the full viewport height. Hero sections, landing page folds, and modal overlays benefit from viewport-height sizing. Viewport-width sizing helps create full-bleed elements that extend beyond their containers.
Fluid typography using vw creates text that scales smoothly with viewport width. A headline set to 5vw would be smaller on mobile and larger on desktop, maintaining proportional size. However, pure vw typography lacks both minimum and maximum bounds, potentially becoming unreadably small on narrow screens or absurdly large on wide monitors.
The clamp() Function
Modern CSS provides the clamp() function for bounded fluid sizing: clamp(minimum, preferred, maximum). For typography, clamp(1rem, 2.5vw, 2rem) creates text that scales with viewport width but never falls below 1rem or exceeds 2rem. This combines the fluidity of viewport units with the safety of fixed bounds.
Viewport Unit Gotchas
On mobile browsers, 100vh can be problematic because it includes the browser's address bar height. When users scroll and the address bar hides, elements sized at 100vh may appear shorter than intended. The newer dvh, svh, and lvh units (dynamic, small, and large viewport height) address this by accounting for dynamic browser UI, though browser support is still maturing.
Points: The Print Heritage
Points (pt) come from traditional typography, where one point equals 1/72 of an inch. While not commonly used in web design, points occasionally appear in print stylesheets or designs migrated from print-oriented tools.
For screen design, pixels and relative units typically serve better than points. However, understanding the conversion (1pt ≈ 1.333px at 96 DPI) helps when interpreting designs from print-focused tools like Adobe InDesign or when creating print stylesheets.
Choosing the Right Unit
For font sizes, use rem to ensure accessibility and consistent scaling. For component-internal spacing (like button padding), em provides proportional scaling with local font size. For layout dimensions, percentages or viewport units enable responsive sizing. For borders and shadows, pixels provide precise, consistent rendering.
For media query breakpoints, em is often recommended because it scales with user font preferences, triggering responsive breakpoints at appropriate perceptual sizes rather than fixed pixel widths.
❓ Frequently Asked Questions
Both rem and em are relative units, but they have different reference points. Rem (root em) is always relative to the root element's font size—typically the html element, which defaults to 16px in most browsers. Em is relative to the computed font size of the element itself (for font-size) or its parent (for other properties). The crucial difference is that em compounds through nesting: if you have nested elements each set to 1.5em, the inner elements grow exponentially (1.5 × 1.5 × 1.5...). Rem does not compound because it always references the same root value. For this reason, rem is generally preferred for consistent typography, while em is useful for component-specific spacing that should scale with local font size.
16px became the default font size for browsers through historical convention. Early browser developers chose it as a readable size for screen text at typical viewing distances. When the CSS specification standardized relative units like em and rem, browsers maintained 16px as the default to preserve backwards compatibility. Today, 16px remains the default in all major browsers, making it the standard reference for rem calculations. However, users can change this default in their browser settings for accessibility reasons, which is precisely why using rem for font sizes is important—it respects these user preferences.
Use rem for font sizes in production code. When users change their browser's default font size for accessibility (a common need for users with visual impairments), rem-based font sizes scale accordingly while px-based sizes do not. Using pixels for font sizes creates accessibility barriers because users who need larger text will not see your content at their preferred size. The WCAG accessibility guidelines specifically recommend allowing text to scale with user preferences. Pixels are acceptable for non-scalable elements like borders and shadows, but typography should use rem to ensure all users can read your content comfortably.
Viewport units on mobile can be tricky because mobile browsers have dynamic UI elements like address bars that affect the available viewport size. The classic 100vh might be larger than the visible area when the address bar is visible, causing content to be cut off. When users scroll and the address bar hides, the viewport expands but elements do not resize. To address this, CSS now includes dvh (dynamic viewport height), svh (small viewport height—minimum with UI showing), and lvh (large viewport height—maximum with UI hidden). These newer units are increasingly supported and provide more predictable behavior on mobile devices. For broad compatibility, consider JavaScript-based solutions or avoid 100vh for critical mobile layouts.
The 62.5% technique involves setting html { font-size: 62.5%; } which results in 1rem equaling 10px (62.5% of the default 16px). This makes mental math easier: 1.6rem equals 16px, 2.4rem equals 24px, etc. However, this technique has drawbacks. It can confuse other developers unfamiliar with the convention. It may conflict with third-party components expecting the standard 16px base. Some argue it also slightly complicates accessibility because you must remember that your base is 10px not 16px. With modern tools like this converter making calculations trivial, many developers prefer keeping the natural 16px base and using calculators when needed.
Many experts recommend em for media query breakpoints rather than px. Em-based breakpoints scale with user font preferences, which creates more appropriate responsive behavior. If a user has increased their default font size, they likely want the mobile layout sooner (at a wider viewport) because content will be larger. An em-based breakpoint automatically provides this behavior. For example, instead of @media (min-width: 768px), use @media (min-width: 48em). This calculates to 768px at the default 16px base, but scales up for users with larger font settings, triggering the responsive change at a perceptually appropriate point regardless of their preferences.
Why Ads?
Ads help keep DC Tools free and running. Thank you for your support!
Found a Bug?
Help us improve! Report issues and suggestions.