Color scheme

Advanced Text Properties and Font-Relative Units

For years, common typographic problems had no clean CSS solution. Balancing a headline so it did not leave one orphaned word on the last line meant manual line breaks. Drop caps meant floats and careful margin math. Truncating a card description to a fixed number of lines meant JavaScript that measured and cut. CSS now solves all three natively, and pairs them with a set of font-relative units that let you size text and space by the dimensions of the font itself rather than by guessed pixel values. This page covers the modern text properties and the units that make typographic layout predictable.

Support for the features on this page is genuinely uneven, more so than most CSS you have used. Each section below carries its own current support note rather than a single blanket statement, because some of these properties are solid everywhere while others are still prefixed or partial. Read the note with each feature and plan a fallback for the ones that are not yet universal.

Balancing and Wrapping Text

The text-wrap property changes how the browser decides where to break lines. The default value, wrap, breaks greedily: it fills each line as full as possible before moving to the next, which often leaves a short, lonely last line. Two newer values fix specific problems with that behavior.

text-wrap: balance

The balance value distributes text as evenly as possible across all its lines, so a heading does not end with a single orphaned word. It is designed for short blocks of text such as headings, captions, and pull quotes. Browsers limit balancing to a small number of lines (commonly six or fewer) because the calculation is expensive, which is why you should reserve it for short text and never apply it to long body copy.

Current support is good across Chrome, Edge, Firefox, and Safari, and the property degrades gracefully: a browser that does not understand balance simply falls back to normal wrapping. Confirm details at Can I Use: text-wrap: balance.


        h1, h2, h3 {
            text-wrap: balance;
        }
    

text-wrap: pretty

The pretty value targets a different problem: it focuses on the last few lines of a longer block, preventing orphans and improving the rag without rebalancing the whole paragraph. Because it only considers the tail of the text, it is far cheaper than balance and is appropriate for body copy where balance would be too costly. Use pretty for paragraphs and balance for headings.

Support for pretty is more recent and less complete than balance, with some browsers implementing only a subset of its intended behavior. Check Can I Use: text-wrap: pretty before relying on it, and treat it as a progressive enhancement.


        p {
            text-wrap: pretty;
        }
    

Drop Caps with initial-letter

A drop cap is the large initial letter that sinks into the opening lines of a paragraph, a typographic device borrowed from print. Achieving it historically required floating a span and hand-tuning its font size, line height, and margins until it lined up, and the result was fragile across font changes. The initial-letter property does it declaratively, applied to the ::first-letter pseudo-element.

The property takes one or two numbers. The first sets how many lines tall the letter should be, which also determines its size. The optional second number sets how many lines it sinks below the first line; omit it and the letter sinks by its full height. A value of 3 makes a letter three lines tall that sinks three lines deep.


        p::first-letter {
            -webkit-initial-letter: 3;
            initial-letter: 3;
        }
    

Support requires care: Safari has shipped initial-letter for years under the -webkit- prefix, Chrome and Edge support the unprefixed property, and Firefox does not support it as of this writing. Always include both the prefixed and unprefixed declarations, and make sure the paragraph still reads correctly when the drop cap does not render, since a missing drop cap simply leaves a normal first letter. See Can I Use: initial-letter for the current picture.

Clamping Text to a Line Count

Truncating text to a fixed number of lines with an ellipsis is the classic card-description problem: you want every card the same height regardless of how much text it holds. The long-standing solution is a small group of -webkit- properties that, despite the vendor prefix, work across every major browser today.


        .card-description {
            display: -webkit-box;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 3;
            overflow: hidden;
        }
    

All four declarations are required. The -webkit-box display and vertical orientation establish the old flexbox context the clamp depends on, -webkit-line-clamp sets the line count, and overflow: hidden applies the ellipsis. A newer unprefixed line-clamp property is being standardized, but the prefixed version remains the reliable choice across browsers. Confirm at Can I Use: line-clamp.

The demo below shows the pattern in action. Drag the slider to set the line count and watch the card description truncate with an ellipsis, holding the card to a predictable height.

Line clamp: 3 lines
Mountain Ridge Trail
A demanding ascent that rewards hikers with sweeping views across three valleys. The trail climbs steadily through pine forest before breaking above the treeline, where exposed granite switchbacks lead to a windswept summit. Bring layers, plenty of water, and start early to beat the afternoon weather that rolls in off the western slopes during summer months.

Font-Relative Units for Measure

A font-relative unit is sized by some dimension of the current font rather than by a fixed pixel value or the root font size. They let you express layout intentions in terms the typography itself defines, which keeps those intentions correct when the font or its size changes.

The ch Unit and Line Length

The ch unit equals the advance width of the 0 (zero) character in the current font. Its most valuable use is controlling measure, the technical term for line length. Typographers consider roughly 45 to 75 characters per line the comfortable range for sustained reading, and setting a max-width in ch expresses that directly: max-width: 65ch targets about 65 zero-widths of text per line.


        article p {
            max-width: 65ch;
        }
    

There is an important caveat in proportional fonts. Because ch is specifically the width of the zero character, and most fonts are proportional (an i is narrower than an m), 65ch does not guarantee exactly 65 characters of arbitrary text per line. It is an excellent approximation for measure, not a precise character count. In a monospace font, where every glyph has the same advance width, ch does correspond exactly to character count, which is why it is reliable for sizing code blocks and terminal-style layouts. The ch unit has universal browser support; see Can I Use: ch unit.

The ex Unit

The ex unit equals the x-height of the current font, the height of a lowercase x. It is used far less often than ch, but it is the right tool when you need to align or size something to the lowercase letterforms rather than the full font size, such as positioning an inline icon to match the visual height of the text beside it. Like ch, it is universally supported.

The demo below sets a paragraph's measure in ch. Drag the slider and watch the line length change, with the comfortable reading range marked so you can see where the text becomes too cramped or too wide.

Measure: 65ch

Good typography is invisible. When the measure is right, the reader's eye moves from the end of one line to the start of the next without effort or thought, and the words simply flow. Set the line too short and the eye jumps constantly, breaking rhythm. Set it too long and the eye loses its place on the return sweep.

Font-Relative Units for Rhythm

Two units size by line height rather than glyph dimensions, which makes them ideal for vertical rhythm: the consistent vertical spacing that makes a column of text feel orderly.

The lh and rlh Units

The lh unit equals the computed line height of the current element, and rlh equals the line height of the root element. Spacing elements by lh ties their margins to the same rhythm as the text itself, so the vertical spacing between blocks lines up with the baseline grid the line height establishes. A margin of 1lh below a paragraph is exactly one line of space, which keeps a stack of text blocks on a consistent rhythm regardless of font size changes.


        p {
            margin-block-end: 1lh;
        }

        h2 {
            margin-block-start: 2rlh;
        }
    

The difference between the two matters when an element's own line height differs from the root. Use lh to space relative to the element's own text, and rlh to space everything against one shared root rhythm regardless of local line height. Both units are supported in current Chrome, Edge, Firefox, and Safari, though they arrived more recently than the glyph units, so check Can I Use: lh unit and rlh unit if you need to support older versions.

Teaching Video Checklist

When you record your teaching video, make sure your teammates come away understanding these concepts:

  • The difference between text-wrap: balance for headings and pretty for body copy, and why balance is limited to short text
  • How initial-letter creates a drop cap, the meaning of its line values, and why both the prefixed and unprefixed declarations are needed
  • The four-declaration line-clamp pattern and what each declaration contributes, demonstrated by truncating a real card description
  • What ch measures and why it controls measure well in proportional fonts but only equals exact character count in monospace
  • How lh and rlh keep a stack of text blocks on a consistent vertical rhythm, and the difference between the two
  • Your own demo on your portfolio: at least one text property and one font-relative unit applied to real content

Open the browser developer tools and measure an element using a font-relative unit so teammates can see, for example, how a max-width in ch maps to an actual rendered line length and how it shifts when the font size changes.

Resources