CSS units are used to define measurements in web design. They control font size,
spacing, layout width, height, borders, media sizes, responsive containers, and
viewport-based designs. Learning CSS units helps you build flexible, accessible,
and responsive websites.
What Are CSS Units?
CSS units define the size or measurement of a CSS property. For example, when you set
font-size, width, margin, padding,
or height, you usually use a CSS unit.
CSS units are mainly divided into two groups: absolute units and relative units.
Why CSS Units Are Important
Units control element size, spacing, and layout.
Responsive units help websites adapt to different screen sizes.
Relative units improve accessibility and browser zoom support.
Correct units reduce layout problems on mobile devices.
Units like rem, %, vw, and fr help create scalable layouts.
Good unit choices improve readability, usability, and maintainability.
CSS Units Cheatsheet
The following table explains the most common CSS units and when to use them.
Unit
Type
Example
Best Use
px
Absolute
font-size: 16px;
Precise borders, icons, shadows, and fixed measurements.
%
Relative
width: 100%;
Fluid widths and responsive containers.
em
Relative
padding: 1em;
Spacing based on the current element’s font size.
rem
Relative
font-size: 1rem;
Accessible typography and scalable spacing.
vw
Viewport
width: 100vw;
Viewport-based widths and full-screen sections.
vh
Viewport
min-height: 100vh;
Hero sections and full-screen layouts.
vmin
Viewport
font-size: 5vmin;
Responsive sizing based on the smaller viewport side.
vmax
Viewport
font-size: 5vmax;
Responsive sizing based on the larger viewport side.
ch
Font Relative
max-width: 70ch;
Readable text line length.
fr
Grid
grid-template-columns: 1fr 2fr;
CSS Grid flexible columns and rows.
Absolute CSS Units
Absolute units have fixed values. They do not scale based on the parent element or viewport.
The most common absolute unit in web design is px.
Unit
Name
Common Use
px
Pixels
Most common screen unit.
pt
Points
Print styles.
cm
Centimeters
Print-specific layouts.
mm
Millimeters
Print-specific layouts.
in
Inches
Print styles.
.box {
width: 300px;
border: 1px solid #dee2e6;
}
Relative CSS Units
Relative units are based on another value such as the parent size, root font size,
current font size, or viewport size. They are useful for responsive and accessible design.
CSS units do not directly create SEO metadata, but they affect mobile usability,
readability, page experience, accessibility, and Core Web Vitals.
Responsive units improve mobile-friendly layouts.
Readable widths and font sizes improve content engagement.
Stable sizing reduces layout shift.
Viewport units help create flexible sections.
Accessible sizing supports better user experience.
Common CSS Unit Mistakes
Using fixed pixel widths for full layouts.
Using 100vw and causing horizontal scrolling.
Using 100vh on mobile without considering browser UI.
Using too many nested em values and causing unexpected scaling.
Using tiny fixed font sizes that hurt readability.
Not setting max widths for readable content.
Using percentage heights without a defined parent height.
Forgetting to test zoom, mobile, and different screen sizes.
CSS Units Best Practices
Use rem for most font sizes and spacing.
Use em when spacing should scale with the element’s font size.
Use % and max-width for fluid containers.
Use ch to control readable line length.
Use fr for CSS Grid layouts.
Use clamp() for responsive typography.
Use px for borders, small icons, and exact details.
Use dvh carefully for mobile full-height layouts.
Test layouts on mobile, desktop, zoomed text, and different viewport sizes.
Key Takeaways
CSS units define size, spacing, layout, and typography.
Absolute units like px are fixed.
Relative units like rem, em, and % are flexible.
Viewport units like vw, vh, and dvh depend on screen size.
The fr unit is useful for CSS Grid layouts.
Choosing the right unit improves responsiveness, accessibility, readability, and maintainability.
Pro Tip
A simple beginner rule: use rem for text and spacing,
% for fluid widths, fr for grid columns,
ch for readable text width, and px for small precise details.
You now understand CSS units, absolute units, relative units, viewport units,
px, em, rem, percentages, vw,
vh, dvh, ch, fr,
responsive sizing, accessibility, SEO benefits, best practices, and common mistakes.