codequick-darkmode-logo
Zaloguj sięZarejestruj się
  • css

Understanding Text Alignment in CSS

Aligning text properly is essential for creating visually appealing and readable web pages. Luckily, with CSS, you have several options for controlling the alignment of your text.

Text Alignment Properties

In CSS, you can use various text alignment properties to align your text. Here are the most commonly used properties:

  • text-align: left; - Aligns the text to the left edge of the containing element.
  • text-align: right; - Aligns the text to the right edge of the containing element.
  • text-align: center; - Centers the text horizontally within the containing element.
  • text-align: justify; - Adjusts the spacing between words in a paragraph to justify both the left and right edges of the containing element.

These properties can be applied to block-level and inline-level elements. Block-level elements, such as paragraphs and headings, occupy the full width of the containing element, while inline-level elements, such as links and span tags, only take up the space they require.

Examples

Let's take a look at some examples to better understand how text alignment works in CSS.

Example 1: Left Alignment

To align text to the left, you can use the text-align: left; property. Here's an example:

<p style="text-align: left;"> This is a paragraph with left-aligned text. </p>

Result: This is a paragraph with left-aligned text.

Example 2: Right Alignment

To align text to the right, you can use the text-align: right; property. Here's an example:

<p style="text-align: right;"> This is a paragraph with right-aligned text. </p>

Result: This is a paragraph with right-aligned text.

Example 3: Center Alignment

To center align text, you can use the text-align: center; property. Here's an example:

<p style="text-align: center;"> This is a paragraph with center-aligned text. </p>

Result: This is a paragraph with center-aligned text.

Example 4: Justified Alignment

To justify text, you can use the text-align: justify; property. Here's an example:

<p style="text-align: justify;"> This is a paragraph with justified text. This will adjust the spacing between words to align both the left and right edges of the text container. </p>

Result: This is a paragraph with justified text. This will adjust the spacing between words to align both the left and right edges of the text container.

Conclusion

Text alignment in CSS is a powerful technique that helps you create visually balanced and readable layouts. By using the text-align property, you can easily align text to the left, right, center, or justify it. Experiment with these properties to find the best alignment for your web content! For more information on CSS text alignment, check out the following resources: