vertical-align
Aligns inline elements and table cell content vertically.
- Initial value
baseline- Inherited
- No
- Animatable
- yes, for lengths and percentages
- Applies to
- inline elements and table cells
- JavaScript
element.style.verticalAlign = "middle"
Example
#.icon {
width: 20px;
height: 20px;
vertical-align: -4px;
}<p style="margin: 0; font-size: 16px;">
<img class="icon" src="/collections/nextjs.png" alt="" width="20" height="20" style="vertical-align: -4px;">
Next.js in the course’s new module
</p>Definition and usage
#The vertical-align property works in two contexts. On inline elements, such as icons or images inside text, it positions them relative to the line of text: on the baseline, centered, at the top. On table cells, it aligns the whole cell’s content vertically.
It doesn’t center blocks inside a container. That’s a common misunderstanding: vertical-align: middle on a plain <div> changes nothing. Flexbox or Grid handle that instead.
Classic uses are an icon next to text (middle), and removing the gap under an inline image that sits on the baseline (vertical-align: top or display: block).
Syntax
#vertical-align: baseline | middle | top | bottom | text-top | text-bottom | sub | super | <length> | <percentage>;Values
#Values in practice
#baseline
.demo { vertical-align: baseline; }middle
.demo { vertical-align: middle; }top
.demo { vertical-align: top; }super
.demo { vertical-align: super; }-6px
.demo { vertical-align: -6px; }Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- To center a block vertically, use Flexbox or Grid, not
vertical-align. - A small gap under an image inside a block disappears once you set the image to
display: blockorvertical-align: top.
Related entries
#- line-heightSets the height of each line of text, the space between lines.
- text-alignAligns text horizontally: left, right, center, or justified.
- align-itemsAligns items along the cross axis of a flex or grid container.
- displayDetermines how an element behaves in the page layout and how it lays out its children.
Spotted a mistake or missing an example? Write to us.