CSS text-align Property

The CSS text-align property specifies the horizontal alignment of inline content within a block container.

It is commonly used to align paragraphs, headings, table cell content, and inline elements. The property supports physical alignment values such as left and right, as well as logical values such as start and end that respond to the text direction.

text-align Syntax

The text-align property accepts alignment keywords that determine how inline content is positioned along the inline axis.

Basic Syntax

p {
  text-align: center;
}

Common Values

p {
  text-align: left;
}
p {
  text-align: right;
}
p {
  text-align: center;
}
p {
  text-align: justify;
}
p {
  text-align: start;
}
p {
  text-align: end;
}

Additional Values

p {
  text-align: match-parent;
}
p {
  text-align: justify-all;
}

The match-parent value resolves logical alignment relative to the parent's direction. The justify-all value is defined in CSS Text Level 3 but has limited browser support.

CSS-Wide Keywords

p {
  text-align: initial;
}
p {
  text-align: inherit;
}
p {
  text-align: unset;
}
p {
  text-align: revert;
}
p {
  text-align: revert-layer;
}

The initial value is start, which aligns content according to the writing direction.

Property Values

Value Description
left Aligns inline content to the left side of the line box.
right Aligns inline content to the right side of the line box.
center Centers inline content within the line box.
justify Adjusts spacing to align text with both edges of the line box, except for the last line or a line immediately before a forced line break.
start Aligns content with the start edge according to the writing direction.
end Aligns content with the end edge according to the writing direction.
match-parent Behaves like inheritance but resolves start and end according to the parent's direction.
justify-all Justifies all lines, including the last line. Browser support is limited.
initial Resets the property to start.
inherit Uses the computed value from the parent element.
unset Uses the inherited value because the property is inherited.
revert Reverts the property according to CSS cascade origin rules.
revert-layer Reverts the property according to CSS cascade layer rules.

Property Characteristics

Characteristic Value
Initial value start
Inherited Yes
Applies to Block containers
Computed value Specified keyword, with match-parent resolved to a physical alignment value
Animation type Discrete
Percentages Not accepted
Related properties text-align-last, text-justify, direction, writing-mode
Specification CSS Text Module Level 3

Valid Examples

h1 {
  text-align: center;
}
p {
  text-align: justify;
}
th {
  text-align: left;
}
.rtl-content {
  text-align: start;
}
.footer-text {
  text-align: end;
}

Invalid Examples

p {
  text-align: middle;
}
p {
  text-align: top;
}
p {
  text-align: 50%;
}
p {
  text-align: 20px;
}
p {
  text-align: left right;
}

Unsupported keywords, lengths, percentages, and combinations of unrelated alignment keywords are invalid.

How It Works

The text-align property determines how inline-level content is distributed within each line box of a block container.

Step 1: Create a Paragraph

<p class="example-text">
  This paragraph demonstrates horizontal text alignment.
</p>

Step 2: Center the Text

.example-text {
  text-align: center;
}

The text is centered within the paragraph's available inline space.

Step 3: Align the Text to the Right

.example-text {
  text-align: right;
}

The text is aligned to the right edge of the line box.

Step 4: Justify the Text

.example-text {
  text-align: justify;
}

The browser adjusts spacing in eligible lines to align their content with both edges.

Important Consideration

The property aligns inline content inside a container. It does not move or center the container itself.

Physical Alignment

Physical alignment values refer to specific sides of the line box rather than the logical beginning or end of the text.

Left Alignment

.left-text {
  text-align: left;
}

Aligns inline content to the left side.

Right Alignment

.right-text {
  text-align: right;
}

Aligns inline content to the right side.

Center Alignment

.center-text {
  text-align: center;
}

Centers inline content within the available line width.

Comparing Physical Values

Value Horizontal Writing Behavior
left Aligns content to the physical left side.
right Aligns content to the physical right side.
center Centers content between the two sides.

Important Consideration

Physical values are useful when alignment must remain on a particular side. For multilingual layouts, logical values are often more appropriate.

Logical Alignment

The start and end values align content according to the writing direction rather than a fixed physical side.

Start Alignment

.start-text {
  text-align: start;
}

In horizontal left-to-right text, start aligns to the left. In horizontal right-to-left text, it aligns to the right.

End Alignment

.end-text {
  text-align: end;
}

In horizontal left-to-right text, end aligns to the right. In horizontal right-to-left text, it aligns to the left.

Logical Alignment Comparison

Value LTR RTL
start Left Right
end Right Left
left Left Left
right Right Right

Using match-parent

.child-text {
  text-align: match-parent;
}

This value uses the parent's alignment, resolving logical alignment according to the parent's direction.

When to Use Logical Alignment

Use start and end when a layout should adapt naturally to languages with different reading directions.

Justified Text

The justify value distributes spacing within eligible lines so their content aligns with both edges of the line box.

Basic Justification

.justified-text {
  text-align: justify;
}

For typical English text, browsers primarily adjust spacing between words.

Last Line Behavior

By default, the last line of a justified paragraph is not stretched across the full width. Its alignment follows the applicable last-line alignment behavior.

Using text-align-last

.justified-text {
  text-align: justify;
  text-align-last: center;
}

The paragraph's eligible lines are justified, while the final line is centered.

Controlling Justification

.justified-text {
  text-align: justify;
  text-justify: inter-word;
}

The text-justify property can influence how spacing is distributed during justification. Browser support for its values varies.

Using justify-all

.justify-all-text {
  text-align: justify-all;
}

The justify-all keyword requests justification of every line, including the final line. It has limited browser support and should not be relied upon without testing.

Important Consideration

Justification can create large or uneven spaces between words, especially in narrow columns. Test readability before using it for long passages.

Text Direction

The direction property and the HTML dir attribute influence the behavior of logical alignment values.

Left-to-Right Text

<p dir="ltr" class="direction-text">
  Left-to-right text
</p>
.direction-text {
  text-align: start;
}

The text aligns to the left in horizontal writing mode.

Right-to-Left Text

<p dir="rtl" class="direction-text">
  نص باللغة العربية
</p>
.direction-text {
  text-align: start;
}

The text aligns to the right in horizontal writing mode.

Using end With RTL Text

.rtl-end {
  direction: rtl;
  text-align: end;
}

For horizontal right-to-left text, end aligns content to the left.

Using the HTML dir Attribute

When indicating the direction of actual document content, prefer the HTML dir attribute. It communicates text direction semantically, not just visually.

Vertical Writing Modes

In vertical writing modes, alignment follows the inline axis, which may run vertically rather than horizontally. Logical values are better suited to these layouts than assuming every text line runs from left to right.

Aligning Inline Content

The text-align property can align more than text. It also affects inline-level elements within a block container.

Centering an Inline Image

<div class="image-container">
  <img src="example.png" alt="Example graphic">
</div>
.image-container {
  text-align: center;
}

An image that remains inline-level is centered within the container.

Aligning Inline Links

.link-container {
  text-align: right;
}

Inline links inside the container align to the right.

Aligning Table Cell Content

th {
  text-align: left;
}
td.numeric {
  text-align: right;
}

Table cell content can be aligned independently by column or cell type.

Centering Inline Buttons

.button-container {
  text-align: center;
}

Inline-block buttons can be centered within a block container.

Block-Level Elements

The property does not center block-level children such as ordinary <div> elements. Use margins, Flexbox, or Grid to position those elements.

text-align vs. Other Properties

Several CSS properties control alignment, but they affect different parts of a layout.

Property Purpose
text-align Aligns inline content within a block container.
text-align-last Controls alignment of the last line and lines immediately before forced line breaks.
text-justify Controls aspects of spacing distribution for justified text.
vertical-align Controls vertical alignment of inline-level boxes and table-cell content.
margin-inline Can position a block element horizontally when appropriate auto margins are used.
justify-content Distributes items along the main axis of a flex container or the inline axis of a grid container.
align-items Controls alignment of items along the cross axis in Flexbox or within their grid areas in Grid.

Centering Text

.text-box {
  text-align: center;
}

Centering a Block Element

.content-box {
  width: 300px;
  max-width: 100%;
  margin-inline: auto;
}

Centering Flex Items

.flex-container {
  display: flex;
  justify-content: center;
}

Choose the alignment property based on whether you are aligning inline content, block elements, or layout items.

Practical Examples

Centered Page Heading

.page-heading {
  text-align: center;
}

Centers heading text within its block container.

Left-Aligned Paragraph

.article-text {
  text-align: left;
}

Aligns paragraph text to the physical left side.

Direction-Aware Paragraph

.article-text {
  text-align: start;
}

Aligns text to the beginning of the writing direction.

Right-Aligned Numeric Column

.data-table .numeric {
  text-align: right;
}

Aligns numbers along their right edges for easier comparison.

Centered Navigation Links

.navigation-links {
  text-align: center;
}
.navigation-links a {
  display: inline-block;
  margin-inline: 8px;
}

Centers a group of inline-block navigation links.

Justified Article Text

.article-content p {
  text-align: justify;
}

Justifies eligible lines of paragraph text.

Centered Table Headings

.data-table th {
  text-align: center;
}

Centers the content of table header cells.

Responsive Text Alignment

.responsive-heading {
  text-align: start;
}
@media (min-width: 768px) {
  .responsive-heading {
    text-align: center;
  }
}

Uses direction-aware alignment on smaller screens and centered alignment on larger screens.

End-Aligned Footer Content

.footer-links {
  text-align: end;
}

Aligns inline footer content to the logical end of the writing direction.

text-align Example

This example compares four common alignment values. Each paragraph uses the same text so the difference in alignment is easy to see.

HTML Example

<div class="text-align-demo">
  <div class="alignment-example">
    <h4>text-align: left</h4>
    <p class="align-left">CSS controls how text is positioned within a container. Alignment can change the appearance and readability of a paragraph.</p>
  </div>
  <div class="alignment-example">
    <h4>text-align: center</h4>
    <p class="align-center">CSS controls how text is positioned within a container. Alignment can change the appearance and readability of a paragraph.</p>
  </div>
  <div class="alignment-example">
    <h4>text-align: right</h4>
    <p class="align-right">CSS controls how text is positioned within a container. Alignment can change the appearance and readability of a paragraph.</p>
  </div>
  <div class="alignment-example">
    <h4>text-align: justify</h4>
    <p class="align-justify">CSS controls how text is positioned within a container. Alignment can change the appearance and readability of a paragraph.</p>
  </div>
</div>

CSS Example

.text-align-demo {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 12px;
}
.text-align-demo .alignment-example {
  padding: 12px;
  border: 1px solid #cbd5e1;
  background: #f8fafc;
}
.text-align-demo h4 {
  margin: 0 0 10px;
  font-size: 14px;
}
.text-align-demo p {
  margin: 0;
  padding: 12px;
  min-height: 110px;
  background: #fff;
  line-height: 1.6;
}
.text-align-demo .align-left {
  text-align: left;
}
.text-align-demo .align-center {
  text-align: center;
}
.text-align-demo .align-right {
  text-align: right;
}
.text-align-demo .align-justify {
  text-align: justify;
}
Browser Result

text-align: left

CSS controls how text is positioned within a container. Alignment can change the appearance and readability of a paragraph.

text-align: center

CSS controls how text is positioned within a container. Alignment can change the appearance and readability of a paragraph.

text-align: right

CSS controls how text is positioned within a container. Alignment can change the appearance and readability of a paragraph.

text-align: justify

CSS controls how text is positioned within a container. Alignment can change the appearance and readability of a paragraph.

The left-aligned paragraph begins at the left edge, the centered paragraph distributes unused space on both sides, and the right-aligned paragraph ends at the right edge. The justified paragraph adjusts spacing in eligible lines to align with both edges.

Comparing the Results

Value Result
left Text aligns to the left edge.
center Text is centered within each line box.
right Text aligns to the right edge.
justify Eligible lines stretch to align with both edges.

Changing to Logical Alignment

.text-align-demo .align-left {
  text-align: start;
}
.text-align-demo .align-right {
  text-align: end;
}

The first and third examples now follow the logical start and end of the writing direction.

Changing the Text Direction

.text-align-demo .alignment-example {
  direction: rtl;
}

Logical alignment values respond to the new direction, while physical values such as left and right continue to refer to their physical sides.

Centering the Last Line of Justified Text

.text-align-demo .align-justify {
  text-align: justify;
  text-align-last: center;
}

The final line of the justified paragraph is centered instead of following the default last-line alignment.

Accessibility Considerations

Text alignment can affect reading comfort, especially for long paragraphs and users who need consistent visual spacing.

Prefer Start Alignment for Body Text

For long passages, alignment to the beginning of the reading direction generally provides a consistent starting edge and makes lines easier to follow.

Use Center Alignment Selectively

Centered text works well for short headings or brief messages, but long centered paragraphs can be difficult to scan.

Be Careful With Justification

Justified text can introduce irregular spacing between words, particularly in narrow columns. This may make reading more difficult for some users.

Support Right-to-Left Languages

Use logical alignment values and appropriate HTML direction attributes when supporting languages with different writing directions.

Maintain Readable Line Lengths

Text alignment does not control line length. Use appropriate container widths to prevent excessively long lines.

Avoid Alignment Through Spaces

Do not insert repeated space characters to position text. Use CSS alignment properties instead.

Preserve Meaningful Reading Order

The text-align property changes visual alignment, not the underlying document order or semantic meaning.

Common Mistakes

Using middle Instead of center

p {
  text-align: middle;
}

The correct keyword for centering text is center.

Using top or bottom

p {
  text-align: top;
}

The property controls alignment along the inline axis, not vertical positioning in ordinary horizontal writing.

Trying to Center a Block Element

.container {
  text-align: center;
}

This centers inline content but does not center an ordinary block-level child. Use auto margins or a layout system to position the block.

Assuming left and start Are Always Identical

In horizontal right-to-left text, start aligns to the right, while left remains left-aligned.

Expecting justify to Stretch the Last Line

The justify value does not normally stretch the final line. Use text-align-last when a different last-line alignment is needed.

Using Justification in Narrow Columns

Very narrow columns can produce large gaps between words when text is justified.

Using Unsupported Values

p {
  text-align: spread;
}

The spread keyword is not a valid value.

Expecting Smooth Alignment Animation

The property uses discrete animation, so changes between alignment keywords do not interpolate smoothly.

Ignoring Inheritance

Descendant elements inherit text-align unless they specify another value.

Confusing text-align With justify-content

The text-align property aligns inline content within a block container. The justify-content property distributes layout items in Flexbox and Grid.

Important Considerations

Keep the following points in mind when using text-align:

  • It controls alignment of inline content within a block container.
  • The initial value is start.
  • The property is inherited.
  • It supports physical values such as left and right.
  • It supports logical values such as start and end.
  • The center value centers inline content.
  • The justify value adjusts spacing in eligible lines.
  • The final line is not normally stretched by justify.
  • The text-align-last property can control last-line alignment.
  • The text-justify property can influence justification behavior.
  • The match-parent value resolves alignment according to the parent's direction.
  • The justify-all value has limited browser support.
  • Logical values adapt to different writing directions.
  • Physical values remain tied to physical sides in horizontal writing.
  • The HTML dir attribute communicates text direction semantically.
  • Vertical writing modes can change the orientation of the inline axis.
  • The property affects inline images and inline-block elements.
  • It does not center ordinary block-level child elements.
  • It can align text within table cells.
  • It does not change the underlying document order.
  • It does not control vertical alignment in ordinary horizontal text.
  • It is discretely animated.
  • Justified text can create uneven word spacing.
  • Centered alignment is generally better suited to short text.
  • Start alignment is often preferable for longer passages.
  • Test alignment with different languages, viewport widths, and text lengths.

Browser Support

The CSS text-align property is widely supported in modern browsers, including Chrome, Edge, Firefox, and Safari.

Common values such as left, right, center, and justify have longstanding support. Logical values such as start and end are supported in modern browsers, while newer or less commonly implemented values may have compatibility limitations.

For current compatibility information, visit Can I Use? and search for text-align.

Feature Detection

@supports (text-align: start) {
  .direction-aware-text {
    text-align: start;
  }
}

Feature queries can test whether a browser recognizes a particular property value.

Fallback for Logical Alignment

.direction-aware-text {
  text-align: left;
  text-align: start;
}

Browsers that do not recognize start can use the preceding left declaration. This fallback is intended for left-to-right content.

Fallback Behavior

If a declaration is invalid or unsupported, the browser ignores it and uses another applicable declaration or the inherited alignment value.

Testing Recommendations

Test physical and logical alignment, justified text, right-to-left content, and last-line behavior in the browsers you support.

Specifications

The text-align property is defined in the CSS Text Module Level 3 specification.

CSS Text Module Level 3 - text-align Property

The specification defines alignment values, direction-aware behavior, justification, and interactions with related text alignment properties.

For additional documentation and examples, consult MDN Web Docs.

MDN Web Docs: text-align