CSS units tell the browser how large, small,
wide, tall, or spaced an element should be.
They are used with properties such as width, height,
padding, margin, font-size,
gap, border, and many other CSS properties.
Choosing the right unit is an important part of building responsive websites.
A fixed value such as 20px can be useful for a border or small UI detail,
while rem, percentages, viewport units, and other relative units
are often more useful when a component needs to adapt to its environment.
Common CSS Units
px
A CSS pixel unit commonly used for precise dimensions.
%
A relative value whose reference depends on the property and layout context.
em
A font-relative unit whose reference depends on the element and property.
rem
Relative to the root element's font size.
vw
Relative to the viewport width.
vh
Relative to the viewport height.
What Is a CSS Length Unit?
A CSS length consists of a numeric value followed by a unit in most cases. For example:
width: 300px;
padding: 1rem;
margin: 5%;
font-size: 2em;
The number describes the amount, while the unit describes how that amount should be interpreted. CSS length units are broadly divided into absolute and relative units.
Absolute vs Relative CSS Units
Absolute Units
These represent fixed CSS dimensions in the context of CSS.
The most commonly used one in web development is px.
width: 300px;
Relative Units
Their resolved size depends on another value or environment, such as the parent, root font size, viewport, or element font size.
width: 80%;
What Is px in CSS?
px stands for CSS pixel.
It is one of the most widely used CSS units for precise UI dimensions.
.card {
width: 320px;
border-radius: 12px;
border: 1px solid #cbd5e1;
}
.button {
padding: 10px 16px;
}
Pixel values are useful when a design calls for predictable dimensions. They are commonly used for borders, small spacing adjustments, icon dimensions, radii, and other UI details.
However, using px for every dimension can make a layout less adaptable.
Responsive interfaces usually benefit from combining pixel values with relative sizing techniques.
Is px Always Literally One Physical Pixel?
No. A CSS pixel is a CSS unit used by the browser and should not be treated as a guaranteed one-to-one measurement of a physical device pixel. Devices can have different pixel densities and display scaling.
For web development, the important idea is that px provides a stable CSS dimension,
while the physical display hardware may represent that dimension using a different number of device pixels.
What Is % in CSS?
The percentage unit represents a value relative to another relevant dimension or value. Exactly what it is relative to depends on the property being used and the layout context.
.container {
width: 800px;
}
.content {
width: 50%;
}
In a typical block formatting context, the percentage width of the child is resolved relative to the containing block's width. This makes percentages useful for fluid layouts.
When Should You Use Percentages?
Percentages are useful when an element should scale with its containing layout.
.column {
width: 50%;
}
.image {
max-width: 100%;
}
.progress {
width: 75%;
}
They are especially common for fluid widths, progress bars, image constraints, and layouts that need to respond to the available container size.
What Is em in CSS?
em is a font-relative unit.
Its exact reference depends on the property being used and the element's font-size context.
For example, when em is used for an element's
font-size, it is based on the inherited font size.
For many other properties, em is based on the element's computed font size.
body {
font-size: 16px;
}
.card {
font-size: 1.25em;
padding: 1em;
}
Once the card's font size is established, its padding: 1em
is based on that element's font size.
Why Can em Become Complicated?
Because em can depend on the element's font-size context,
nested components can produce compounded sizing.
.parent {
font-size: 1.2em;
}
.child {
font-size: 1.2em;
}
The child's resulting font size can be relative to the inherited size from the parent,
which can make deeply nested em-based typography harder to predict.
What Is rem in CSS?
rem means root em.
It is relative to the font size of the document's root element,
normally the html element.
html {
font-size: 16px;
}
h1 {
font-size: 2rem;
}
.card {
padding: 1.5rem;
}
With a 16px root font size, 2rem resolves to 32px
and 1.5rem resolves to 24px.
em vs rem: What Is the Difference?
| Unit | Main Reference | Common Use |
|---|---|---|
em |
Element font-size context | Component-relative typography and spacing |
rem |
Root element font-size | Consistent page-level typography and spacing |
Why Is rem Popular for Typography?
Because rem is tied to the root font size rather than a deeply nested component,
it provides a consistent reference for sizing across a page.
body {
font-size: 1rem;
}
h1 {
font-size: 2.5rem;
}
h2 {
font-size: 2rem;
}
p {
font-size: 1rem;
}
This makes a spacing and typography scale easier to maintain.
What Are vw and vh in CSS?
vw and vh are viewport-relative units.
vw
1vw represents 1% of the viewport width.
width: 50vw;
vh
1vh represents 1% of the viewport height.
height: 50vh;
When Should You Use vw?
vw is useful when the design should respond directly to viewport width.
It can be useful for fluid typography, large decorative elements,
hero layouts, and other viewport-aware designs.
.hero-title {
font-size: 5vw;
}
However, using only vw for text can allow the value to become too small
on narrow screens or too large on very wide screens.
A bounded value such as clamp() is often more robust.
.hero-title {
font-size:
clamp(
2rem,
5vw,
4rem
);
}
When Should You Use vh?
vh can be useful when an element should have a size related to the viewport height,
such as a large hero area or an intentionally screen-oriented section.
.hero {
min-height: 60vh;
}
Be careful with viewport-height layouts on mobile devices.
Browser interface changes can affect the available viewport area.
Modern CSS also provides newer viewport units such as dvh,
svh, and lvh when the exact viewport behavior matters.
What Are vmin and vmax?
vmin and vmax are viewport-relative units based on the
smaller or larger viewport dimension respectively.
vmin
Based on the smaller of the viewport's width and height.
width: 20vmin;
vmax
Based on the larger of the viewport's width and height.
width: 20vmax;
What Is ch in CSS?
The ch unit is based on the width of the
0 glyph in the element's font.
It is useful for creating readable text measure and input-related sizing.
article {
max-width: 65ch;
}
A value such as 65ch is not a universal guarantee of an exact number of characters per line,
because glyph widths differ.
It is better understood as a font-relative measure for controlling text width.
What Are ex, lh and rlh?
CSS also includes font-relative and line-height-relative units that can be useful in specialized situations.
| Unit | Reference | Example |
|---|---|---|
ex |
Font x-height | height: 10ex; |
lh |
Element's line-height | margin-block: 1lh; |
rlh |
Root element's line-height | padding-block: 1rlh; |
These units are useful when a design needs to relate dimensions directly to typography or line-height.
For everyday application UI, rem, em, percentages, and other common units
are encountered more frequently.
What Are CSS Absolute Length Units?
CSS defines several absolute length units, including cm,
mm, Q, in, pt, pc,
and px.
The non-pixel units are more commonly encountered in print-oriented or specialized layouts.
For typical web application interfaces, px is the absolute unit developers most often use.
What Are Relative Font Units?
Font-relative units are useful when dimensions should adapt along with typography.
The most important examples for everyday CSS are em and rem.
.button {
font-size: 1rem;
padding: 0.75em 1.25em;
}
.card {
padding: 1.5rem;
}
A component can therefore use em when its internal spacing should scale
with its own text size, while rem can provide a more globally consistent scale.
How Should You Choose CSS Units?
| Requirement | Common Choice | Why |
|---|---|---|
| Thin borders | px |
Simple and predictable UI detail |
| Page-level spacing | rem |
Consistent root-relative scale |
| Component-relative spacing | em |
Can scale with component typography |
| Fluid container width | % |
Responds to containing space |
| Viewport-aware sizing | vw / vh |
Responds to viewport dimensions |
| Readable text width | ch |
Useful for font-relative text measure |
| Fluid but bounded value | clamp() |
Combines minimum, preferred, and maximum values |
Should You Always Use rem Instead of px?
No.
Different units solve different problems.
Using rem everywhere simply because it is relative can make some styles less clear.
For example, a one-pixel border is straightforward:
.card {
border: 1px solid #cbd5e1;
}
A border does not necessarily need to participate in a scalable spacing system. Unit selection should be based on the purpose of the value.
Should You Always Use px for Layouts?
Using pixels for every width, height, font size, and spacing value can create rigid layouts. Responsive interfaces often need values that adapt to their context.
Rigid
.container {
width: 1200px;
}
Flexible
.container {
width: min(
calc(100% - 2rem),
1200px
);
}
Combining Different CSS Units
Good CSS rarely requires choosing only one unit for an entire project. Different values can use different units according to their purpose.
.container {
width: 100%;
max-width: 70rem;
padding-inline: 1.5rem;
}
.title {
font-size: clamp(
2rem,
5vw,
4rem
);
}
.card {
padding: 1.5rem;
border: 1px solid #cbd5e1;
}
.article {
max-width: 65ch;
}
This example combines percentages, rem, vw,
px, and ch because each solves a different sizing problem.
CSS Units in Responsive Typography
A modern responsive heading often benefits from a combination of relative and bounded sizing.
h1 {
font-size:
clamp(
2rem,
4vw,
4rem
);
line-height: 1.1;
}
The viewport component provides fluid behavior while clamp()
prevents the result from becoming unreasonably small or large.
CSS Units and Responsive Containers
A common page container can combine percentage width with a maximum width and root-relative internal spacing.
.container {
width: 100%;
max-width: 75rem;
padding-inline: 1rem;
margin-inline: auto;
}
This allows the container to shrink on small screens while preventing it from growing indefinitely on large screens.
CSS Units and Spacing Systems
A consistent spacing system makes a design easier to maintain.
rem is often useful for creating a page-level spacing scale.
:root {
--space-1: 0.25rem;
--space-2: 0.5rem;
--space-3: 0.75rem;
--space-4: 1rem;
--space-6: 1.5rem;
--space-8: 2rem;
}
.card {
padding: var(--space-6);
}
.section {
margin-block: var(--space-8);
}
CSS Units in Flexbox and Grid
CSS units are also used heavily inside Flexbox and Grid layouts.
Flexible grid tracks can use fr, while other dimensions can combine
lengths, percentages, and intrinsic sizing functions.
.cards {
display: grid;
grid-template-columns:
repeat(
auto-fit,
minmax(220px, 1fr)
);
gap: 1rem;
}
The fr unit is different from the length units discussed earlier.
It represents a fraction of the available space in the Grid container.
What Is the fr Unit?
The fr unit is a Grid-specific flexible sizing unit.
It distributes leftover space among grid tracks according to their fractional values.
.layout {
display: grid;
grid-template-columns:
1fr 2fr;
}
The two columns divide the available track space in a 1-to-2 proportion, subject to the Grid sizing rules and other constraints.
CSS Units and calc()
CSS math functions allow different units to participate in the same expression.
calc() is especially useful when a dimension needs a fixed offset combined with a relative value.
.content {
width:
calc(
100% - 2rem
);
}
This creates a width based on the available percentage dimension while reserving a fixed amount of space.
CSS Units and min(), max(), and clamp()
Modern CSS sizing frequently combines multiple units through math functions.
.container {
width:
min(
calc(100% - 2rem),
75rem
);
}
.sidebar {
width:
max(
16rem,
25vw
);
}
.title {
font-size:
clamp(
2rem,
5vw,
4rem
);
}
These functions make it possible to express constraints directly in CSS instead of creating a large number of breakpoint-specific overrides.
Common CSS Unit Mistakes
Using px for Everything
A layout built entirely from fixed dimensions can become difficult to adapt across screen sizes.
Confusing em and rem
em and rem have different reference behavior.
Treating them as interchangeable can produce unexpected typography and spacing.
Using vw for Every Font Size
Pure viewport-based typography can become too small or too large. Bounded fluid values are often safer.
Using Fixed Heights on Dynamic Content
Content may require more space than expected, so fixed heights can cause overflow.
Misunderstanding Percentage References
Percentage values do not always refer to the viewport. Their reference depends on the property and layout context.
Using viewport Units Without Boundaries
Very large screens can make viewport-based values unexpectedly large.
A Practical Responsive Page Example
*,
*::before,
*::after {
box-sizing: border-box;
}
body {
margin: 0;
font-size: 1rem;
}
.page {
width: min(
calc(100% - 2rem),
75rem
);
margin-inline: auto;
padding-block: 3rem;
}
.hero-title {
font-size:
clamp(
2rem,
5vw,
4rem
);
}
.article {
max-width: 65ch;
}
.cards {
display: grid;
grid-template-columns:
repeat(
auto-fit,
minmax(220px, 1fr)
);
gap: 1rem;
}
.card {
padding: 1.5rem;
border:
1px solid #cbd5e1;
border-radius: 12px;
}
Notice that the example does not rely on one unit for everything.
Percentages and calc() help the page use available width,
rem provides consistent spacing,
ch limits readable text width,
fr distributes Grid space,
and clamp() creates bounded fluid typography.
How to Choose a CSS Unit in Practice
CSS Units Best Practices
Choose Units by Purpose
Do not use the same unit everywhere just because it is familiar.
Use rem for Consistency
Root-relative units are useful for page-level typography and spacing systems.
Use % for Container Relationships
Percentages are useful when elements should respond to their containing space.
Use em for Component Scaling
em can be useful when spacing should scale with a component's typography.
Bound Viewport-Based Values
Use min/max constraints or functions such as clamp when fluid sizing needs boundaries.
Test at Different Sizes
A unit choice that looks correct on one screen may behave differently elsewhere.
Key Takeaways
CSS units define how dimensions such as width, height, spacing, and typography are interpreted.
px is a common fixed CSS dimension and is useful for precise UI details.
% creates a relative value whose reference depends on the property and layout context.
em is font-relative and can be useful for component-level scaling.
rem is relative to the root element's font size and is commonly useful for typography and spacing systems.
vw and vh relate sizes to the viewport dimensions.
vmin and vmax use the smaller or larger viewport dimension.
ch, ex, lh, and rlh provide specialized font- and line-height-relative sizing.
Grid also provides the fr unit for distributing available space between tracks.
Modern responsive CSS often combines several units with functions such as
calc(), min(), max(), and clamp().
The best CSS unit is not always the smallest or most modern one; it is the unit whose reference behavior matches the problem you are solving.