Sub topic 8
Baseline alignment and vertical-align on inline-block
This chapter assumes you know the CSS box model, how to write a rule with a selector and properties, and how inline-block boxes sit in a line of text. It covers why inline-block elements sometimes look pushed up or down, and how vertical-align fixes it.
What the baseline is
Inside a line of text, every glyph sits on an invisible line called the baseline. Letters like "p" and "g" hang below it. Inline-level boxes in a line are lined up on this baseline by default, because the initial value of vertical-align is baseline.
For an inline-block, the browser has to pick which point of the box counts as its baseline:
- If the box contains in-flow line boxes (normal text), its baseline is the baseline of the last line of text inside it.
- If the box has no in-flow line boxes, or if
overflowis anything other thanvisible, its baseline is the bottom margin edge.
The second rule is the source of most surprises. An empty box, or one with overflow: hidden, sits with its bottom edge on the line's baseline, not its middle.
Seeing the misalignment
Two inline-block boxes with different amounts of text do not line up at the top. They line up on the last line of text in each.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Baseline demo</title>
<style>
.card {
display: inline-block;
width: 8rem;
padding: 0.5rem;
border: 1px solid #555;
}
</style>
</head>
<body>
<div class="card">Short</div>
<div class="card">A longer card with text that wraps onto several lines.</div>
</body>
</html>The last line of "Short" is aligned with the last line of the second card. The short card therefore appears to hang low, next to the bottom of the tall one. Nothing is broken. This is the default baseline rule at work.
Changing the alignment with vertical-align
vertical-align applies to inline-level boxes, including inline and inline-block. It does not apply to block boxes. Common values:
| Value | Effect |
|---|---|
| `baseline` | Default. Aligns the box's baseline with the parent's baseline. |
| `top` | Aligns the top of the box with the top of the line box. |
| `middle` | Centers the box's midpoint on the parent's baseline plus half the x-height. |
| `bottom` | Aligns the bottom of the box with the bottom of the line box. |
To make the cards line up along their tops, set vertical-align: top:
.card {
display: inline-block;
width: 8rem;
padding: 0.5rem;
border: 1px solid #555;
vertical-align: top;
}The gap under images and empty boxes
The same rule explains the small gap that appears under an image inside a block. An img is inline by default, so it sits on the baseline, and the space below the baseline is reserved for letters like "p" and "g". That reserved space shows up as a few pixels under the image.
img {
vertical-align: middle;
}Setting vertical-align to anything other than baseline on the image removes the gap. Setting display: block on the image works too, because a block box no longer takes part in the line's baseline alignment.
A quick checklist
- Items in a row look uneven? Check whether their last lines of text differ.
- Try
vertical-align: topon every item in the row, not only one. - Empty or
overflow: hiddenboxes sit on their bottom edge. Addvertical-align: topormiddleif that looks wrong. - If you need full control over alignment in a row,
display: flexwithalign-itemsis more predictable than baseline rules.
Exercise
Two inline-block boxes sit side by side. The first has one line of text and the second has four lines. The first box appears to hang near the bottom of the second. Why does this happen, and which single declaration, added to both boxes, aligns their tops?
Show answer
Inline-block boxes with text are aligned on the baseline of their last line of text, and vertical-align defaults to baseline. The last lines match, so the short box lines up with the bottom of the tall one. Adding vertical-align: top to both boxes aligns their tops.
Exercise
An inline-block box has overflow: hidden and contains text. Where is its baseline, and how does this differ from the same box with overflow: visible?
Show answer
With overflow: hidden, its baseline is the bottom margin edge of the box. With overflow: visible, its baseline is the baseline of its last line of text, so the box sits higher or lower relative to its neighbors.