CSS text-align Property

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

To center the element, the margin property can be used.

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

Initial Value left if direction is "ltr", right if direction is "rtl"
Applies to Block containers.
Inherited Yes
Animatable No.
Version CSS1
DOM Syntax object.style.textAlign = "right";

Syntax

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

Example

<!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>

An example which contains "center", "left" and "justify" values:

Example

<!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 has been the industry's standard dummy text ever since the 1500s when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
  </body>
</html>

Property Values

Value Descriptions
left Aligns the text to the left. This is the default value of this property.
right Aligns the text to the right.
center Aligns the text to the center.
justify Extends the lines so that each line has equal width.
initial It makes the property use its default value.
inherit It inherits the property from its parents element.





Related articles