text-align

Term side of the Card.
Topic: Text Handling
Unit title: text-align
Categories: Property
Explanation side of the Card

Explanation: The property that sets the horizontal alignment of text within an element. It can be aligned to the left, right, center or justified, controlling how text flows in relation to the container's edges, making it crucial for creating visually balanced typography.

Browser compatibility:
  • The chrome browser supports this feature starting from version 1.
  • The edge browser supports this feature starting from version 12.
  • The firefox browser supports this feature starting from version 1.
  • The safari browser supports this feature starting from version 1.
Availability: 

Example

Wrongcss
.card {
  width: 300px;
  text-align: center;
}
Rightcss
.card {
  width: 300px;
  margin-inline: auto;
  text-align: center;
}

text-align centers only the text inside; margin-inline: auto centers the card block itself.

Common mistakes

  • text-align: center doesn't center a fixed-width block, only its inline content.
  • Text aligned with left stays on the wrong side in Arabic or Hebrew pages.

Key points

  • Alignment uses logical values start and end, so text flips correctly in right-to-left languages.
  • Blocks are centered with margin-inline: auto or flex/grid, not text-align: center on the parent.
  • Long body text is not justify aligned without hyphens, which leaves uneven word gaps.

Interview questions

What is the difference between text-align: left and start?

left always aligns left. start aligns to the start of the line, which is the right side in right-to-left text.

How do I center text vertically?

text-align is horizontal only. Use flexbox or grid with align-items: center for vertical centering.

Is justified text bad for accessibility?

It can be: uneven word gaps make text harder to read for some people, so left or start alignment is safer for body text.

Analytics cookiesWe'd like to use analytics cookies to see which pages are useful. They're set only if you accept. Privacy Policy