Inline Formatting: Line Boxes, line-height, vertical-align and text-box-trim
How lines of text are built: content areas, leading and inline boxes, line boxes and the strut, choosing line-height, vertical-align values, the gap under images, inline-block baselines, inline padding, and trimming space around text with text-box.
kvi5hnuCode Sprout team member11 min readOn this page
Inside a paragraph, the browser lays text out in an inline formatting context: it breaks the text into lines, gives each line a height, and aligns everything in it on a baseline. Most of the time this is invisible. It becomes visible when a line is taller than its neighbors, an icon sits a little too high, an image has a gap underneath, or a heading's top doesn't line up with the box next to it. This article explains how a line is built, and the properties that control it.
How a line is built

Every piece of inline content, whether a <span> or plain text, which forms an anonymous inline box, contributes boxes to its line:
- Content area. Its height is based on the font size; for most fonts it's a bit taller than
font-size, from the font's ascent and descent. - Leading. The difference between
line-heightand the content area's height. Half goes above the content area and half below. It's roughlyline-height − font-size, but usually a little less, because the content area is taller than the font size: a 16px system font has an 18px content area in Chrome, so withline-height: 24pxthere's 3px above and 3px below. The leading is negative ifline-heightis smaller than the content area. - Inline box. Content area plus leading: exactly as tall as the element's
line-height. - Line box. The inline boxes are aligned on the baseline, and the line box stretches from the top of the highest inline box to the bottom of the lowest.
Lines stack directly on top of each other, so the line boxes' heights set the line spacing. A taller inline box anywhere in a line, such as a larger font or a different line-height, makes that one line taller, so the spacing becomes uneven:
body { font: 1rem/1.5 Georgia, serif; margin: 1rem; max-width: 20rem; }
p { margin: 0 0 1rem; background: repeating-linear-gradient(#fff 0 1.5rem, #f4efe6 1.5rem 3rem); }
big { font-size: 1.6em; color: #8a3b12; }
.fixed big { line-height: 0; } /* no inline box height: the line keeps its height */
In the second paragraph, the larger text gets line-height: 0, so its inline box doesn't push the line apart, and the spacing stays even. Even line-height: 1 isn't enough here: the larger box is aligned on the baseline, so it still reaches above the strut and makes the line taller.
The strut
Every line box also starts with an invisible, zero-width inline box called the strut, with the block's own font and line-height. That's why a line containing only a small <span> is still as tall as the paragraph's normal lines, and it explains the gap under images, below.
line-height
line-height sets the height of inline boxes, and so the spacing between lines:
| Value | Meaning |
|---|---|
normal (initial) | chosen from the font's metrics, usually around 1.1 to 1.3 |
a number, such as 1.5 | 1.5 × the element's own font size, recomputed for each descendant |
a length, such as 24px or 1.5em | a fixed height, inherited as that length |
a percentage, such as 150% | of the element's font size, inherited as the resulting length |
Use unitless numbers. A length or percentage computes once and is inherited as a fixed length, so a larger heading inside gets the small text's line height, and its lines overlap (article 16). A number is inherited as the number, and each element multiplies it by its own font size.
Typical values:
body { line-height: 1.5; } /* body text: 1.4 to 1.6 */
h1, h2, h3 { line-height: 1.15; } /* large text needs less */
.button, .tag { line-height: 1.2; } /* single-line UI text */
WCAG's text spacing rule (1.4.12) asks that pages keep working when users set line height to 1.5 times the font size, so don't depend on tight line heights for layouts to fit. The lh and rlh units (article 24) are lengths equal to the element's or the root's line height, handy for spacing in whole lines: margin-block: 1lh.
On a block, line-height sets the minimum line height for its lines, through the strut. The block itself has no "line height" of its own: its height is the sum of its line boxes.
vertical-align
vertical-align moves an inline-level box up or down within its line. It applies to inline boxes, inline-blocks, images and table cells, not to blocks: it can't center a block in its parent (flexbox and grid do that, articles 63 and 70).
| Value | Aligns the element's... |
|---|---|
baseline (initial) | baseline with the parent's baseline |
middle | vertical midpoint with the parent's baseline plus half the parent's x-height |
text-top, text-bottom | top or bottom with the top or bottom of the parent's content area |
top, bottom | top or bottom of its inline box with the top or bottom of the line box |
sub, super | baseline lowered or raised, as for subscripts and superscripts; the font size doesn't change |
a length, such as 0.2em or -2px | baseline raised (positive) or lowered (negative) by that amount |
| a percentage | raised or lowered by a percentage of its own line-height |
- Moving a box can make the line box taller, since the line has to contain it.
- For icons next to text,
vertical-align: middleis close, but depends on the font's x-height. A small negative length such as-0.125emis often more precise, or use an inline flex container withalign-items: center(article 63). - In table cells,
vertical-alignmeans something else:top,middleandbottomalign the cell's whole content (article 74).

The heading in the bottom row shows the inheritance rule from the line-height section. 20px is inherited as 20px, while 1.4 is multiplied again by the heading's own 26px.
<sup> and <sub> use vertical-align: super or sub plus a smaller font size, which can make their lines taller. A common fix keeps their inline box from growing the line:
sup, sub { line-height: 0; } /* the inline box no longer pushes the line apart */
The gap under images
An image in a <div> usually leaves a few pixels of space below it:
.thumb { border: 2px solid #8a3b12; } /* shows a gap between the image and the bottom border */
Images are inline, and by default they sit on the baseline. Below the baseline, the line keeps room for descenders, the tails of letters like g and p, taken from the strut. So the line box is taller than the image. Any of these remove the gap:
| Fix | Effect |
|---|---|
img { display: block; } | the image isn't inline, so there's no line box |
img { vertical-align: middle; } or bottom | the image no longer sits on the baseline |
| a flex or grid container | items are blocks, not inline content |
Most resets include img, svg, video { display: block; } for this reason.
Inline-block and its baseline
display: inline-block makes a box that's laid out inline, like an image, but formats its inside like a block (article 46). It sits on the baseline using:
- the baseline of its last line of text;
- or, if it has no lines, or its
overflowisn'tvisible, its bottom margin edge.
That second rule is why an inline-block with overflow: hidden suddenly jumps up, with its bottom on the text baseline. Fix it with vertical-align: top or middle. In flex and grid layouts, the baseline-source property can choose the first or last baseline of items; it's in Chrome and Firefox, not Safari.

The 7px gap is the room the strut keeps for descenders at this font size. The raised second box is the jump described above.
Padding, borders and margins on inline boxes
As covered in articles 36 and 39:
- Horizontal padding, borders and margins push the text before and after the element.
- Vertical padding and borders are painted, but don't change the inline box's height, so they overlap the lines above and below. Vertical margins have no effect.
- When the element wraps,
box-decoration-break: clonerepeats padding and borders on each fragment.
To give an inline element real vertical space, use display: inline-block, or increase line-height.
Trimming the space around text: text-box
Even with line-height: 1, text has space above the capital letters and below the baseline, from the font's ascent and descent. That's why a heading in a box looks like it has more padding on top than you set, and why text in a button never looks exactly centered.
text-box-trim removes that space from the first and last lines of a block, and text-box-edge chooses where to cut:
h1, .button {
text-box: trim-both cap alphabetic; /* shorthand: trim, then edges */
}
| Property | Values |
|---|---|
text-box-trim | none (initial), trim-start (first line's top), trim-end (last line's bottom), trim-both |
text-box-edge | top edge: text (the font's ascent), cap (cap height) or ex (x-height); bottom edge: text or alphabetic (the baseline) |
cap alphabetic cuts exactly at the top of capital letters and at the baseline, so padding is measured from the visible letters:
body { font-family: system-ui, sans-serif; margin: 1rem; }
h2 { font-size: 2rem; line-height: 1.2; margin: 0 0 0.75rem; padding: 0.5rem; background: #f4efe6; border-left: 4px solid #8a3b12; }
button { font: inherit; font-size: 1rem; padding: 0.6rem 1rem; border: 0; border-radius: 6px; background: #2f7d4f; color: white; }
.trim { text-box: trim-both cap alphabetic; }
Trimming applies to the first and last line boxes of the block, so multi-line text keeps its normal spacing between lines. The text-box properties are Baseline since August 2026, so you can use them now: in an older browser, the text just keeps its usual space.
What's current
text-box,text-box-trimandtext-box-edgeare in all browsers since August 2026 (Chrome 133, Safari 18.2, Firefox 154).baseline-sourceis in Chrome and Firefox only.initial-letterfor drop caps (article 93) is in Chrome and, prefixed, Safari, not Firefox.- The
lhandrlhunits are Baseline.
Common questions
Why is one line in my paragraph taller than the others?
Something in it has a taller inline box: a larger font size, an inline image, a <sup>, or a raised vertical-align. For text such as <sup>, give it a smaller line-height; 0 stops it from affecting the line at all. That doesn't help an inline image, whose margin box always counts: make it smaller, display: block, or align it with vertical-align: middle.
Why is there a gap under my image?
It's inline and sits on the baseline, with space for descenders below. Use display: block, or vertical-align: middle.
Why doesn't vertical-align: middle center my div?
vertical-align only works on inline-level boxes and table cells. Use flexbox or grid with align-items: center.
Why are my lines overlapping after I set a big font size?
A line-height with units was inherited as a fixed length from a parent with a smaller font. Use a unitless number.
Why doesn't my heading look aligned with the image next to it?
The heading's first line has space above the capital letters. text-box: trim-start cap alphabetic removes it, so the top of the capitals lines up with the image's top.
Summary
- Each inline box is its content area plus half-leading above and below, exactly
line-heighttall. A line box spans from its highest to its lowest inline box, so one tall box makes its line taller. - Each line starts with a strut, with the block's font and line height.
- Use unitless
line-height: about 1.5 for body text, less for headings. vertical-alignmoves inline-level boxes within the line:baseline,middle,top,bottom,sub,superor a length. It doesn't apply to blocks.- Images leave a gap below because they sit on the baseline.
display: blockorvertical-align: middleremoves it. - An
inline-blocksits on its last line's baseline, or its bottom edge if it has no lines or clips overflow. text-box: trim-both cap alphabetictrims the space above capitals and below the baseline, for tighter, more precise alignment.
Practice questions
With font-size: 20px and line-height: 1.4, how tall is each inline box, and how much leading goes above the text?
Answer
28px tall. The leading is 28px minus the content area's height. That's at most 28 − 20 = 8px, about 4px above and 4px below, and a little less in practice, because most fonts' content area is a bit taller than the font size.
A paragraph with line-height: 1.5 has a <sup> that makes its line taller than the others. Write a fix.
Answer
sup { line-height: 0; }
Its inline box no longer extends the line box, so the spacing stays even.
An avatar image inside a bordered <div> has 4px of empty space below it. Why, and what are two fixes?
Answer
The image is inline and sits on the baseline, and the line keeps room below the baseline for descenders. Use img { display: block; } or img { vertical-align: middle; }.
Write CSS so a button's padding is measured from the top of its capital letters and its baseline.
Answer
.button { text-box: trim-both cap alphabetic; }
Previous: Normal Flow, the Containing Block and Block Formatting Contexts
Next: Hiding and Showing: display: none, visibility, display: contents and content-visibility

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.