How do I vertically align text in a div?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Vertical centering in CSS is one of those problems that looks easy until the content starts wrapping or the layout changes. The reason is simple: different CSS features center content in different layout contexts. For most modern code, Flexbox or Grid is the right answer, while older tricks only work in narrow cases.
Use Flexbox for Most Cases
If you want text centered vertically inside a div, Flexbox is usually the best default because it handles one line, multiple lines, and mixed content reliably.
align-items: center does the vertical centering. justify-content: center also centers horizontally. If you only want vertical centering, keep the first line and remove the second.
This method works well when:
- the container has a known height or min-height
- the text might wrap
- the container includes more than plain text, such as icons or buttons
Grid Is Also Excellent for Simple Centering
CSS Grid can center content with very compact syntax.
place-items: center centers content vertically and horizontally. This is ideal for simple cards, badges, loading panels, and other boxed components.
The line-height Trick Only Works for One Line
If the element contains a single line of text and the height is fixed, matching line-height to the height still works:
This works because the text line box is made as tall as the container. It is fast and simple, but very limited. The moment the text wraps, the alignment falls apart.
Use this only when all of these are true:
- the content is guaranteed to stay on one line
- the height is fixed
- you do not need a general-purpose layout system
Why vertical-align: middle Usually Fails
Many developers try:
and then wonder why nothing changes. That is expected because vertical-align does not vertically center ordinary block elements in normal flow. It mainly applies to inline elements, inline-block alignment, and table cells.
A table-cell approach does work:
This is valid CSS, but it is mostly a legacy technique. In new code, Flexbox or Grid is usually clearer.
Container Height Still Matters
A common hidden issue is that the parent has no height. If the div naturally shrinks to the height of its content, there is no spare vertical space to center within.
For example, this will not create visible centering:
unless .box has some actual height, padding, or min-height. Vertical centering only makes sense when the container is taller than the content.
Choosing the Right Method
A practical rule:
- use Flexbox for general layouts
- use Grid for simple centered content blocks
- use
line-heightonly for fixed-height single-line text - use table-cell only when maintaining older CSS patterns
That keeps the solution aligned with the actual layout problem instead of forcing one old technique everywhere.
Common Pitfalls
- Using
vertical-align: middleon a normal blockdiv. - Using
line-heightfor text that can wrap. - Forgetting to give the container height or min-height.
- Centering both axes by accident when only vertical alignment was needed.
- Keeping old table-cell hacks in new layouts where Flexbox or Grid would be clearer.
Summary
- There is no single universal CSS property for vertical centering in every layout.
- Flexbox is the best general-purpose solution for vertically aligning text in a
div. - Grid with
place-items: centeris excellent for simple centered boxes. - '
line-heightis only reliable for fixed-height single-line text.' - '
vertical-alignis not the correct tool for most block-leveldivcentering problems.'

