Introduction
Text alignment helps you control how text lines up inside a box or container. It makes your content look neat and easy to read.
Jump into concepts and practice - no test required
selector {
text-align: value;
}p {
text-align: left;
}h1 {
text-align: center;
}.price { text-align: right; }
article {
text-align: justify;
}<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Text Alignment Example</title> <style> body { font-family: Arial, sans-serif; margin: 2rem; } header { text-align: center; margin-bottom: 1rem; } p.left { text-align: left; background-color: #e0f7fa; padding: 0.5rem; } p.right { text-align: right; background-color: #ffe0b2; padding: 0.5rem; } p.justify { text-align: justify; background-color: #c8e6c9; padding: 0.5rem; } </style> </head> <body> <header> <h1>Text Alignment Demo</h1> </header> <p class="left">This paragraph is aligned to the left. It looks like normal reading text starting from the left side.</p> <p class="right">This paragraph is aligned to the right. It lines up on the right side of the container.</p> <p class="justify">This paragraph is justified. The text stretches so both the left and right edges line up evenly, making it look tidy like in books or newspapers.</p> </body> </html>
text-align: center; do to the text inside an element?text-align propertycentercenter places the text in the middle horizontally.text-align is the property, and right is the value. The syntax uses a colon and semicolon.<div> appear?div {
text-align: justify;
width: 300px;
}text-align: justify;header {
text-align center;
}text-align center; missing the colon after text-align.<section> so that the first line is centered, but all other lines are left aligned. Which CSS approach achieves this?::first-line pseudo-element::first-line.text-align: left; on the section and text-align: center; on the first line with ::first-line pseudo-element correctly applies text-align: center; to the first line and left alignment to the rest.