Search
Close this search box.
Course Content
CSS Basic
0/1
CSS Selectors
0/1
CSS Comments
0/1
CSS Backgrounds
0/1
CSS Borders
0/1
CSS Outline
0/1
CSS Fonts
0/1
CSS Height and Width
0/1
CSS Margins and Paddings
0/2
CSS Icons
0/1
CSS Links
0/1
CSS Lists
0/1
CSS Tables
0/1
CSS Display Properties
0/1
CSS Max-Width Property
0/1
CSS Positioning Elements
0/1
CSS Z-Index Property
0/1
CSS Overflow
0/1
CSS Float
0/1
CSS Opacity
0/1
CSS Forms
0/1
CSS Dropdowns
0/1
CSS Buttons
0/1
CSS Media Queries
0/1
About Lesson

Text Alignment (text-align):

Definition:

The text-align property in CSS is used to set the horizontal alignment of text content within a block-level element or table cell. It specifies whether the text should be aligned to the left, right, center, or justified within its containing element.

Syntax:

selector {
text-align: value;
}

  • selector: The HTML element or class to which the styling is applied.
  • value: The alignment value, which can be one of the following: left, right, center, or justify.

Example:

<!DOCTYPE html>
<html lang=”en”>
<head>
  <meta charset=”UTF-8″>
  <meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
  <style>
    .center-text {
      text-align: center;
    }
    .right-text {
      text-align: right;
    }
    .justified-text {
      text-align: justify;
    }
  </style>
  <title>Text Alignment Example</title>
</head>
<body>
  <div class=”center-text”>
    <p>This text is centered.</p>
  </div>
  <div class=”right-text”>
    <p>This text is aligned to the right.</p>
  </div>
  <div class=”justified-text”>
    <p>This text is justified, meaning it spreads evenly across the entire width of the container.</p>
  </div>
</body>
</html>

Output:

Explanation:

  1. HTML Structure:

    • Create a basic HTML file with three div elements, each having a different class (center-text, right-text, justified-text).
  2. CSS Styles:

    • Use the text-align property to set the horizontal alignment of the text within each div.
      • .center-text: Text is centered.
      • .right-text: Text is aligned to the right.
      • .justified-text: Text is justified, spreading evenly across the container.