Menu

HTML Center Text: text-align, Vertical Centering, Indent

Center text in HTML with the CSS text-align: center property. Learn where to put it, why it does not work on a span, how to center text vertically in a box, how to align text to the start, end or both edges, and how to indent paragraphs.

This page includes runnable editors - edit, run, and see output instantly.

How to center text in HTML

Use the CSS property text-align: center on the element that holds the text:

text-align centers every line inside the element, including inline things such as links, images and buttons. It is inherited, so setting it on a container centers the text in everything inside it.

Put it on the block, not the span

text-align works on block containers: <p>, <div>, headings, table cells. On a <span> or <a> it does nothing, because an inline element is only as wide as its text and there is no space to center within:

The fix is always the same: move text-align: center to the parent block.

Center text, left-align the lines

For a paragraph of several lines, fully centered text is hard to read because every line starts at a different place. A common pattern is to center the block on the page and keep its text aligned to the start:

margin-inline: auto centers a block that has a width, text-align centers the lines inside it. Centering boxes (with flexbox, grid or margins) is covered on the center a div page.

Center text vertically

text-align only works horizontally. To center text vertically inside a box, make the box a flex container:

align-items: center centers vertically, justify-content: center centers horizontally, and text-align: center still centers the lines relative to each other when there are several. The line-height trick only works for a single line of text in a box of known height.

All text-align values

ValueResult
start (default)Lines start at the start edge: left in English, right in Arabic and Hebrew
endLines end at the end edge
centerLines centered
justifyLines stretched to touch both edges (except the last line)
left, rightAlways left or always right, whatever the language

Prefer start and end over left and right: they flip automatically for right to left languages, as the last box shows.

The old center tag

Old pages center text with <center> or with an align attribute:

<center>Old way</center>
<p align="center">Also old</p>

Both are obsolete. Browsers still render them so old sites do not break, but they are invalid in current HTML and mix layout into the markup. Use text-align: center in CSS.

Indenting text

HTML has no indent tag, and typed spaces collapse (see HTML space). Indents are CSS:

IndentCSS
First line onlytext-indent: 2em
Whole blockpadding-inline-start: 2em or margin-inline-start: 2em
Hanging (first line out)padding-inline-start: 2em; text-indent: -2em

em ties the indent to the font size, so it scales with the text.

Common mistakes

  • text-align on an inline element. Put it on the parent block.
  • Expecting text-align to center a box. It centers text and inline content, not a block with a width. Use margin-inline: auto or flexbox for boxes.
  • Using <center> or align="center". Obsolete. Use CSS.
  • Centering long paragraphs. Center headings and short lines; keep body text aligned to the start.
  • Indenting with &nbsp; or spaces. Use text-indent or padding.

Frequently Asked Questions

How do I center text in HTML?

Add text-align: center to the element that contains the text, for example <p style="text-align: center"> or a CSS rule like h1 { text-align: center; }. It centers every line of text and inline content inside that element.

Why does text-align: center not work on my span?

A span is inline, so it is only as wide as its text and there is no room to center within. Put text-align: center on the parent block (the <p> or <div> around it), or make the span display: block.

How do I center text vertically?

Make the container a flex box: display: flex; align-items: center; (add justify-content: center to center horizontally too). For a single line in a box of fixed height, line-height equal to the height also works.

Is the <center> tag still valid?

No. <center> and the align="center" attribute are obsolete. Browsers still render them for old pages, but new code should use text-align: center in CSS.

How do I indent text in HTML?

Use CSS. text-indent: 2em indents the first line of a paragraph, and padding-inline-start: 2em or margin-inline-start: 2em indents the whole block. Spaces or &nbsp; in the text are not a reliable indent.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED