The Wayback Machine - https://web.archive.org/web/20250108155822/https://www.geeksforgeeks.org/css-text-align-property/
Open In App

CSS text-align Property

Last Updated : 28 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The text-align property in CSS controls the horizontal alignment of inline content (such as text) and inline-block elements within their containing block-level element.

Syntax:

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

Default Value: left if the direction is ltr, and right if the direction is rtl

Property Value:

  • left: It is used to set the text alignment to the left. This is the default property.
  • right: It is used to set the text-alignment to right.
  • center: It is used to set the text alignment into the center.
  • justify: It is used to spread the words into the complete line i.e., by stretching the content of an element.
  • initial: It is used to set an element’s CSS property to its default value.
  • inherit: It is used to inherit a property to an element from its parent element property value.

Please refer to the CSS Align article for further details.

Example: This example illustrates the use of the text-align property, to align it to specified values.

HTML
<!DOCTYPE html>
<html>
<head>
    <title>text-align property</title>
    <style>
    h1 {
        color: green;
    }
    
    .main {
        border: 1px solid black;
    }
    
    .gfg1 {
        text-align: left;
    }
    
    .gfg2 {
        text-align: right;
        ;
    }
    
    .gfg3 {
        text-align: center;
    }
    
    .gfg4 {
        text-align: justify;
    }
    </style>
</head>

<body>
    <h1>GeeksforGeeks</h1>
    <h2>text-align property</h2>
    <div class="main">
        <h3>text-align: left;</h3>
        <div class="gfg1"> 
        The course is designed for students 
        as well as working professionals to 
        prepare for coding interviews. This 
        course is going to have coding questions 
        from school level to the level needed 
        for product based companies like Amazon, 
        Microsoft, Adobe, etc. 
        </div>
    </div>
    <br>
    <div class="main">
        <h3 style="text-align: right;">text-align: right;</h3>
        <div class="gfg2"> 
        The course is designed for students 
        as well as working professionals to 
        prepare for coding interviews. This 
        course is going to have coding questions 
        from school level to the level needed 
        for product based companies like Amazon, 
        Microsoft, Adobe, etc. 
        </div>
    </div>
    <br>
    <div class="main">
        <h3 style="text-align: center;">text-align: center;</h3>
        <div class="gfg3"> 
        The course is designed for students 
        as well as working professionals to 
        prepare for coding interviews. This 
        course is going to have coding questions 
        from school level to the level needed for 
        product based companies like Amazon, 
        Microsoft, Adobe, etc. 
        </div>
    </div>
    <br>
    <div class="main">
        <h3 style="text-align: justify;">text-align: justify;</h3>
        <div class="gfg4"> 
        The course is designed for students 
        as well as working professionals to 
        prepare for coding interviews. This 
        course is going to have coding questions 
        from school level to the level needed 
        for product based companies like Amazon, 
        Microsoft, Adobe, etc. 
        </div>
    </div>
</body>
</html>

Output: 

Supported Browsers: The browser supported by the text-align property are listed below: 

  • Google Chrome 1.0
  • Internet Explorer 3.0
  • Microsoft Edge 12.0
  • Firefox 1.0
  • Opera 3.5
  • Safari 1.0

CSS text-align Property – FAQs

What is the text-align property in CSS?

The text-align property in CSS sets the horizontal alignment of text within an element. It is commonly used to align text to the left, right, center, or justify it across the width of the containing element.

How do I center-align text using CSS?

To center-align text, use: text-align: center;. This centers the text within its containing block.

Can text-align be used with block-level elements?

Yes, text-align can be applied to block-level elements like <div>, <p>, or <h1> to control the alignment of inline content within those blocks.

What is the difference between text-align: justify and text-align: left?

Text-align: justify adjusts the spacing between words so that each line is stretched to align evenly along both the left and right edges, while text-align: left aligns text to the left margin.

Does text-align affect the alignment of block elements?

No, text-align affects only inline-level content or text within block elements. To align block elements themselves, other properties like margin or display should be used.



Next Article

Similar Reads

three90RightbarBannerImg