Introduction to text-align in CSS π
The text-align property controls the horizontal alignment of inline content (like text) inside a block-level element. Itβs one of the simplest yet most frequently used CSS properties β perfect for headings, paragraphs, navigation items, and more! π―
Why Use text-align? π€
- To center headings or banners π―
- To align paragraphs for better readability π
- To right-align numbers or UI text π’
- To create flexible and elegant layouts β¨
Syntax π
Basic Syntax
selector {
text-align: left | right | center | justify | start | end;
}Note
text-align affects inline-level content inside a block container.Available Values π§©
1. left (Default in LTR languages) β¬ οΈ
Aligns text to the left edge.
2. right β‘οΈ
Aligns text to the right edge. Useful for UI labels or numbers.
3. center π―
Centers the inline content inside the container.
4. justify π
Stretches each line so text touches both left and right sides β commonly used in books and newspapers.
5. start βοΈ
Aligns text according to writing direction: left for LTR, right for RTL.
6. end βοΈ
Opposite of start, aligns text based on writing direction.
Examples π₯
1. Basic Alignment
Left, Right, Center
h1 {
text-align: center;
}
p {
text-align: left;
}
.price {
text-align: right;
}2. Justified Paragraph
Justified text
.article {
text-align: justify;
}3. Direction-aware Alignment π
Start & End
.dynamic {
text-align: start;
}Comparison Table π
| Value | Description |
|---|---|
| left | Aligns text to the left |
| right | Aligns text to the right |
| center | Centers text |
| justify | Stretches text to fill width |
| start | Aligns based on writing mode (LTR/RTL) |
| end | Opposite of start |
Visual Example πΌοΈ
Hereβs a visual placeholder showing different text alignments:
Further details can be found at: https://dummyimage.com/800x250/eeeeee/000000&text=Left+|+Center+|+Right+|+Justify
Conclusion π
The text-align property is a simple yet powerful tool for controlling text alignment in CSS. Whether youβre crafting clean UI layouts or designing readable article content, mastering text alignment will elevate your typography and design skills. Start experimenting with it β especially center and justify β to bring balance and beauty to your layouts! π