CSS Properties
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:
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
Value | Description |
---|---|
left | Aligns the text to the left. |
right | Aligns the text to the right. |
center | Centers the text. |
justify | Stretches the lines of text so that each line has equal width, and the text aligns evenly on both the left and right sides. |
start | Aligns the text to the start of the block-level container (left in left-to-right text, right in right-to-left text). |
end | Aligns 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.
<!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:
Author
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
If you have any doubts regarding this article (CSS text-align Property), please comment here. I will help you immediately.