The text-align
property in CSS is used to specify the horizontal alignment of text within its container. This property can be applied to block-level elements to control how the inline content, such as text and images, is aligned. It offers several values to left-align, right-align, center, or justify the text.
Example 1: Aligning Text to the Left
/* This CSS rule targets a paragraph element with the class "left-align" */
.left-align {
text-align: left; /* Aligns the text to the left side of its container */
border: 1px solid #ccc; /* Adds a border for visual clarity */
padding: 10px; /* Adds some space inside the border */
}
Explanation: The text-align: left;
declaration forces the text inside any element with the class .left-align
to be aligned to the left edge of its parent container. This is the default alignment for text in most web browsers for left-to-right languages.
Example 2: Aligning Text to the Right
/* This rule applies to any paragraph with the class "right-align" */
.right-align {
text-align: right; /* Aligns the text to the right side of its container */
border: 1px solid #ccc; /* Visual border to show the container's boundaries */
padding: 10px; /* Provides internal spacing */
}
Explanation: By setting text-align: right;
, the text within the .right-align
class is pushed to the right boundary of its container. This is often used for stylistic effect or for languages that are read from right to left.
Example 3: Centering Text
/* This CSS targets paragraphs with the class "center-align" */
.center-align {
text-align: center; /* Centers the text horizontally */
border: 1px solid #ccc; /* A border to visualize the element's box */
padding: 10px; /* Inner spacing for better readability */
}
Explanation: The text-align: center;
value horizontally centers the text within its containing element. This is a very common technique for headings and titles to create a symmetrical and focused look.
Example 4: Justifying Text
/* This rule is for paragraph elements with the class "justify-align" */
.justify-align {
text-align: justify; /* Stretches the lines so that each line has equal width */
border: 1px solid #ccc; /* Border to illustrate the container's edges */
padding: 10px; /* Padding for spacing */
}
Explanation: text-align: justify;
makes the text spread out to fill the container's width, so both the left and right edges of the text are aligned. This creates a clean, block-like appearance for paragraphs of text.
Example 5: Using text-align
with start
/* This CSS rule targets a div with the class "start-align" */
.start-align {
text-align: start; /* Aligns text to the start based on writing direction */
direction: rtl; /* Sets the writing direction to right-to-left for this example */
border: 1px solid #ccc; /* Visual aid to see the container */
padding: 10px; /* Spacing inside the container */
}
Explanation: The text-align: start;
value is direction-sensitive. For left-to-right languages, it behaves like left
, and for right-to-left languages (as simulated here with direction: rtl;
), it behaves like right
.
Example 6: Using text-align
with end
/* This rule applies to any div with the class "end-align" */
.end-align {
text-align: end; /* Aligns text to the end based on writing direction */
direction: ltr; /* Sets the writing direction to left-to-right */
border: 1px solid #ccc; /* Border for visualization */
padding: 10px; /* Internal spacing */
}
Explanation: Similar to start
, text-align: end;
is also direction-sensitive. In a left-to-right context, it aligns text to the right. If the text direction were right-to-left, it would align the text to the left.
Example 7: Aligning an Image with text-align
/* This CSS rule targets a div with the class "image-align" */
.image-align {
text-align: center; /* Centers inline-level content, including images */
border: 1px solid #ccc; /* A border to show the div's boundaries */
padding: 10px; /* Adds some space around the image */
}
Explanation: The text-align
property isn't just for text. It can align any inline-level element within a block container. In this case, text-align: center;
is used to horizontally center an image within a div
.