Skip to content
beginnerPhase 31 · CSS

CSS Units

Compare px, em, rem, vw, vh, %, and other units for different use cases.

30m
0 problems
Topic Progress0%

Absolute Units

Absolute Units

Fixed-size units that don't change based on context.

px (Pixels)

Most common absolute unit:

.container {
  width: 960px;
  padding: 16px;
  font-size: 14px;
  border: 1px solid black;
}

Other Absolute Units

/* Physical units (rarely used) */
.width { width: 10cm; }   /* Centimeters */
.width { width: 100mm; }  /* Millimeters */
.width { width: 5in; }    /* Inches */
.width { width: 25pc; }   /* Picas */
.width { width: 72pt; }   /* Points */

When to Use Absolute Units

  • Borders: border: 1px solid
  • Shadows: box-shadow: 0 2px 4px
  • Small, precise adjustments
  • When you need exact pixel control

Best Practice

/* Avoid */
.container {
  width: 960px; /* Fixed, not responsive */
}

/* Better */
.container {
  max-width: 960px;
  width: 100%;
}

Relative Units

Relative Units

Units that scale based on context.

em

Relative to parent font size:

.parent {
  font-size: 16px;
}

.child {
  font-size: 1.5em; /* 24px */
  padding: 1em;     /* 16px */
  margin: 0.5em;    /* 8px */
}

rem

Relative to root (html) font size:

html {
  font-size: 16px;
}

h1 {
  font-size: 2rem;  /* 32px */
}

p {
  font-size: 1rem;  /* 16px */
}

.container {
  padding: 1.5rem; /* 24px */
}

% (Percentage)

Relative to parent element:

.parent {
  width: 800px;
}

.child {
  width: 50%;   /* 400px */
  padding: 10%; /* 80px */
}

ex and ch

/* ex - height of lowercase 'x' */
.height { height: 2ex; }

/* ch - width of '0' character */
.width { width: 20ch; }

Unit Recommendations

Property Recommended Unit
Font size rem
Line height unitless or em
Padding/Margin rem or em
Width %, vw, rem
Border px
Shadow px

Viewport Units

Viewport Units

Relative to the browser viewport.

Basic Viewport Units

.hero {
  width: 100vw;    /* Full viewport width */
  height: 100vh;   /* Full viewport height */
}

.sidebar {
  width: 25vw;     /* 25% of viewport width */
}

.section {
  min-height: 50vh; /* At least half viewport */
}

Dynamic Viewport Units (Modern)

/* Accounts for mobile browser chrome */
.hero {
  height: 100dvh;  /* Dynamic viewport height */
  min-height: 100svh; /* Smallest viewport height */
  max-height: 100lvh; /* Largest viewport height */
}

Practical Uses

/* Full-screen hero */
.hero {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Sticky footer */
.content {
  min-height: calc(100vh - 100px);
}

/* Fluid typography */
h1 {
  font-size: clamp(2rem, 5vw, 4rem);
}

/* Responsive spacing */
.section {
  padding: 5vw;
}

Viewport Units vs Percentage

/* Percentage: relative to parent */
.parent { width: 800px; }
.child { width: 50%; } /* 400px */

/* Viewport: relative to browser */
.child { width: 50vw; } /* 50% of viewport */

Common Patterns

/* Full-screen section */
.full-screen {
  height: 100vh;
  display: grid;
  place-items: center;
}

/* Modal overlay */
.modal-overlay {
  position: fixed;
  width: 100vw;
  height: 100vh;
}

/* Responsive container */
.container {
  width: min(90vw, 1200px);
  margin: 0 auto;
}

Practice Problems

0/3solved
Build CSS Units Component

Create a reusable React component implementing CSS Units. Include proper state management and accessibility.

Solution
// Production-ready component with:
// - Proper TypeScript types
// - Accessibility (ARIA)
// - Error boundaries
// - Loading states
// - Memoization where needed
CSS Units Testing

Write unit and integration tests for CSS Units using React Testing Library.

Solution
// Test coverage:
// 1. Rendering tests
// 2. Interaction tests
// 3. Edge case tests
// 4. Accessibility tests
CSS Units Performance

Optimize CSS Units for performance. Consider memoization, code splitting, and bundle size.

Solution
// Optimization techniques:
// 1. React.memo / useMemo / useCallback
// 2. Code splitting with lazy()
// 3. Virtual scrolling for lists
// 4. Image lazy loading
// 5. Bundle analysis

Quiz

1. What is the difference between em and rem?

Question 1 options

2. What does 100vh represent?

Question 2 options

3. Which unit is recommended for font sizes?

Question 3 options

4. What is 1rem by default?

Question 4 options

5. What does dvh stand for?

Question 5 options

Flashcards

Question

What is the difference between px and rem?

Answer

px is absolute (fixed size). rem is relative to the root font size (1rem = 16px by default).

Question

When should you use viewport units (vw, vh)?

Answer

For full-screen layouts, responsive spacing, and fluid typography that scales with the viewport.

Question

Why avoid using em for padding/margin?

Answer

Because em is relative to the element's own font size, which can cause unexpected scaling.

Question

What does min(90vw, 1200px) do?

Answer

Sets width to whichever is smaller: 90% of viewport width or 1200px. Creates a responsive max-width.

Question

What is the recommended unit for borders?

Answer

px (pixels), because borders need to be precise and consistent regardless of context.

Revision Notes

Key Takeaways

  • 1.px is absolute, rem/em are relative
  • 2.rem is relative to root, em is relative to parent
  • 3.Viewport units (vw, vh) are relative to browser window
  • 4.Use rem for fonts to avoid compounding issues
  • 5.px is best for borders and shadows

Interview Tips

  • Explain the difference between em and rem
  • Know when to use each unit type
  • Understand viewport units and their use cases
  • Be able to create fluid typography with clamp()

Cheat Sheet

CSS Units Cheat Sheet

Absolute Units

  • px - Pixels (most common)
  • pt - Points (1/72 inch)
  • cm, mm, in - Physical units

Relative Units

  • em - Relative to parent font size
  • rem - Relative to root font size
  • % - Relative to parent element
  • ch - Width of '0' character
  • ex - Height of lowercase 'x'

Viewport Units

  • vw - Viewport width
  • vh - Viewport height
  • vmin - Smaller of vw/vh
  • vmax - Larger of vw/vh
  • dvh - Dynamic viewport height

Unit Recommendations

Property Unit
Font size rem
Padding/Margin rem
Width %, vw, rem
Border px
Shadows px