Why CSS vertical-align Doesn’t Work and How to Center Content Vertically
vertical-align only works on inline elements and table cells. To center text vertically inside a box such as a <div>, add align-content: center to the box, or use display: flex with align-items: center. Use these when you want a heading or text in the vertical middle of a card, button, or banner.
I tested on Ubuntu 24.04, Node.js 22.23.5, Playwright 1.63.0, and Chromium 158. I opened an HTML and CSS-only page with Playwright and measured positions with getBoundingClientRect(). Unless noted otherwise, the box is a <div> 300px wide and 120px tall, the text is 16px, line-height is 1.5, and the font is Noto Sans CJK JP. I did not test Firefox or Safari.

vertical-align only works on inline elements and table cells
The MDN page for vertical-align says that this property applies only to inline elements, inline-block elements, and table cells. It cannot control the vertical position of block-level elements.
I set vertical-align: middle on a <div> that was 120px tall, then measured the position of the text inside. The top of the text was 0px from the top of the box, and 96px was left empty below it. The result was the same as with no setting. getComputedStyle() reports the <div>'s display as block. Its vertical-align computes to middle, but a block box ignores it.
The vertical-align of the inner <span> computed to baseline. The MDN property table also lists vertical-align as non-inherited. Setting it on the parent does not affect the inner text.
I measured the following combinations that do not work. In every case, the text stayed at the top of the box.
| Element with the setting | Computed display |
Top of the text |
|---|---|---|
Box <div> |
block |
0px |
Inner <p> (block element) |
block |
0px |
Inner <span> with float: left added |
block |
0px |
Inner <span> with position: absolute added |
block |
0px |
A <span> is normally inline, but adding float or position: absolute changed its computed display to block. In that case, vertical-align has no effect.
vertical-align has no effect on flex or grid children
I set the parent to display: flex and gave the child <span>
vertical-align: middle or bottom. The position did not change. The top of the child stayed at 0px, and its height stretched to the full 120px of the box. The same happened when I set the parent to display: grid.
The flex container section of the CSS Flexbox specification says that a flex container is not a block container, so some properties that assume block layout do not apply. It lists vertical-align as having no effect on flex items. The grid container section of the CSS Grid specification says the same about grid items. To set the vertical position inside flex or grid, set align-items on the parent, as shown in the section on centering inside a box below.
On an inline element, it moves only within the line box
Setting vertical-align on an inner <span> does work, but the range is small. I set vertical-align: middle on a <span> inside the 120px box. The top of the text moved from 0px down to only 2.66px. With bottom, it stayed at 0px.
This is because vertical-align positions content within the line box (the area for one line of text), not within the whole box. The height of that line is 24px: 16px text with line-height: 1.5. Even if the box is 120px tall, the line box occupies only 24px at the top. According to MDN, middle aligns the midpoint of the element with the parent's baseline plus half the x-height (the height of a lowercase x). A <span> that is already inside a line box moves only a little in that case.
vertical-align does its intended job when you place an icon or image next to text. I placed a 40px icon (inline-block) before 16px text and compared the centers of the two.
| Setting | Icon center minus text center | Line height |
|---|---|---|
| None | −13px | 45px |
vertical-align: middle on parent <p> |
−13px | 45px |
vertical-align: middle on icon |
2.66px | 40px |
Setting it on the parent <p> changed nothing, and the icon's vertical-align still computed to baseline. Setting it on the icon itself reduced the difference in centers from 13px to 2.66px. In the MDN definition, middle is based on the baseline plus half the x-height, so it does not match the text's center exactly. For a more precise way to align the centers of an icon and text, see the article "How to align an icon and text height with CSS vertical-align."
Apply it to both elements
When two inline-block elements of different heights sit side by side, setting middle on only one made the offset larger.
| Setting | Center of 40px box minus center of 80px box | Parent height |
|---|---|---|
| None | 20px | 85px |
middle on the 40px box only |
35.66px | 95.66px |
middle on both boxes |
0px | 80px |
With no setting, the bottom edges of the two boxes lined up. When only the 40px box had middle, only that box moved, and the 80px box stayed where it was, so the centers drifted further apart. When both boxes had middle, the difference in centers was 0px.
How to center content vertically inside a box
I compared ways to center one line of text (24px tall) inside a 120px box. In every case, the text sat 48px from the top and 48px from the bottom.
| Setting on the box | Space above text | Space below text |
|---|---|---|
align-content: center |
48px | 48px |
display: flex; align-items: center |
48px | 48px |
display: grid; align-items: center |
48px | 48px |
display: table-cell; vertical-align: middle |
48px | 48px |
line-height: 120px |
48px | 48px |
align-content: center is the simplest option
align-content: center lets you keep the box's display unchanged. The box stayed display: block, and the content moved to the vertical center.
.card {
height: 120px;
align-content: center; /* display can stay block */
}
As the MDN page for align-content explains, align-content applies to block-level elements as well as flex and grid containers. According to the browser compatibility data (browser-compat-data) on MDN, support for block layout starts with Chrome 123, Firefox 125, and Safari 17.4. If you need to support older browsers, use the flex or grid approach below.
Use flex or grid to center horizontally too
For flex, set both display: flex and align-items: center on the box. If you also want to center horizontally, display: grid with place-items: center does it in one line. In a 300px wide box, 63.53px wide text started at 118.23px from the left, so the margins on both sides matched.
.card {
height: 120px;
display: grid;
place-items: center; /* centered both vertically and horizontally */
}
With flex or grid, the child elements become flex or grid items. As shown earlier, vertical-align on those children no longer has any effect.
display: table-cell works like a table cell
In a table cell, vertical-align determines the vertical position of the content. When I did not set anything on a <td>, its computed vertical-align was middle. In a 120px tall <td>, the text sat 48px from the top and bottom with no setting, 0px from the top with top, and 0px from the bottom with bottom.
Adding display: table-cell to a <div> makes it behave like a table cell, so vertical-align: middle centers the content.
line-height works only for a single line
If you set line-height to 120px, the same as the box height, a single line is centered. However, it breaks when the text wraps. I tested a long sentence that does not fit in 300px. It wrapped to three lines, and each line took 120px, so the content became 360px tall and overflowed the box.
With the same long sentence, align-content: center and flex with align-items: center kept the three lines (72px in total) inside the box with 24px of space above and below. When the amount of text can change, avoid using line-height for centering.





