Skip to content
Tutorials Center
My learning

Topics

Lesson 45 of 139 ยท Text and Fonts

Modern Text Features: text-box-trim and initial-letter

Use newer CSS text features: trim extra space with text-box, make drop caps with initial-letter, and combine them with text-wrap and hanging punctuation.

For years, CSS typography had a few stubborn gaps. Text boxes always carried invisible extra space above and below the letters, so aligning a heading precisely with an image or a button took guesswork. Drop caps needed floats, fixed heights and magic numbers. Balanced line breaks were impossible.

Newer CSS closes these gaps with small, focused properties: text-box-trim and text-box-edge to trim the invisible space, initial-letter for real drop caps, and text-wrap for nicer line breaks. This lesson shows each one, how to use it safely when browser support is uneven, and where it fits in a typographic design. For line-length control see Wrapping, Overflow and Hyphens, and for spacing basics Line Height.

Where the extra space comes from

Every line of text sits in a line box whose height comes from line-height. Fonts also define how much room they reserve above capital letters (the ascent) and below the baseline (the descent). A font with generous metrics, set at line-height: 1.5, leaves a visible gap above the tallest letters and below the baseline. This space is called half-leading plus font metrics, and you cannot see it, but you feel it:

  • A heading with a 0 margin still looks too far from the box above it.
  • A button label never looks vertically centered, even with equal padding top and bottom.
  • Aligning an icon to the cap height of a word needs a hand-tuned margin-top: -0.12em.

text-box-trim removes that extra space in a principled way: it cuts the text box tight to the glyphs you care about.

text-box-trim and text-box-edge

Two properties work together:

  • text-box-trim chooses which sides to trim: none (default), trim-start, trim-end, trim-both. The start is the first line's top; the end is the last line's bottom.
  • text-box-edge chooses where the edge goes. The top edge can be text (the font's ascent), cap (the height of capital letters) or ex (the height of lowercase x). The bottom edge can be text or alphabetic (the baseline). A single value is used for both sides where it makes sense, and a pair such as cap alphabetic sets them separately.
CSS
h1 {
  text-box-trim: trim-both;
  text-box-edge: cap alphabetic;   /* top at cap height, bottom at the baseline */
}

The shorthand is text-box: trim-both cap alphabetic;.

With cap alphabetic, the box of a heading runs exactly from the top of the capital letters to the baseline. Padding, margins and gaps then measure from the visible letters, so a card with padding: 1rem really looks like 1rem of space on every side.

Optical padding with text-box

Compare the two yellow cards. In the left one the word floats a little high or low; in the right one the space above the capital E and below the baseline is the same as the side padding (as long as your browser supports the property). The @supports rule makes the page report which case you are seeing.

Buttons and badges

The same trick centers labels optically:

CSS
.button {
  padding: 0.75rem 1.25rem;
  line-height: 1.5;
}
.button > span {
  display: block;
  text-box: trim-both cap alphabetic;
}

Because the trimmed box hugs capitals and the baseline, equal padding looks equal. Be aware that lowercase-only labels with descenders (g, y, p) hang below the baseline, so decide whether cap alphabetic or ex alphabetic suits your text.

Notes on text-box-trim

  • It applies to block containers, not to inline elements. Put it on a heading, paragraph, div or a display: block span.
  • It trims only the first and last line. Lines in the middle keep their normal spacing.
  • It does not change line-height or margin: those still exist, and the trimming happens inside them.
  • The edge keywords depend on the font's metrics, so test with your real font.
  • Support is newer than most features here (Chromium-based browsers and Safari have it; check your target browsers). Treat it as a refinement: the page must look acceptable without it, and @supports (text-box-trim: trim-both) lets you adjust margins only when trimming is active.
CSS
h2 { margin-block: 1.2em 0.4em; }   /* works everywhere */

@supports (text-box-trim: trim-both) {
  h2 {
    text-box: trim-both cap alphabetic;
    margin-block: 1.5rem 0.75rem;   /* spacing now measures from the letters */
  }
}

Drop caps with initial-letter

A drop cap is a large first letter that sinks into the first lines of a paragraph. Before initial-letter, you faked it with float: left, a big font size and fiddly margins that broke whenever the font changed. initial-letter does the geometry for you. It applies to ::first-letter (or to the first letter of a block) and takes up to two numbers:

CSS
p::first-letter {
  initial-letter: 3;       /* the letter is 3 lines tall and sinks 3 lines */
}
  • The first number is the size, in lines of text. 3 makes the letter as tall as three lines.
  • The second number is the sink: how many lines it drops. With initial-letter: 3 2, the letter is 3 lines tall but sinks only 2, so it rises above the first line (a "raised cap").
  • initial-letter: normal is the default, an ordinary letter.

The browser sizes the font and aligns the letter to the text baselines for you, so the top of the cap lines up with the top of the first line and the bottom sits on the baseline of the third.

Drop cap and raised cap

The @supports not (initial-letter: 3) block is a classic progressive enhancement: browsers without initial-letter get the old float approach, and browsers with it get exact alignment. See Feature Queries for the pattern.

A few reminders when you use drop caps:

  • Style ::first-letter with color, font-weight and margins. Padding and borders work too. Keep the styling modest: its box is part of the layout calculation.
  • Punctuation that precedes the letter (such as an opening quotation mark) is included in ::first-letter, so it becomes part of the initial.
  • Screen readers read the paragraph normally, because ::first-letter is a presentation effect and does not change the DOM.
  • Use drop caps sparingly: for the opening paragraph of an article, not for every paragraph.
  • Support has been uneven across engines for a long time, so always give a fallback that is acceptable (even a plain letter).

Balanced and pretty line breaks

The other big improvement is text-wrap, covered in detail in Wrapping, Overflow and Hyphens. A quick recap with a modern headline system:

CSS
h1, h2, h3, blockquote { text-wrap: balance; }  /* even line lengths for short text */
p, li                   { text-wrap: pretty; }  /* avoid orphans in body text */

balance is for short blocks (the browser balances only a few lines), pretty favors good breaks in paragraphs, and stable keeps earlier lines from reflowing while someone edits text in an editable region.

Hanging punctuation and quotes

Two smaller refinements round out a polished text block:

  • hanging-punctuation: first lets an opening quote mark hang into the margin, so the letters of quoted lines align with the left edge of the text. Support is limited to Safari at the time of writing, which is fine: it is a pure enhancement.
  • quotes and the ::before/::after content open-quote and close-quote create language-appropriate quotation marks for <q> and <blockquote>.
CSS
blockquote { hanging-punctuation: first; text-wrap: balance; }

A compact modern text recipe

Putting the pieces together as layers, each one safe where unsupported:

CSS
/* 1. Works everywhere */
h1, h2, h3 { line-height: 1.2; margin-block: 1.5em 0.5em; }

/* 2. Better line breaks */
h1, h2, h3 { text-wrap: balance; }
p          { text-wrap: pretty; }

/* 3. Optical spacing where available */
@supports (text-box-trim: trim-both) {
  h1, h2, h3 { text-box: trim-both cap alphabetic; }
}

/* 4. Drop cap for the opening paragraph only */
.article > p:first-of-type::first-letter { initial-letter: 3; margin-right: 0.4rem; }

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does text-box-trim trim?

    The property cuts the leading and font-metric space at the start and end of the text box, down to the edge chosen by text-box-edge.
  2. 2. What does text-box: trim-both cap alphabetic produce?

    cap sets the top edge at cap height and alphabetic sets the bottom edge at the baseline, so spacing is measured from the visible letters.
  3. 3. What does initial-letter: 3 2 mean?

    The first number is the size in lines and the second is the sink depth in lines. A sink smaller than the size makes a raised cap.
  4. 4. How should you handle browsers that lack initial-letter?

    Feature queries let you provide an older technique only where the new property is missing, which is classic progressive enhancement.
  5. 5. Which is true of text-box-trim?

    It is meant for block containers and affects only the start and end of the text, leaving the spacing between inner lines alone.

Summary

  • Line boxes carry invisible space from line-height and font metrics; text-box-trim with text-box-edge (or the text-box shorthand) removes it for precise, optical spacing.
  • cap alphabetic hugs capital letters and the baseline; use it on block containers, and wrap it in @supports because support is recent.
  • initial-letter: N (and N M for raised caps) creates properly aligned drop caps on ::first-letter; provide a float fallback.
  • text-wrap: balance and pretty improve line breaks, and hanging-punctuation tidies quotes where supported.
  • Layer these enhancements so the page looks good without them.

Next lesson: Text Shadows, where you add depth and glow to text.