HTML & CSSEasy
What is text-align?
text-align sets the horizontal alignment of inline content (text, inline and inline-block elements) inside a block container. Values include left, right, center, justify, and the direction-aware start and end, which flip automatically for right-to-left languages.
Values
start(default) /end: follow the writing direction; prefer them overleft/rightfor internationalized sites.left,right,center.justify: stretches lines so both edges are flush. It can create uneven "rivers" of space in narrow columns; pair it withhyphens: autoand alangattribute.match-parent: likeinherit, but resolvesstart/endagainst the parent's direction.text-align-lastcontrols the final line of justified text.
What it does not do
- It does not center a block element itself; it aligns the content inside it. To center a block, use
margin-inline: auto(with a width) or a flex/grid parent. - It has no effect on vertical alignment; use
align-itemsin flex/grid orvertical-alignfor inline/table cells.
Common patterns
- Centering a button or image inside a block:
text-align: centeron the parent works because they are inline-level. - Numeric table columns:
text-align: end(plusfont-variant-numeric: tabular-nums). - Long body text is easiest to read left-aligned (start-aligned); centered text works only for short headings.
Interview tip
Mention logical values (start/end) and the difference between aligning content and centering the box itself.
Code example
html
1<style>2 body { font-family: system-ui; max-width: 420px; }3 p { border: 2px solid #0b1020; padding: 8px; }4 .start { text-align: start; }5 .center { text-align: center; }6 .end { text-align: end; }7 .justify { text-align: justify; hyphens: auto; }8 .box { width: 200px; background: #dbeafe; text-align: center; }9 .centered-block { margin-inline: auto; } /* centers the box itself */10</style>11<p class="start">start (left in English)</p>12<p class="end" dir="rtl">end in a right-to-left paragraph: النهاية</p>13<p class="center">center <button>inline button centered too</button></p>14<p class="justify" lang="en">Justified text stretches every line except the last so that both the left and right edges line up neatly, like a newspaper column.</p>15<p class="box">text centered, box not centered</p>16<p class="box centered-block">text and box both centered</p>
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the HTML & CSS set.