Length units define the size and spacing of elements in CSS. Understanding absolute vs relative units is essential for building layouts that look right on every screen.
01
px
Absolute.
02
%
Parent-based.
03
em / rem
Font-based.
04
vw / vh
Viewport.
05
Spacing
Margin & pad.
06
Responsive
Scale well.
Fundamentals
Introduction
In CSS, length units are a key part of designing layouts and elements. Lengths define the size of properties such as width, height, margin, padding, font-size, and more. CSS supports a wide range of units, from absolute values like pixels to relative values like percentages and viewport units.
What Is CSS Length?
CSS length is a measure used to define the dimensions or spacing of an element. Length values control how elements render in relation to the document, their parent, or the viewport.
💡
Beginner Tip
Start with px for learning, then switch to rem for font sizes and % for fluid widths. This combination covers most real-world layouts.
Types of CSS Length Units
CSS offers two main categories of length units:
Absolute length units — Fixed sizes not affected by parent or viewport (e.g. px, cm, in).
Relative length units — Sizes based on another value: parent size, font-size, root font-size, or viewport (e.g. %, em, rem, vw).
Foundation
📝 Absolute Length Units
Absolute units represent a fixed length. Best when you need exact dimensions regardless of screen size. Common absolute units:
px (pixels) — One CSS pixel on the screen. Most common for web design.
cm (centimeters) — 1cm ≈ 37.8px in most browsers.
mm (millimeters) — 1mm = 1/10 of a centimeter.
in (inches) — 1in = 96px.
pt (points) — Print unit; 1pt = 1/72 of an inch.
pc (picas) — Print unit; 1pc = 12 points.
absolute.css
div{width:300px;margin-left:1in;}
📝 Relative Length Units
Relative units scale with parent elements, font sizes, or the viewport. Essential for responsive design:
% (percentage) — Relative to the parent container’s same property.
em — Relative to the element’s font-size (or parent for some properties).
rem — Relative to the root html element’s font-size.
vw (viewport width) — 1vw = 1% of viewport width.
vh (viewport height) — 1vh = 1% of viewport height.
vmin / vmax — Relative to the smaller or larger viewport dimension.
min-height with vh — Add min-height so small viewports don’t collapse panels.
Be consistent — Pick a primary unit strategy per project (e.g. rem + %).
Watch Out
⚠️ Common Pitfalls
Mixing units carelessly — Random px, em, and % without a plan causes inconsistent layouts.
Fixed units only — Using only px for everything makes responsive design harder.
% without parent size — Percentages need a defined parent; otherwise results can be zero or unexpected.
em compounding — Nested em font-sizes multiply; prefer rem for global typography.
100vh on mobile — Mobile browsers may treat 100vh differently; test on real devices.
A11y
♿ Accessibility
Respect user font settings — rem scales when users change browser default font size.
Avoid tiny px text — Very small fixed font sizes block users who need larger text.
Touch targets — Buttons and links need adequate padding (at least 44×44px recommended).
Zoom-friendly layouts — Relative units help content reflow when users zoom to 200%.
Line length — Use max-width with relative units for readable line lengths.
🧠 How CSS Length Works
1
Property accepts a value
You write width: 50%; or padding: 1rem; in a CSS rule.
Declare
2
Browser resolves the unit
Absolute units use fixed values; relative units compute from parent, root, or viewport.
Compute
3
Element is sized
The computed pixel value is applied to layout, painting, and hit-testing.
Render
=
📏
Sized layout
Elements occupy the right space; spacing and typography look intentional.
Compatibility
🖥 Browser Compatibility
All length units in this tutorial — px, %, em, rem, vw, vh, vmin, and vmax — have universal support in modern browsers.
✓ Baseline · Universal support
CSS Length Units
Every unit covered here works in Chrome, Firefox, Safari, Edge, and mobile browsers without prefixes.
100%Modern browsers
Google ChromeFull support
Full support
Mozilla FirefoxFull support
Full support
Apple SafariFull support
Full support
Microsoft EdgeFull support
Full support
OperaFull support
Full support
CSS length units100% supported
Bottom line: Use rem, %, and viewport units confidently in production.
Wrap Up
🎉 Conclusion
CSS lengths are fundamental to controlling the size and spacing of elements in web design. By understanding absolute and relative units, you can create flexible layouts that adapt to various screen sizes.
For responsive sites, relative units like rem, %, and vw/vh are often more versatile than fixed px alone. Practice the four examples, then explore individual properties like width, margin, and font-size.
Length units measure size and spacing in CSS — width, height, margin, padding, font-size, and more. They are either absolute (fixed, like px) or relative (based on parent, root, or viewport, like %, rem, or vw).
px is an absolute unit — 1px is roughly one device pixel. rem is relative to the root html font-size — 1rem equals the root size, so 1.5rem is 1.5 times that. rem scales better for accessible, responsive typography.
Use % when an element should scale with its parent — like a column that is 50% of a container width. Use px for fixed borders, small precise spacing, or when you need exact pixel control.
em is relative to the current element font-size (or parent for some properties). rem is always relative to the root html font-size. rem avoids compounding — nested elements with em can grow unexpectedly.
1vw equals 1% of the viewport width; 1vh equals 1% of the viewport height. They are useful for full-screen sections, hero banners, and layouts that respond to the browser window size.