Align the text of a document with CSS

The text-align property in CSS is used to align the text content within an element. It accepts several values: left (default), right, center, and justify.

Syntax

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

Values

  • left: Aligns text to the left edge (default)
  • right: Aligns text to the right edge
  • center: Centers the text horizontally
  • justify: Spreads text evenly across the width, aligning both left and right edges

Example

Here's how to use different text alignment values:

<html>
    <head>
        <style>
            .left-align { text-align: left; }
            .right-align { text-align: right; }
            .center-align { text-align: center; }
            .justify-align { text-align: justify; }
        </style>
    </head>
    <body>
        <p class="left-align">
            This text is aligned to the left side of the container.
        </p>
        
        <p class="right-align">
            This text is aligned to the right side of the container.
        </p>
        
        <p class="center-align">
            This text is centered within the container.
        </p>
        
        <p class="justify-align">
            This text is justified, which means it spreads evenly across the full width of the container, creating straight edges on both left and right sides.
        </p>
    </body>
</html>

Inline Styling Example

You can also apply text alignment using inline styles:

<html>
    <head>
    </head>
    <body>
        <p style="text-align: right;">
            Asia is a continent aligned to the right.
        </p>
        
        <p style="text-align: center;">
            Asia is a continent centered on the page.
        </p>
        
        <p style="text-align: justify;">
            Asia is the largest continent on Earth, covering about 30% of the world's total land area and home to over 4.6 billion people.
        </p>
    </body>
</html>

Browser Compatibility

The text-align property is supported by all modern browsers and has been part of CSS since CSS1, making it universally compatible.

Conclusion

The text-align property provides simple yet effective control over text positioning within containers. Use center for headings, justify for paragraphs, and left or right based on your design needs.

Updated on: 2026-03-15T23:18:59+05:30

97 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements