Text Alignment
Part of the Styling with CSS section of Coddy's HTML journey — lesson 21 of 76.
In CSS, the text-align property is used to control the horizontal alignment of text within an HTML element.
Common values for text-align include:
left: Aligns the text to the left. This is the default alignment for most text.
right: Aligns the text to the right.center: Centers the text horizontally within the element.
For example:
p {
text-align: left;
}
h1 {
text-align: center;
}
.right-align {
text-align: right;
}Challenge
EasyYou are given an HTML document with a heading (<h1>), a paragraph (<p>), and a division (<div>). Your task is to use the text-align property to style these elements. Follow the steps below:
- Write a CSS rule that targets the
<h1>element. Set thetext-aligntocenter. - Write a CSS rule that targets the
<p>element. Set thetext-aligntoright. - Write a CSS rule that targets the
<div>element. Set thetext-aligntoleft.
Cheat sheet
The text-align property controls horizontal text alignment within an element.
Common values:
left: Aligns text to the left (default)right: Aligns text to the rightcenter: Centers text horizontally
p {
text-align: left;
}
h1 {
text-align: center;
}
.right-align {
text-align: right;
}Try it yourself
<html>
<head>
<title>Text Alignment</title>
<style>
/* Write CSS rules here */
</style>
</head>
<body>
<h1>This is a heading. </h1>
<p>This is a paragraph.</p>
<div>This is a division. </div>
</body>
</html>This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Styling with CSS
4Text Fundamentals
Text ColorFont FamilyFont SizeFont WeightText AlignmentText DecorationRecap Challenge #1Recap Challenge #25 Colors and Backgrounds
Background ColorHEX ColorsRGB ColorsTransparency with RGBARecap Challenge #1