Skip to main content

How to Center Text in CSS

Three simple methods to center text horizontally, vertically, or both. Copy-paste ready code examples.

Quick Answer

Horizontal Only

text-align: center;

Both Directions

display: grid; place-items: center;
1

text-align: center

The classic method for horizontal text centering. Simple and works everywhere.

Centered Text
HorizontalVertical
2

Flexbox Centering

Centers text both horizontally and vertically. Most versatile method.

Centered Text
HorizontalVertical
3

CSS Grid Centering

The shortest code for centering in both directions.

Centered Text
HorizontalVertical

Frequently Asked Questions

What's the simplest way to center text horizontally?

Use text-align: center on the parent element. This works for all inline content including text, spans, and inline-block elements. It's the most straightforward method and has 100% browser support.

How do I center text vertically and horizontally?

Use Flexbox: display: flex; justify-content: center; align-items: center; on the parent. Or use Grid: display: grid; place-items: center;. Both methods center text in both directions.

Why doesn't vertical-align center my text?

vertical-align only works on inline and table-cell elements, not block elements. For block elements, use Flexbox or Grid. vertical-align: middle aligns inline elements relative to each other, not within a container.