text-align
Summary
The text-align CSS property describes how inline content like text is aligned in its parent block element. text-align does not control the alignment of block elements itself, only their inline content.
Overview table
- Initial value
start, or a nameless value that acts as left if direction is ltr, right if direction is rtl, if start is not supported by the browser.
- Applies to
- block containers
- Inherited
- Yes
- Media
- visual
- Computed value
- as specified, except for the match-parent value which is calculated against its parent’s direction value and results in a computed value of either left or right
- Animatable
- No
- CSS Object Model Property
textAlign
Syntax
text-align: <string>
text-align: center
text-align: end
text-align: justify
text-align: left
text-align: match-parent
text-align: right
text-align: start
text-align: start end
Values
- start
- The same as
left
if direction is left-to-right andright
if direction is right-to-left. - end
- The same as
right
if direction is left-to-right andleft
if direction is right-to-left. - left
- The inline contents are aligned to the left edge of the line box.
- right
- The inline contents are aligned to the right edge of the line box.
- center
- The inline contents are centered within the line box.
- <string>
- The first occurrence of the one-char string is the element used for alignment. the keyword that follows or precedes it indicates how it is aligned. This allows to align numeric values on the decimal point, for instance.
- justify
- The text is justified. Text should line up their left and right edges to the left and right content edges of the paragraph.
- match-parent
- Similar to inherit with the difference that the value
start
andend
are calculated according the parent’s direction and are replaced by the adequateleft
orright
value. - start end
- Specifies
start
alignment of the first line and any line immediately after a forced line break; andend
alignment of any remaining lines not affected by text-align-last.
Examples
This just shows the four possible types of text-alignment.
<p class="left"> This paragraph is aligned to the left. </p>
<p class="centered"> This paragraph is centered. </p>
<p class="right"> This paragraph is aligned to the right. </p>
<p class="justified">This paragraph needs to be really long in order to show how to justify text. It only works because we set a width for this paragraph though.</p>
.left { text-align: left;}
.cenetered{ text-align: center;}
.right { text-align: right;}
.justified { width: 200px; text-align: justify;}
Notes
The standard-compatible way to center a block itself without centering its inline content is setting the left and right margin
to auto, e.g.: margin:auto;
or margin:0 auto;
or margin-left:auto; margin-right:auto;
Related specifications
- CSS Text Level 3
- Working Draft
See also
Other articles
- CSSStyleDeclarationCSSStyleDeclaration
- currentStylecurrentStyle
- runtimeStyleruntimeStyle
- stylestyle