How to Align the text in CSS

Aligning text in CSS is a relatively simple task, but it can be tricky if you’re not familiar with the different properties and values that are used to control text alignment.

In this CSS tutorial, we’ll explore the various ways you can align text using CSS and provide code examples for each method.

Introduction to Text Alignment in CSS

CSS, or Cascading Style Sheets, is a powerful tool for controlling the layout and appearance of text on a website.

One of the most basic elements of text layout is alignment, which determines how the text is positioned within a given area.

Whether you’re creating a simple blog post or a complex web application, understanding how to align text in CSS is essential for creating a polished and professional-looking website.


The text-align Property

The most basic way to align text in CSS is to use the text-align property.

This property can be applied to any element that contains text, and it controls the horizontal alignment of the text within that element.

The text-align property accepts several different values, including left, center, right, and justify.

Here’s an example of how to use the text-align property to center the text within a paragraph element:

p {
  text-align: center;
}

In this example, all text within the <p> elements will be centered on the page.

You can also use the text-align property to align text to the left or right by setting the value to left or right.

The text-align-last Property

The text-align-last property is used to align the last line of a block-level element.

It is similar to the text-align property, but it only affects the last line of the element.

The text-align-last property is useful when you have a long text and you want to align the last line differently from the rest of the text.

Here’s an example of how to use the text-align-last property to align the last line of a paragraph to the right:

p {
  text-align: justify;
  text-align-last: right;
}

In this example, the text within the <p> elements will be justified, except for the last line, which will be aligned to the right.

The vertical-align Property

The vertical-align property is used to control the vertical alignment of text within an element.

This property is often used in conjunction with the display property to control the alignment of inline elements such as images and text.

The vertical-align property accepts several different values, including top, middle, and bottom.

Here’s an example of how to use the vertical-align property to center an image within a paragraph:

img {
  display: inline-block;
  vertical-align: middle;
}

In this example, all images within the <img> elements will be vertically aligned in the middle of the text in the paragraph.


Conclusion

In this post, we’ve covered the basics of text alignment in CSS, including the text-align, text-align-last, and vertical-align properties.

We’ve also provided code examples for each method to help you better understand how to use these properties to control the alignment of text on your website.

With a solid understanding of text alignment in CSS, you’ll be able to create professional-looking websites that are easy to read and navigate.