left and right align text against the corresponding edge of its container, center centers each line horizontally within the available width, and justify stretches each line, except typically the last, so that both the left and right edges align evenly, achieved by adjusting the spacing between words. text-align only affects inline-level content, like text and inline elements, within the block it's applied to — it has no effect on positioning the block-level element itself within its own parent, which is instead controlled by properties like margin: auto or flexbox/grid alignment properties.
1Understanding Text-align
left and right align text against the corresponding edge of its container, center centers each line horizontally within the available width, and justify stretches each line, except typically the last, so that both the left and right edges align evenly, achieved by adjusting the spacing between words. text-align only affects inline-level content, like text and inline elements, within the block it's applied to — it has no effect on positioning the block-level element itself within its own parent, which is instead controlled by properties like margin: auto or flexbox/grid alignment properties.
text-align only centers or aligns inline content, like the text inside a box — it does NOT center the box itself within its own parent container, a common point of confusion; use margin: auto or a flexbox/grid alignment property to actually center the block element itself.
.title {
text-align: center;
}2Practical Example
Here is a real-world application of Text-align showing how it is used in production CSS code.
.box {
width: 200px;
margin: 0 auto;
text-align: center;
}
<div class="box">Text</div>3Best Practices
Follow these guidelines when working with Text-align:
1. Use text-align specifically to align the inline content, like text, within a block, not to position the block element itself within its parent
2. Use margin: auto, for a block with a defined width, or flexbox/grid alignment properties to center the block element itself, rather than expecting text-align to do that job
3. Use justify sparingly for body text, since evenly stretching word spacing to fill each line can occasionally create noticeably uneven, distracting gaps, especially in narrow columns
Tip: text-align only centers or aligns inline content, like the text inside a box — it does NOT center the box itself within its own parent container, a common point of confusion; use margin: auto or a flexbox/grid alignment property to actually center the block element itself.
.title {
text-align: center;
}