Course contentsShow
← Web Development
Lesson 619 of 2,98211. Utility-First CSS with TailwindFree lesson

Sizing: Width and Height

Use w-* and h-* utilities including fixed values, percentages, viewport units, and arbitrary values.

Sizing: Width and Height

You'll learn how to control element dimensions in Tailwind CSS using width and height utilities with various unit types.

The Core Idea

Just like you learned m-* for margins and p-* for padding, Tailwind provides w-* and h-* utilities to control how wide and tall elements are. These utilities map to the spacing scale you configured earlier, but also support special sizing units like percentages, viewport dimensions, and custom values when you need them.

Width Utilities

Use w-* classes to set an element's width:

  • Fixed spacing scale: w-4, w-8, w-64 (uses your spacing theme)
  • Fractions: w-1/2, w-1/3, w-full (percentage-based)
  • Viewport units: w-screen (100vw)
  • Auto: w-auto (browser calculates)
  • Arbitrary values: w-[247px], w-[80%], w-[50vw] (exact custom sizes)

Height Utilities

Similarly, h-* classes control height:

  • Fixed spacing scale: h-4, h-12, h-96
  • Fractions: h-1/2, h-full
  • Viewport units: h-screen (100vh)
  • Auto: h-auto
  • Arbitrary values: h-[300px], h-[75vh]

Think of It Like This

Imagine sizing utilities as templates for common dimensions. Most of the time, the pre-defined values keep your design consistent. But when you need something specific—like matching an image's exact pixel dimensions—arbitrary values let you escape the system temporarily without writing custom CSS.

Key Takeaway: Tailwind's w-* and h-* utilities let you size elements using spacing scale values, fractions, viewport units, or arbitrary values in square brackets, keeping your sizing consistent while remaining flexible.