Sub topic 8
Baseline alignment and vertical-align on inline-block
This chapter assumes you know the box model, you know how display: inline and display: block differ, and you have seen display: inline-block accept width and height. Here you will learn why inline-block boxes of different heights often sit at uneven heights, why a gap appears below some images, and how vertical-align fixes both.
What a baseline is
A baseline is the invisible line that letters sit on. Letters like "p" and "g" have descenders that hang below it. When the browser places several inline-level boxes in one line, it lines them up on their baselines by default. This default is vertical-align: baseline.
Plain text already follows this rule, so you rarely notice it. The rule matters once inline-block boxes join the line, because each box must report where its baseline is. The browser then moves each box up or down until the baselines match.
Where an inline-block gets its baseline
The browser picks the baseline of an inline-block box in one of two ways:
- If the box has at least one line of in-flow content and its
overflowisvisible, the baseline is the baseline of the last line of text inside it. - Otherwise, the baseline is the bottom margin edge of the box. This applies to an empty box, to a box with no in-flow line boxes, and to a box whose
overflowis anything other thanvisible, such ashidden,auto, orscroll.
The first rule means a tall box with several lines of text lines up with its neighbors by its last line. The second rule causes most of the surprises.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Baseline demo</title>
<style>
.box {
display: inline-block;
width: 8rem;
padding: 0.5rem;
border: 2px solid #345;
background: #eef;
}
.clip {
overflow: hidden;
}
</style>
</head>
<body>
<p>
Text
<span class="box">Short</span>
<span class="box">Three<br>lines<br>of text</span>
<span class="box clip">Short, but clipped</span>
</p>
</body>
</html>In this page the word "Text" and the last line of each of the first two boxes share one baseline. The first box is short, so it sits low next to the tall second box. The third box has overflow: hidden, so its baseline is its bottom margin edge. The whole box rises above the text line, and its bottom edge touches the shared baseline instead of its last line of text.
Common symptom: a row of cards at uneven heights
A frequent complaint is that inline-block cards with different amounts of text do not line up at the top. The cause is baseline alignment. Each card's last line of text is pushed onto the same line, so a card with more text starts higher than a card with less.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Uneven cards</title>
<style>
.card {
display: inline-block;
width: 10rem;
padding: 0.5rem;
border: 1px solid #888;
}
</style>
</head>
<body>
<div class="card">One short line.</div>
<div class="card">This card has much more text, so it wraps onto several lines and grows taller.</div>
<div class="card">Another short one.</div>
</body>
</html>In this page the short cards drop down so their last lines match the last line of the tall card. The tops are ragged.
Fixing alignment with vertical-align
vertical-align changes how an inline-level box is placed in its line. The most useful values are:
| Value | What it does |
|---|---|
| `baseline` | The default. Aligns the box baseline with the parent baseline. |
| `top` | Aligns the top of the box with the top of the line box. |
| `middle` | Aligns the vertical midpoint of the box with the parent baseline plus half the parent's x-height. |
| `bottom` | Aligns the bottom of the box with the bottom of the line box. |
| `text-top` / `text-bottom` | Aligns with the top or bottom of the parent's font box. |
| `sub` / `super` | Lowers or raises the box to subscript or superscript position. |
| length or percentage | Raises (or, if negative, lowers) the box by that amount. A percentage is relative to the element's own `line-height`. |
For the uneven cards, set vertical-align: top on the cards:
.card {
display: inline-block;
width: 10rem;
padding: 0.5rem;
border: 1px solid #888;
vertical-align: top;
}Now the three tops sit on the top of the line box, and the extra height of the tall card only extends the bottom.
The mystery gap under an image
Images are inline by default, so they sit on the baseline of their line. The line box reserves room below the baseline for descenders. The result is a few pixels of empty space under the image, even when the image fills its container.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Image gap</title>
<style>
.frame {
width: 200px;
background: #c33;
}
.frame img {
width: 200px;
height: 100px;
background: #39c;
}
</style>
</head>
<body>
<div class="frame">
<img alt="Placeholder" src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==">
</div>
</body>
</html>In this page a red strip shows under the image. The strip is the descender space. The same effect applies to any inline-block box, such as an icon box or a video element. Two fixes work:
.frame img {
display: block;
}.frame img {
vertical-align: middle;
}Use display: block when the image should own its whole line. Use vertical-align: middle, top, or bottom when the image must stay inline, for example next to text. Any value other than baseline removes the descender gap, because the image no longer rests on the baseline.
Aligning an icon with text
Another common case is a small inline-block icon next to a label. With the default baseline rule, an empty icon box sits with its bottom edge on the baseline, so it looks too high.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Icon alignment</title>
<style>
.icon {
display: inline-block;
width: 1rem;
height: 1rem;
background: #d84;
border-radius: 50%;
vertical-align: middle;
}
</style>
</head>
<body>
<p><span class="icon"></span> Status: online</p>
</body>
</html>The icon is empty, so its baseline is its bottom margin edge. With vertical-align: middle the icon centers on the lowercase letters next to it. If the result is a pixel or two off, try a length value such as vertical-align: -0.125em. A length is relative to the font size, so it scales when the text size changes.
Common errors
- Setting
vertical-alignon a block. Adivwithdisplay: blockignores it. Change itsdisplayor use a different layout method. - Setting it on the parent. It does not inherit and it moves the element itself, so it must go on the children.
- Adding
overflow: hiddento a card. This changes the card's baseline to its bottom margin edge and shifts it in the row. This surprises people who add the property to clip an image or to contain floats. - Mixing
topandbottomwithbaselinein one line. The line box height is calculated from all of the boxes. A box aligned totoporbottomcan still make the line taller, and it can leave extra space. - Expecting the fix to remove the whitespace gap. The horizontal gap between inline-block boxes is a different problem. It comes from spaces in the markup and
vertical-aligndoes not affect it.
Alternatives
If you are building a row of cards, a flex container is usually simpler. With display: flex the children are not in a line box, so there are no baselines to fix by default. The align-items property controls cross-axis alignment, and the default value stretch makes every card the same height.
.row {
display: flex;
align-items: flex-start;
gap: 1rem;
}Flexbox can also align items on their text baselines with align-items: baseline, which is useful for a row of buttons with different font sizes. Use vertical-align when boxes must flow inside running text, such as an icon in a sentence, a badge in a heading, or an image inside a paragraph.
Exercise
A row of inline-block cards has uneven top edges. One card has five lines of text, and the others have one. A teammate sets vertical-align: top on the row's container, and nothing changes. Why, and what is the correct fix?
Show answer
vertical-align applies to the inline-level box being placed in its line, and it is not inherited. Setting it on the container does nothing to the cards. The fix is to put vertical-align: top on each card (for example on the .card rule). Alternatively, make the container display: flex with align-items: flex-start.
Exercise
An inline-block box with overflow: hidden and three lines of text sits next to a plain paragraph of text. Which part of the box lines up with the text baseline?
Show answer
The bottom margin edge of the box. A box whose overflow is not visible uses its bottom margin edge as its baseline, not the baseline of its last line of text.