CSS Reference ยท Typography
The vertical-align property
Reference for the CSS vertical-align property: baseline, middle, top, sub, super and lengths for inline boxes and table cells, with live examples and pitfalls.
The vertical-align property sets how an inline-level box (text, an image, an inline-block) is lined up vertically against the other boxes on the same line. In table cells, it sets how the cell's content is aligned within the cell.
The most common mistake is to expect it to center a block inside its parent. It does not: it applies only to inline-level elements and table cells. To center blocks vertically, use flexbox or grid; for a gap under an image, use vertical-align or display: block on the image.
img.icon { vertical-align: middle; }
Try it
In the first line, the chips sit at different heights next to the large text: baseline aligns the chip's text with the text baseline, middle centers it around the middle of the lowercase letters, and top and bottom align it to the edges of the line box. The second line shows sub and super.
In table cells, the property aligns the content inside the cell. Each frame has an orange background so the gap is visible: an inline image sits on the text baseline and leaves a strip below it for descenders. With vertical-align: middle (or display: block) the strip disappears.
Syntax
vertical-align: baseline | sub | super | text-top | text-bottom | middle | top | bottom | <length-percentage>;
| Initial value | baseline |
| Applies to | inline-level boxes and table cells |
| Inherited | no |
| Animatable | yes, for lengths and percentages |
| Computed value | for lengths and percentages, the absolute length; otherwise as specified |
Values
| Value | Description |
|---|---|
baseline |
Aligns the baseline of the box with the parent's baseline. Default. In table cells, it aligns the first line's baseline with other cells in the row. |
sub |
Lowers the box to the position for subscripts. |
super |
Raises the box to the position for superscripts. |
text-top |
Aligns the top of the box with the top of the parent's font. |
text-bottom |
Aligns the bottom of the box with the bottom of the parent's font. |
middle |
Aligns the middle of the box with the baseline plus half the x-height of the parent. |
top |
Aligns the top of the box with the top of the line box. |
bottom |
Aligns the bottom of the box with the bottom of the line box. |
<length> |
Raises (positive) or lowers (negative) the box by this amount relative to the baseline. |
<percentage> |
Like a length, as a percentage of the element's own line-height. |
Usage notes
- Inline and table cells only. On a
<div>or other block,vertical-aligndoes nothing. Usealign-items,align-contentor grid centering instead. - It changes line height. An aligned box can make the line taller, because the line box must contain every aligned box. Large or raised items can push lines apart.
- The image gap. An
<img>is inline and sits on the baseline, so there is space below it for descenders. Usedisplay: blockorvertical-align: middleto remove it. - Table cells. Cell content aligns to
middleby default in many browsers' default styles. Set it explicitly (top) when you want text to start at the top of tall cells. - Subscripts and superscripts. Prefer
<sub>and<sup>for meaning. If you usevertical-align: super, also shrink the font so the line height does not grow. - Newer syntax. Recent specifications also make
vertical-aligna shorthand foralignment-baseline,baseline-shiftandbaseline-source, but the keywords above are what matters in practice.
Browser support
Supported in every browser.
Related
line-heightโ size of the line boxdisplayโinline-blockand table display typesalign-itemsโ cross-axis alignment in flex and gridtext-alignโ horizontal alignment- Lesson: Line Height and Vertical Rhythm