Skip to content

CSS text-align Property

The text-align property is used for aligning the inner content of a block element.

You can use the margin property to center the element.

INFO

Alignment specified with text-align is not with respect to the containing block or viewport.

Initial Valueleft if direction is "ltr", right if direction is "rtl"
Applies toBlock containers.
InheritedYes
AnimatableNo.
VersionCSS1
DOM Syntaxobject.style.textAlign = "right";

Syntax

Syntax of CSS text-align Property

css
text-align: left | right | center | justify | initial | inherit;

Example of the text-align property with the "right" and "center" values:

Example of CSS text-align Property with right and center values

html
<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      div {
        text-align: right;
      }
      p {
        text-align: center;
      }
    </style>
  </head>
  <body>
    <h2>Text-align property example</h2>
    <div>Example for the text-align property.</div>
    <p>Some paragraph for example.</p>
  </body>
</html>

Result

CSS text-align Property

Example of the text-align property with the "center", "left" and "justify" values:

Example of CSS text-align Property with left, center and justify values

html
<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      h2 {
        text-align: center;
      }
      p.date {
        text-align: left;
      }
      p.example {
        text-align: justify;
      }
    </style>
  </head>
  <body>
    <h2>Text-align property example</h2>
    <p class="date">March, 2019</p>
    <p class="example">
      Lorem Ipsum is dummy text of the printing and typesetting industry. Lorem Ipsum h as been the industry's standard dummy text ever since the 1500s when an unknown pri nter took a galley of type and scrambled it to make a type specimen book. It has surviv ed not only five centuries, but also the leap into electronic typesetting, remaining essent ially unchanged. It was popularised in the 1960s with the release of Letraset sheets conta ining Lorem Ipsum passages, and more recently with desktop publishing software like Ald us PageMaker including versions of Lorem Ipsum.
    </p>
  </body>
</html>

Values

ValueDescriptionsPlay it
leftAligns the text to the left. This is the default value of this property.Play it »
rightAligns the text to the right.Play it »
centerAligns the text to the center.Play it »
justifyExtends the lines so that each line has equal width.Play it »
initialIt makes the property use its default value.Play it »
inheritIt inherits the property from its parents element.

Practice

Which of the following are valid property values for the 'text-align' property in CSS?

Dual-run preview — compare with live Symfony routes.