How to Align Text in CSS
What You’ll Build or Solve
You’ll align text horizontally using text-align. By the end, you’ll know how to apply alignment to containers or specific elements and avoid confusing text alignment with layout positioning.
When This Approach Works Best
This approach works best when you:
Learn CSS on Mimo
- Center headings in a hero section
- Right-align prices or numbers
- Justify long article paragraphs
- Align labels inside cards or sections
Avoid layout systems like Flexbox or Grid when you only need to move text inside a block. Use those systems when positioning elements, not aligning text content.
Prerequisites
- A basic HTML file
- A linked CSS file or a
<style>block - Basic knowledge of elements like
p,h1, anddiv
Example structure used in this guide:
CSS
<divclass="container">
<h1>Title</h1>
<p>Sample paragraph text.</p>
</div>
Step-by-Step Instructions
Step 1: Align Text Horizontally with text-align
Use text-align on a block-level element.
CSS
.container {
text-align:center;
}
Common Values
CSS
text-align:left;
text-align:right;
text-align:center;
text-align:justify;
You can apply alignment to a container or to specific elements:
CSS
h1 {
text-align:center;
}
p {
text-align:justify;
}
Choose the selector that matches your layout structure. Apply it to the block element that wraps the text.
What to Look For
- Apply
text-alignto block-level elements likediv,section,article, orp - The property aligns text and inline content inside the block
- In Flexbox or Grid layouts,
justify-contentmoves elements, whiletext-alignaligns the text inside them text-aligndoes not vertically center text- Inline elements like
spanwill not visibly change alignment unless converted to block-level elements
Examples You Can Copy
Example 1: Center a Page Heading
<h1class="page-title">Welcome</h1>
CSS
.page-title {
text-align:center;
}
Example 2: Right-Align Prices in a List
<divclass="product">
<spanclass="name">Laptop</span>
<spanclass="price">$999</span>
</div>
CSS
.price {
display:block;
text-align:right;
}
Example 3: Justify Article Text
CSS
<article>
<p>
This is a longer paragraph that spans multiple lines to show how justified alignment distributes space evenly between words.
</p>
</article>
CSS
articlep {
text-align:justify;
}
Example 4: Center Text Inside a Card
CSS
<divclass="card">
<h2>Join Now</h2>
<p>Sign up to get started.</p>
</div>
CSS
.card {
text-align:center;
}
Common Mistakes and How to Fix Them
Mistake 1: Using justify-content to Align Text
What you might do:
CSS
.container {
display:flex;
justify-content:center;
}
Why it breaks:
justify-content controls the position of child elements, not the text inside them.
Correct approach:
CSS
.container {
text-align:center;
}
Use Flexbox for layout positioning and text-align for text alignment.
Mistake 2: Applying text-align to Inline Elements
What you might do:
CSS
span {
text-align:center;
}
Why it breaks:
Inline elements do not control layout width, so alignment changes are not visible.
Correct approach:
Apply alignment to the parent:
CSS
div {
text-align:center;
}
Or convert the element:
CSS
span {
display:block;
text-align:center;
}
Troubleshooting
If the text doesn't move, confirm you applied text-align to a block-level parent.
If right alignment looks unchanged, check that the container has enough width.
If alignment works in one section but not another, inspect inherited styles.
If nothing updates, confirm your CSS file is linked correctly, and selectors match your HTML.
Quick Recap
- Use
text-alignto control horizontal text alignment - Apply it to block-level elements
- Do not confuse
justify-contentwith text alignment - Inline elements may need
display: block - Vertical alignment requires different properties
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot