Web Design
HTML
CSS
Text Alignment
Web Development

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.

html
<div class="box">
  <span>Hello world</span>
</div>
css
1.box {
2  height: 160px;
3  display: flex;
4  align-items: center;
5  justify-content: center;
6  border: 1px solid #999;
7}

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.

html
<div class="card">
  <p>Centered text that may wrap to two or three lines.</p>
</div>
css
1.card {
2  min-height: 180px;
3  display: grid;
4  place-items: center;
5  border: 1px solid #999;
6  padding: 16px;
7}

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:

html
<div class="badge">Online</div>
css
1.badge {
2  height: 40px;
3  line-height: 40px;
4  text-align: center;
5  border: 1px solid #999;
6}

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:

css
div {
  vertical-align: middle;
}

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:

html
<div class="table-box">
  <div class="table-cell">Centered text</div>
</div>
css
1.table-box {
2  display: table;
3  width: 240px;
4  height: 120px;
5}
6
7.table-cell {
8  display: table-cell;
9  vertical-align: middle;
10  text-align: center;
11  border: 1px solid #999;
12}

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:

css
1.box {
2  display: flex;
3  align-items: center;
4}

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-height only 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: middle on a normal block div.
  • Using line-height for 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: center is excellent for simple centered boxes.
  • 'line-height is only reliable for fixed-height single-line text.'
  • 'vertical-align is not the correct tool for most block-level div centering problems.'

Course illustration
Course illustration

All Rights Reserved.