Skip to main content

How to Align Text Left, Center or Right with text-align in CSS

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

text-align lines the text up inside its box: on the left, in the center or on the right. It goes on the block that holds the text, such as a paragraph.

Syntax​

Syntax
<block selector> {
text-align: <alignment>;
}
PartWhat it does
<block selector>The block that holds the text, such as p or div.
<alignment>Where the text goes: left, center or right, or justify to spread it to both edges.

MDN's text-align page says it sets the horizontal alignment of the inline content inside a block element.

The Example​

The example is three paragraphs, aligned left, center and right with one class each.

Step 1: Write the Code​

Create a file named index.html in a text editor, such as Visual Studio Code:

index.html, an HTML page with a style element holding three rules, the class left with text-align left, the class center with text-align center and the class right with text-align right, and three paragraphs, one with each class.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Text align</title>
<style>
.left {
text-align: left;
}

.center {
text-align: center;
}

.right {
text-align: right;
}
</style>
</head>
<body>
<p class="left">Left</p>
<p class="center">Center</p>
<p class="right">Right</p>
</body>
</html>

Step 2: Open It in a Browser​

Double-click index.html. It opens in your browser:

The page in Chromium: the word Left at the left edge, Center in the middle of the page and Right at the right edge, one under the other.

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.

What Happened​

  • Each word sits where its class says. All three paragraphs are the full width of the page, and the text moved inside them: in a window 480 pixels wide, Left started at 8 pixels, Center sat around the middle at 240, and Right ended at 472.
  • Left looks the same as no rule at all, because left is where text starts in English.

Common Mistakes​

MistakeWhat happensDo this instead
Setting text-align on an inline element, such as a spanNothing moves: the span's text stayed at the leftSet it on the block around the text, such as the p
Expecting text-align: center to center the box itselfIt centers the text inside the box: a 200-pixel box stayed at the left with its text centered in itCenter the box with margin: 0 auto

Troubleshooting​

SymptomLikely causeFix
The text doesn't moveThe rule is on an inline elementMove it to the block that holds the text
The text is centered, but the box isn'ttext-align only moves the contentCenter the box with margins

FAQ​

Why doesn't text-align work on a span?​

MDN's page says it applies to block containers. A span is inline, so text-align: center on it changed nothing.

Can I center text inside a narrow box?​

Yes. In a 200-pixel-wide div with text-align: center, the text sat in the middle of the box, and the box stayed at the left of the page.

Conclusion​

Put text-align on the block that holds the text, with left, center or right. It moves the text inside the box, never the box itself.

Additional Resources​