Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

CSS Properties

CSS text-align Property

Posted in CSS Tutorial
Updated on Jul 19, 2024
By Mari Selvan
👁️ 16 - Views
⏳ 4 mins
💬 1 Comment
CSS text-align Property

Photo Credit to CodeToFun

🙋 Introduction

The text-align property in CSS is used to set the horizontal alignment of text within an element. This property is commonly applied to block-level elements like paragraphs and headings to control the alignment of their text content.

The text-align property can take several values, allowing for versatile text alignment options that enhance the readability and visual appeal of your web pages.

💡 Syntax

The syntax for the text-align property is simple. It is applied to an element using the following format:

Syntax
Copied
Copy To Clipboard
element {
  text-align: value;
}

Here, value can be one of the predefined keywords that specify the alignment.

🎛️ Default Value

The default value of the text-align property is left. This means that, unless otherwise specified, text within block-level elements is aligned to the left.

🏠 Property Values

ValueDescription
leftAligns the text to the left.
rightAligns the text to the right.
centerCenters the text.
justifyStretches the lines of text so that each line has equal width, and the text aligns evenly on both the left and right sides.
startAligns the text to the start of the block-level container (left in left-to-right text, right in right-to-left text).
endAligns the text to the end of the block-level container (right in left-to-right text, left in right-to-left text).

📄 Example

In this example, we'll demonstrate how to center-align and justify text within a paragraph.

index.html
Copied
Copy To Clipboard
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS text-align Example</title>
  <style>
    .center {
      text-align: center;
    }
    .justify {
      text-align: justify;
    }
  </style>
</head>
<body>
  <h1>Text Alignment Examples</h1>
  <p class="center">This paragraph is center-aligned.</p>
  <p class="justify">This paragraph is justified. Justification stretches the lines so that each line has equal width, aligning the text evenly on both the left and right sides.</p>
</body>
</html>

🖥️ Browser Compatibility

The text-align property is widely supported across all major browsers, including Chrome, Firefox, Safari, Edge, and Opera. It is a well-established property in CSS, ensuring consistent behavior across different browsing environments.

🎉 Conclusion

The text-align property is a fundamental tool for web developers to control the alignment of text within block-level elements.

Whether you need left, right, center, or justified text alignment, this property offers the flexibility to create visually appealing and readable content. Experiment with different alignment options to see how they can enhance the layout and design of your web pages.

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy