What Units Can Be Used for Height and Width in CSS?

In CSS, defining the height and width of elements is essential to controlling layout, spacing, and visual hierarchy. But with so many different units available, choosing the right one can be confusing.

In this blog post, weโ€™ll explore the various units you can use to set height and width in CSS, including their behavior, use cases, and tips for choosing the right one.


๐Ÿ“ Types of Units in CSS

CSS offers two broad categories of units for defining size:

  1. Absolute units โ€“ Fixed measurements that donโ€™t change regardless of screen size.
  2. Relative units โ€“ Flexible measurements based on other elements or the viewport.

Letโ€™s dive into each.


โœ… 1. Absolute Units

Absolute units have a fixed value, making them ideal when you need precise control โ€” but they donโ€™t respond to screen size changes.

UnitDescription
pxPixels (most commonly used)
ptPoints (1/72 of an inch)
cmCentimeters
mmMillimeters
inInches

Example:

.box {
  width: 300px;
  height: 150px;
}

When to use: For fixed-size elements in print styles or when precision matters more than responsiveness.


๐ŸŒ 2. Relative Units

Relative units are dynamic โ€” they scale based on context like parent size, font size, or the browserโ€™s viewport.

๐Ÿ”ธ % (Percentage)

  • Based on the size of the parent element.
  • Requires the parent to have an explicit height for height: % to work.
.container {
  width: 80%;  /* 80% of the parentโ€™s width */
  height: 50%; /* 50% of the parentโ€™s height */
}

๐Ÿ”ธ em and rem

  • em: Relative to the font size of the parent.
  • rem: Relative to the root (html) font size.
.box {
  width: 20em;
  height: 10rem;
}

Use case: For sizing elements in proportion to text (especially buttons or inputs).


๐Ÿ”ธ vw, vh, vmin, vmax (Viewport Units)

UnitBased on
vw1% of the viewport width
vh1% of the viewport height
vmin1% of the smaller of width/height
vmax1% of the larger of width/height
.hero {
  height: 100vh; /* Full screen height */
  width: 100vw;  /* Full screen width */
}

Great for: Full-screen sections, responsive typography, and layouts that adapt to any screen size.


๐Ÿ”ธ ch and ex

  • ch: Relative to the width of the “0” (zero) character.
  • ex: Relative to the height of the lowercase “x”.

These are rarely used for height/width but can be useful for typographic precision.


๐Ÿง  Choosing the Right Unit

GoalRecommended Units
Fixed dimensionspx, pt
Responsive layouts%, vw, vh, vmin
Typography-based sizingem, rem
Full-screen sections100vh, 100vw
Print stylescm, mm, in

๐Ÿ”„ Bonus: Combining Units with calc()

CSS lets you combine units using the calc() function for more control.

.container {
  width: calc(100% - 50px);
}

This mixes a percentage with pixels โ€” perfect for layouts with fixed sidebars or padding.


โœ… Summary

CSS offers a variety of units to set height and width, each with its own advantages. Understanding how and when to use them empowers you to create designs that are both precise and responsive.

Common units include:

  • ๐Ÿ“ฆ px, cm, in (absolute)
  • ๐ŸŒ %, em, rem, vw, vh (relative)
  • ๐Ÿงฉ calc() to mix and match units
Sharing Is Caring:

Leave a Comment