DEV Community

Cover image for Understanding CSS Size Units: px, em, rem, %, and More
Muhammad Essa
Muhammad Essa

Posted on

Understanding CSS Size Units: px, em, rem, %, and More

In CSS, you have a variety of units to control the size of elements, from pixels (px) to percentages (%) to relative units like em and rem. Choosing the right unit for your design can be crucial for building responsive and accessible layouts. This guide covers the most common CSS size units, when to use each one, and how they impact your layout.


1. Pixels (px)

Pixels are one of the most common absolute units in CSS. A px represents one physical pixel on the screen. Since it’s fixed, using px means the element will stay the same size regardless of the user’s settings.

When to Use px:

  • Use px for precise, fixed elements like icons or borders.
  • Avoid px for text, as it can affect accessibility when users adjust browser zoom levels.

Example:

p {
  font-size: 16px;
  width: 200px;
}
Enter fullscreen mode Exit fullscreen mode

2. Percentages (%)

The % unit is relative, meaning it adjusts based on the size of the parent element. This flexibility makes % an essential tool for responsive design, especially for fluid layouts where elements resize based on the viewport.

When to Use %:

  • Use % for layout elements like containers or images that should adjust relative to their parent container.
  • Combine % with media queries to create fluid, responsive designs.

Example:

.container {
  width: 80%; /* 80% of the parent element's width */
}
Enter fullscreen mode Exit fullscreen mode

3. em Units

The em unit is a relative unit based on the font size of its closest parent element. If no parent font size is defined, it defaults to the browser’s base font size (usually 16px).

Key Points:

  • 1em equals the font size of the parent element.

  • 1em is twice the size, and so on.

  • Be cautious with nesting, as em values can multiply when applied to nested elements.

When to Use em:

  • For spacing, padding, or margin adjustments in relation to text size.

  • For font sizes in components that should adjust relative to their parent’s text size.

Example:

.container {
  font-size: 16px;
}

.child {
  padding: 1.5em; /* 1.5 times the font size of .container */
}
Enter fullscreen mode Exit fullscreen mode

4. rem Units

Unlike em, the rem unit is based on the font size of the root element (<html>), which means 1rem is consistent throughout the document (often 16px unless customized). This makes rem a reliable unit for consistent typography and spacing across a webpage.

When to Use rem:

  • For consistent font sizes across components, regardless of nested elements.

  • To achieve a more scalable and maintainable design with a clear reference point.

Example:

html {
  font-size: 16px;
}

.container {
  font-size: 1rem; /* 16px based on the root font size */
}
Enter fullscreen mode Exit fullscreen mode

5. Viewport Units (vw and vh)

Viewport units—vw (viewport width) and vh (viewport height)—are responsive units based on the size of the viewport (browser window). 1vw equals 1% of the viewport width, and 1vh equals 1% of the viewport height.

When to Use vw and vh:

  • For elements that should scale with the browser size, like full-page hero sections.

  • To make responsive typography that adjusts based on screen width.

Example:

.hero {
  height: 100vh; /* Full viewport height */
  font-size: 3vw; /* Scales with viewport width */
}
Enter fullscreen mode Exit fullscreen mode

6. Flexible Length Units (min,max, and clamp)

Newer units like min(), max(), and clamp() are powerful for responsive design, allowing conditional sizing based on the smallest or largest value.

min(a, b): Takes the smallest of two values.
max(a, b): Takes the largest of two values.
clamp(min, preferred, max): Sets a value that adapts within a defined range.

When to Use:

  • For responsive typography that fits within a specific range.
  • For sizing components based on the viewport but with a minimum or maximum limit.

Example:

.title {
  font-size: clamp(1rem, 2vw, 2.5rem); /* Adapts within 1rem to 2.5rem */
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

Each CSS unit has its own strengths and ideal use cases:

  • px for precise, fixed elements.
  • % for fluid, responsive layout components.
  • em for scaling relative to parent elements.
  • rem for consistent scaling across the page.
  • Viewport units for dynamic, viewport-based elements.
  • Flexible units like clamp() for adaptive, conditional styling.

Mastering these CSS units can help you create flexible, accessible, and maintainable layouts. Mix and match them based on your design needs and watch your layout come to life! Happy styling!

Top comments (7)

Collapse
 
wizard798 profile image
Wizard

Very nice explanation, and showing great use cases if where to use which, I prefer to use rem more, first I set root font size to 10 px so my 1rem = 10 px and it gives me easy calculations and less "Zeroes" clutter, and also in media queries, you just need to change font size of root element and it'll directly effect and reduce size wherever used rem.

Collapse
 
ajacobs profile image
Andrew (AJ) Jacobs

I've seen people use html { font-size: 62.5%; } for the same reason, although I'm not sure what the difference would be - if any.

Collapse
 
wizard798 profile image
Wizard

It's same, 10px equivalents to 16px when font size is set to 62.5%, cause diving 10 by 16 is 0.625, and when you want to insert media queries, you just reduce the %value like to 56.25% ( 9px ), 50% ( 8px ) per rem and so on. It ensures better responsiveness wherever used rem. You don't have to go to all things and put media queries there.

I guess this is not that useful when you learn any css frameworks cause they are made in that way that it'll relive all pressure of responsiveness.

Collapse
 
navneet_verma profile image
Navneet Verma

Nice explanation! Good for beginners, easy to understand! Appreciate it.

Collapse
 
magsimuhammadessa profile image
Muhammad Essa

I am happy to hear that

Collapse
 
quiro__13_cf15c309b7fb165 profile image
Quiro_ 13

Soy principiante en estos temas y buscando informacion me esuentro con esta joya, gracias por la explicacion

Collapse
 
magsimuhammadessa profile image
Muhammad Essa

I loved your response ! Thank you 😊
consider sharing this with everyone you think will benefit from it