Course contentsShow
← Web Development
Lesson 158 of 2,9823. CSS FundamentalsFree lesson

The ch, ex, and cap Units

Explore typography-based units: ch (character width), ex (x-height), and cap (capital height).

The ch, ex, and cap Units

You'll learn three typographic CSS units that relate to the actual dimensions of font characters.

While units like px and rem are useful, CSS also offers units tied directly to the typography itself: ch, ex, and cap. These are particularly valuable when you want elements to scale with the visual characteristics of your chosen font.

Understanding Typography-Based Units

The ch Unit (Character Width)

The ch unit represents the width of the "0" (zero) character in the current font. Think of it as measuring horizontal space in "character chunks." This is incredibly useful for setting readable line lengths—for example, max-width: 60ch creates a container roughly 60 characters wide, which is considered optimal for readability.

The ex Unit (x-Height)

The ex unit equals the height of the lowercase "x" in the current font. This measurement captures the font's visual center—the size of its lowercase letters without ascenders (like on "h") or descenders (like on "g"). It's useful for fine-tuning spacing that should feel proportional to the font's lowercase letters.

The cap Unit (Capital Height)

The cap unit measures the height of capital letters in the current font. This is the distance from the baseline to the top of uppercase letters like "H" or "M." It's helpful when aligning elements with capital letters or creating decorative effects that should match uppercase dimensions.

When to Use Them

Use ch for horizontal measurements related to text width, ex for vertical spacing tied to lowercase letters, and cap for elements that should align with uppercase height. These units automatically adjust if you change fonts, maintaining proportional relationships.

Key Takeaway: Typography-based units (ch, ex, cap) let you size and space elements relative to actual font characteristics, creating designs that adapt intelligently to different typefaces.