In CSS, alignment refers to the positioning of elements on a webpage relative to their containing parent or other elements. CSS offers various properties that allow you to control alignment in different ways. Let's explore some of the most commonly used CSS alignment properties with examples:

  1. text-align property: This property is used to align the text content within a block-level element. It has four possible values:
  • left: Aligns the text to the left.
  • right: Aligns the text to the right.
  • center: Centers the text horizontally.
  • justify: Adjusts the spacing between words to stretch the lines, making the text fit within the width of the container.

Example:

<!DOCTYPE html>
<html>
<head>
  <title>Text Alignment Example</title>
  <style>
    .container {
      width: 300px;
      border: 1px solid black;
      padding: 10px;
    }
    .center-align {
      text-align: center;
    }
  </style>
</head>
<body>
  <div class="container">
    <p>This is a left-aligned paragraph.</p>
    <p class="center-align">This is a center-aligned paragraph.</p>
    <p style="text-align: right;">This is a right-aligned paragraph.</p>
    <p style="text-align: justify;">This is a justified paragraph. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
  </div>
</body>
</html>

 

2. vertical-align property: This property is used to vertically align inline or inline-block elements relative to their containing parent. It has several values, but the most commonly used are:

  • top: Aligns the element to the top of the line.
  • middle: Aligns the element to the middle of the line.
  • bottom: Aligns the element to the bottom of the line.

Example:

<!DOCTYPE html>
<html>
<head>
  <title>Vertical Alignment Example</title>
  <style>
    .container {
      height: 150px;
      border: 1px solid black;
    }
    .content {
      display: inline-block;
      vertical-align: middle;
    }
  </style>
</head>
<body>
  <div class="container">
    <span class="content">This is vertically aligned in the middle.</span>
  </div>
</body>
</html>

 

3. margin property: While not strictly an alignment property, the margin property can be used to center block-level elements horizontally. By setting the left and right margins to "auto," the element will be horizontally centered within its containing parent. 

Example:

<!DOCTYPE html>
<html>
<head>
  <title>Horizontal Centering Example</title>
  <style>
    .container {
      width: 300px;
      height: 100px;
      border: 1px solid black;
    }
    .centered {
      width: 150px;
      height: 50px;
      background-color: lightblue;
      margin: 0 auto;
    }
  </style>
</head>
<body>
  <div class="container">
    <div class="centered"></div>
  </div>
</body>
</html>

 

4. justify property: is particularly common for blocks of text in paragraphs and can provide a polished and professional appearance to the content on a webpage.

Example: 

<!DOCTYPE html>
<html>
<head>
  <title>Justify Text Example</title>
  <style>
    .container {
      width: 300px;
      border: 1px solid black;
      padding: 10px;
    }
    .justified-text {
      text-align: justify;
    }
  </style>
</head>
<body>
  <div class="container">
    <p class="justified-text">
      This is an example of justified text. The text-align property with the value "justify" adjusts the spacing between words to make each line of text fit within the width of the container. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed vitae luctus odio. Donec varius risus vel mauris sollicitudin interdum. Etiam venenatis tristique quam, ut aliquet velit fringilla in. Duis nec justo ac urna facilisis mattis. Nulla facilisi. Aenean tincidunt faucibus lacus, eu ultrices eros dapibus quis. Vestibulum nec dui eu quam pharetra pharetra eu eget tellus. Fusce lacinia, felis eget tincidunt sodales, sapien eros interdum odio, id tincidunt odio est a nulla. Donec facilisis odio eu elit iaculis semper. Integer vel orci at nisi fringilla bibendum a vitae mauris. In hac habitasse platea dictumst.
    </p>
  </div>
</body>
</html>

 

Latest Tutorial

Newly published tutorials

Laravel 10 - Send mail using AWS SES

Learn to send Email using Laravel 10 with AWS SES service.

Laravel - How to upload file to AWS S3 Bucket

Explore how we can upload file using the laravel on Amazon S3 bucket.

Laravel 10 - Tutorial on how to use jQuery to validate the form

Learn how to use jQuery in Laravel 10 to validate the form