How to Align Text Left, Center or Right with text-align in CSS
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
<block selector> {
text-align: <alignment>;
}
| Part | What 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:

<!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 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
| Mistake | What happens | Do this instead |
|---|---|---|
Setting text-align on an inline element, such as a span | Nothing moves: the span's text stayed at the left | Set it on the block around the text, such as the p |
Expecting text-align: center to center the box itself | It centers the text inside the box: a 200-pixel box stayed at the left with its text centered in it | Center the box with margin: 0 auto |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| The text doesn't move | The rule is on an inline element | Move it to the block that holds the text |
| The text is centered, but the box isn't | text-align only moves the content | Center 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
- text-align, on MDN.
