Course contentsShow
← Web Development
Lesson 171 of 2,9823. CSS FundamentalsFree lesson

Text Alignment and Justification

Align text horizontally with text-align and understand the implications of justify alignment.

Text Alignment and Justification

What you'll learn: How to control the horizontal alignment of text within its container using the text-align property.

The Core Idea

When you type text into a container, it needs to know where to sit horizontally—should it hug the left edge, center itself, align to the right, or spread out evenly? The text-align property controls this behavior, just like the alignment buttons in a word processor.

The text-align Property

The text-align property accepts several values:

  • left — Text aligns to the left edge (default for left-to-right languages)
  • right — Text aligns to the right edge
  • center — Text centers within the container
  • justify — Text spreads out to fill the full width of the container
h1 {
  text-align: center;
}

p {
  text-align: left;
}

.quote {
  text-align: right;
}

Understanding Justify

The justify value is special—it stretches each line (except the last one) so both the left and right edges align perfectly with the container. Think of it like a newspaper column where every line reaches both margins.

.article {
  text-align: justify;
}

The Justify Trade-off

While justify creates clean, uniform edges, it can create awkward spacing between words, especially in narrow containers or with long words. This makes text harder to read. Use justify sparingly and test it carefully, particularly on responsive layouts where container widths change.

When to Use Each

  • Left: Standard body text (in LTR languages)
  • Right: Sometimes used for labels or in RTL layouts
  • Center: Headings, hero text, short decorative passages
  • Justify: Long-form articles where the formal look outweighs readability concerns

Key Takeaway: The text-align property controls horizontal text alignment with left, right, center, and justify values—but use justify cautiously as it can create uneven word spacing that hurts readability.