CSS Length Units

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 4 Examples
Units

What You’ll Learn

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.

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).

📝 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.
relative.css
.container {
  width: 80%;
  padding: 2em;
}

.box {
  height: 50vh;
}

📝 Using Length in CSS

Length units apply to many CSS properties:

  • Width and height — Element dimensions.
  • Padding and margin — Inner and outer spacing.
  • Border width — Border thickness.
  • Font-size — Text size (often rem or em).
  • Gap — Space between grid or flex items.
length-usage.css
div {
  width: 100px;
  height: 50vh;
  margin: 20px;
  padding: 1em;
}
rem % px vh

⚡ Quick Reference

UnitTypeRelative toCommon use
pxAbsoluteScreen pixelsBorders, fixed icons, precise spacing
%RelativeParent elementFluid columns, responsive widths
emRelativeElement font-sizePadding scaled to text size
remRelativeRoot font-sizeTypography, consistent spacing
vwRelativeViewport widthFull-width sections
vhRelativeViewport heightHero sections, min-height panels
vminRelativeSmaller viewport sideSquare elements that fit any screen
vmaxRelativeLarger viewport sideLarge display typography

When to Use Each Unit

  • px — Hairline borders, shadows, icons, and when you need pixel-perfect control.
  • % — Layout columns and elements that should fill a fraction of their parent.
  • rem — Font sizes, margins, and padding for accessible, scalable typography.
  • em — Component-internal spacing that scales with that component’s text.
  • vw / vh — Full-viewport sections, hero banners, and responsive height panels.
  • cm / in / pt — Print stylesheets; rarely used for screen layouts.

👀 Live Preview

Compare fixed pixels, percentage width, and rem-based text:

width: 180px (absolute)
width: 60% of parent
font-size: 1.125rem

Text sized with rem units

Examples Gallery

Practice absolute px units, relative % and em, rem with vh, and a mixed-units layout.

🔠 Absolute Units

Fixed pixel dimensions for precise control.

Example 1 — Pixels (px)

Set a fixed width with 300px that does not change when the window resizes.

px.css
.box {
  width: 300px;
  height: 80px;
  background: #2563eb;
}
Try It Yourself

How It Works

px stays the same regardless of parent or viewport. Resize the browser — the box width stays fixed.

🎨 Relative Units

Units that scale with parent, font-size, or viewport.

Example 2 — Percentage (%) and em

Width as 80% of the parent; padding as 2em based on font-size.

pct-em.css
.container {
  width: 80%;
  padding: 2em;
  font-size: 16px;
}
Try It Yourself

How It Works

With font-size: 16px, 2em padding equals 32px. The container shrinks when its parent is narrower.

Example 3 — rem and vh

Typography with rem; panel height with 30vh (30% of viewport height).

rem-vh.css
html { font-size: 16px; }

h1 { font-size: 1.25rem; }

.panel {
  height: 30vh;
}
Try It Yourself

How It Works

1.25rem = 20px when root is 16px. 30vh grows and shrinks as you resize the browser window vertically.

Example 4 — Mixed Length Units

Combine %, px, em, and rem on one element.

mixed.css
.box {
  width: 50%;
  height: 200px;
  margin: 2em;
  padding: 1rem;
}
Try It Yourself

How It Works

Real layouts often mix units: fluid width (%), fixed height (px), and scalable spacing (em/rem).

💬 Usage Tips

  • Set a root font-sizehtml { font-size: 16px; } or 100% makes rem math predictable.
  • Use rem for typography — Users who zoom the page get properly scaled text.
  • Pair % with max-widthwidth: 80%; max-width: 600px; prevents overly wide content.
  • 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 + %).

⚠️ 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.

♿ Accessibility

  • Respect user font settingsrem 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.

🖥 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 Chrome Full support
Full support
Mozilla Firefox Full support
Full support
Apple Safari Full support
Full support
Microsoft Edge Full support
Full support
Opera Full support
Full support
CSS length units 100% supported

Bottom line: Use rem, %, and viewport units confidently in production.

🎉 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.

💡 Best Practices

✅ Do

  • Use rem for font sizes and global spacing
  • Use % for fluid layout widths
  • Set html { font-size: 100%; } or 16px as a base
  • Combine max-width with percentage widths
  • Test layouts at different viewport sizes

❌ Don’t

  • Use only px for everything on responsive sites
  • Mix units randomly without a system
  • Rely on % when parent has no defined size
  • Nest em font-sizes deeply without planning
  • Forget mobile testing for vh layouts

Key Takeaways

Knowledge Unlocked

Five things to remember about CSS length

Choose the right unit for each job.

5
Core concepts
% 02

Percent

Parent-based.

Fluid
rem 03

rem

Root font.

Type
vh 04

Viewport

vw / vh.

Screen
mix 05

Combine

Right tool.

Layout

❓ Frequently Asked Questions

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.

Practice in the Live Editor

Change px, rem, and % values in the Try It editor and watch elements resize instantly.

Try px example →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful