Contents
How to align text in HTML
To define the horizontal alignment of the text within an HTML element, you can use the CSS text-align
property:
The CSS text-align syntax
To apply the text-align
property, you need to define one out of six available values:
text-align: value;
Check out an example of using CSS text-align
below:
h1 {
text-align: center;
}
h2 {
text-align: right;
}
h3 {
text-align: left;
}
All the available properties for using text-align
are explained in the table below:
Value | Description |
---|---|
center |
The default value. Makes CSS center text in an element |
left |
Makes CSS align text along the left edge of the element |
right |
Makes CSS align text along the right edge of the element |
justify |
Makes CSS justify text: the first and the last words are aligned to the edges of the element, and the content is spaced to fill the line |
start |
Works like left when the text direction is left-to-right and like right when it's right to left |
end |
Works like right when the text direction is left-to-right and like left when it's right to left |
Using text-align-last
You can also use the text-align-last
property to define how the last line of the text should be aligned:
As with text-align
, you only need to define one value. You can use all the values available to text-align
, plus the auto
keyword which is also the default value. Using auto
makes CSS align the last line in the same manner the whole text is aligned.
Note: if
text-align
is set tojustify
,auto
will behave asstart
.