Master Code
On The Go

Learn. Practice. Build.

Home › Code Snippets › CSS Snippets › Reference › text-align

CSS Reference

text-align

Controls the horizontal alignment of text and inline content inside a block container.

Last updated: October 2026

Definition: The text-align property sets the horizontal alignment of inline content (text, inline images, inline-block elements) inside a block-level container. It inherits to child elements and applies to all inline content inside.

🎬 Live Example

Every alignment value, side by side.

text-align: left; — Aligns text to the left edge of the container.
text-align: right; — Aligns text to the right edge of the container.
text-align: center; — Centers the inline content horizontally.
text-align: justify; — Stretches the text to fill the container. Notice the uneven word spacing that makes this hard to read.
text-align: start; — Logical value. Same as left in LTR languages.
text-align: end; — Logical value. Same as right in LTR languages.

💡 start and end are the modern, internationalization-friendly alternatives to left and right.

📝

Basic Syntax

.center {
    text-align: center;
}

.right {
    text-align: right;
}

.logical {
    text-align: start;  /* Respects document direction */
}

It applies to the block container and affects all inline content inside it.

🎯 All text-align Values

Value Behavior Use
leftAligns to the left edgeDefault for body text
rightAligns to the right edgeNumbers, prices, dates
centerCenters the contentHeadings, heroes, buttons
justifyStretches lines to fill widthRarely — mostly print
startLogical start (left in LTR)Modern replacement for left
endLogical end (right in LTR)Modern replacement for right
inheritUses parent's valueRarely needed
1

text-align vs Centering a Block

A common source of confusion.

/* ❌ text-align: center does NOT center a div */
.parent {
    text-align: center;
}
.child {
    width: 200px;
}
/* The child stays left-aligned as a block. */

/* ✅ To center a block, use auto margins */
.child {
    width: 200px;
    margin: 0 auto;
}

/* ✅ Or use flexbox */
.parent {
    display: flex;
    justify-content: center;
}

💡 Rule: text-align centers inline content. margin: 0 auto or flexbox centers block elements.

2

text-align vs align-items

Property Aligns
text-alignInline content (text) inside a block
align-itemsBlock-level children inside a flex or grid container
justify-contentDistribution along the flex/grid main axis
3

Why Not to Use justify

Justified text creates uneven word gaps because browsers don't hyphenate well on the web.

  • Rivers of white space — long gaps between words in narrow columns.
  • Harder to track — readers with dyslexia struggle to find the next line.
  • Poor mobile experience — narrow screens make the gaps worse.
  • Not necessary — left-aligned text is more readable and accessible.

If you must use justify: Add hyphens: auto and text-wrap: pretty (or balance) to reduce the rivers. Even then, avoid for body text.

4

Common Real-World Uses

/* Page hero */
.hero {
    text-align: center;
}

/* Right-aligned table numbers */
.price-column {
    text-align: right;
    font-variant-numeric: tabular-nums;
}

/* Centered container with max-width */
.container {
    max-width: 800px;
    margin: 0 auto;
    text-align: start;  /* Reset to start */
}

/* Right-aligned "More" links in lists */
.list-item .more {
    display: block;
    text-align: right;
    font-size: 0.875rem;
}

/* Centered navigation labels */
.tab-label {
    text-align: center;
    padding: 0.75rem 1rem;
}

🎯 Related text Properties

Property Purpose
text-alignHorizontal alignment of inline content.
text-align-lastAlignment of the last line (only when justify).
text-indentFirst-line indentation.
text-decorationUnderline, line-through, overline.
text-transformuppercase, lowercase, capitalize.
text-shadowShadow behind text.
text-overflowEllipsis for overflowing text.

⚠️ Common Mistakes

♿ Accessibility Notes

❓ Frequently Asked Questions

What is the CSS text-align property?

The text-align property controls the horizontal alignment of inline content (text, inline images, inline-block elements) inside a block-level container. It does not align the container itself — use margin: 0 auto or flexbox for that.

What is the difference between text-align and align-items?

text-align aligns inline content (text) within a block. align-items aligns block-level children within a flex or grid container. They operate on different levels of the layout.

Is text-align: justify good for web content?

Generally no for body text on the web. Justified text creates uneven word spacing because browsers don't hyphenate well, which is hard for users with dyslexia to read. Left-aligned text is the accessible default.

What does text-align: start and end mean?

start and end are logical values that respect the document's writing direction. In a left-to-right language (English), start = left and end = right. In Arabic or Hebrew (RTL), start = right and end = left.

How do I center text vertically?

text-align only handles horizontal alignment. For vertical centering, use flexbox (display: flex; align-items: center; justify-content: center) or the line-height technique for single-line text.

🎯 What's Next?

← All CSS Reference