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
| Value | Result |
|---|---|
start (default) | Lines start at the start edge: left in English, right in Arabic and Hebrew |
end | Lines end at the end edge |
center | Lines centered |
justify | Lines stretched to touch both edges (except the last line) |
left, right | Always 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:
| Indent | CSS |
|---|---|
| First line only | text-indent: 2em |
| Whole block | padding-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-alignon an inline element. Put it on the parent block.- Expecting
text-alignto center a box. It centers text and inline content, not a block with a width. Usemargin-inline: autoor flexbox for boxes. - Using
<center>oralign="center". Obsolete. Use CSS. - Centering long paragraphs. Center headings and short lines; keep body text aligned to the start.
- Indenting with
or spaces. Usetext-indentor 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 in the text are not a reliable indent.