Full answer
vertical-align works in two places only: on inline-level elements, and on table cells. It does not work on normal block elements such as a div. This is the most common mistake, and it is why people say "vertical-align does not work".
On inline and inline-block elements, it aligns the element inside its line:
baselineis the default. The bottom of an inline-block box sits on the text baseline, so there is a small gap below it for letters like "g" and "y".topandbottomalign the box with the top or bottom of the line.middlealigns the middle of the box with the middle of the lower-case letters in the parent text.text-top,text-bottom,subandsuperalign by the font or shift the box.- A length or a percentage raises or lowers the box.
On table cells (td, th and elements with display: table-cell), it aligns the content inside the cell: top, middle or bottom. The default for cells is middle.
The baseline gap is a common cause of strange space under images. An image is inline by default and sits on the baseline. Setting vertical-align: middle or display: block on the image removes the gap.
To centre a block, use Flexbox or Grid instead: display: flex; align-items: center;, or display: grid; place-items: center;. These are the current tools. Older code used display: table-cell with vertical-align: middle.
The box on the baseline stays above the bottom of the line. The top box touches the top of the line box, and the bottom box touches the bottom. In the table cell, the text is in the middle. The div inside the tall wrapper stays at the top, because vertical-align does nothing on a block.
For a modern centred layout, choose Flexbox or Grid, and keep vertical-align for inline icons and table cells.
<style>
.line { line-height: 20px; font-size: 16px; margin: 0 0 10px; }
.line span { display: inline-block; width: 10px; height: 10px; background: #c33; }
.top { vertical-align: top; }
.bottom { vertical-align: bottom; }
table { border-collapse: collapse; }
td { height: 80px; border: 1px solid #999; vertical-align: middle; }
.wrap { height: 100px; border: 1px solid #999; }
.wrap div { vertical-align: middle; }
</style>
<p class="line" id="p1">Text <span id="s0"></span></p>
<p class="line" id="p2">Text <span class="top" id="st"></span></p>
<p class="line" id="p3">Text <span class="bottom" id="sb"></span></p>
<table><tr><td id="cell"><span id="cs">Centred</span></td></tr></table>
<div class="wrap" id="wrap"><div id="blk">Not moved</div></div>Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- Works on inline-level elements and table cells only.
- baseline is the default, and it leaves a gap for descenders.
- top, middle and bottom align in the line or the cell.
- It does nothing on block elements.
- Use flex or grid to centre blocks.
Common mistakes
- Using vertical-align on a div and expecting it to centre.
- Ignoring the baseline gap under inline images.
- Using table-cell tricks for new layouts.
Follow-ups
- Why is there a gap under an inline image?
- What is the default value of vertical-align?
Tips
- State where vertical-align works: inline and table cells only.
- Name the modern way to centre a block: flex or grid.