The text-align
property is used to set the horizontal position of the text in the element or relative to the parent element. It is also used to set it for the <img>
element. As the <img>
element is an inline element, you can adjust its position using this property.
There are four main keywords for property values.
left
: This is the default setting. With or without specifying it, the text is positioned at the left edge of the element or its parent element.center
: The text is positioned in the center of the element or its parent element.right
: The text is positioned on the right edge of the element or its parent element.justify
: This is only applicable when the text has multi-lines. With this setting, the text is still positioned on the left edge of the element but each word is spread from the left end to the right end, except the last line. It looks like a newspaper.If there is padding in the element, the left and right edges are defined after padding like in the illustration below.
The text-align
property is also used in the nesting structure. You can control the position of the child element from the parent element like in the illustration below.
There are two remarks to make this work:
margin: auto
or the align-items
property with the display: flex
property. We'll explain them later in this course.As the <img>
element is an inline element, you can also use this technique for the <img>
element.
The text-align
property is inherited. For example, when you set text-align: right
for the grandparent element, its grandchild element (if it's an inline element) is positioned at the right edge of the parent element like in the illustration below.
Add the code below at the end of the <body>
section in the chapter11.html file. In this code, we are testing two types of text:
We are using the outline
class set from the previous practice to show the result clearly.
<h2>Text Align</h2>
<h3>Text Align within the Element</h3>
<p style="border:1px dotted #26BCCE;">Text Align: Original</p>
<p style="border:1px dotted #26BCCE">
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
</p>
<p class="outline" style="text-align: left; ">Text Align: Left</p>
<p class="outline" style="text-align: left; ">
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
</p>
<p class="outline" style="text-align: right; ">Text Align: Right</p>
<p class="outline" style="text-align: right; ">
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
</p>
<p class="outline" style="text-align: center; ">Text Align: Center</p>
<p class="outline" style="text-align: center; ">
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
</p>
<p class="outline" style="text-align: justify; ">Text Align: Justify</p>
<p class="outline" style="text-align: justify; ">
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
</p>
Open chapter11.html with a browser.
text-align
property works.You can also check the sample result here (Demo Site ).
Add the code below at the end of the <body>
section in the chapter11.html file. In this code, we are adding the parent-box
and child-box
classes to contrast the parent element and child element.
<h3> Text Align Relative to the Parent Element</h3>
<div class="parent-box">
<a class="child-box">original position</a>
</div>
<div class="parent-box" style="text-align: left;">
<a class="child-box">text-align:left</a>
</div>
<div class="parent-box" style="text-align: center;">
<a class="child-box" style="text-align: right;">text-align:center</a>
</div>
<div class="parent-box" style="text-align: right;">
<a class="child-box" style="text-align: right;">text-align:right</a>
</div>
Open the practice.css file and add new code for adding styles to the parent-box
and child-box
classes.
.parent-box{
background-color: #55D0DC;
margin:20px;
width:300px;
border:5px dotted #26BCCE;
padding:30px;
box-sizing: border-box;
}
.child-box{
background-color: #E0F7F9;
border-style: solid;
border-color:#B3EBEF;
border-width:5px;
}
text-align
property works under the nesting structure.You can also check the sample result here (Demo Site ).
Add the code below at the end of the <body>
section in the chapter11.html file.
<h3> Text Align Inheritance</h3>
<div class="parent-box" style="width:300px; text-align: right;">
<div class="child-box">
<span>ABC</span>
</div>
</div>
<hr>
text-align
property set at the ground parent level works for the grandchild level.You can also check the sample result here (Demo Site ).
The text-align
property is used to set the horizontal position of the text in the element or relative to the parent element. It is also used to set it for the <img>
element. As the <img>
element is an inline element, you can adjust its position using this property.
There are four main keywords for property values.
left
: This is the default setting. With or without specifying it, the text is positioned at the left edge of the element or its parent element.center
: The text is positioned in the center of the element or its parent element.right
: The text is positioned on the right edge of the element or its parent element.justify
: This is only applicable when the text has multi-lines. With this setting, the text is still positioned on the left edge of the element but each word is spread from the left end to the right end, except the last line. It looks like a newspaper.If there is padding in the element, the left and right edges are defined after padding like in the illustration below.
The text-align
property is also used in the nesting structure. You can control the position of the child element from the parent element like in the illustration below.
There are two remarks to make this work:
margin: auto
or the align-items
property with the display: flex
property. We'll explain them later in this course.As the <img>
element is an inline element, you can also use this technique for the <img>
element.
The text-align
property is inherited. For example, when you set text-align: right
for the grandparent element, its grandchild element (if it's an inline element) is positioned at the right edge of the parent element like in the illustration below.
Add the code below at the end of the <body>
section in the chapter11.html file. In this code, we are testing two types of text:
We are using the outline
class set from the previous practice to show the result clearly.
<h2>Text Align</h2>
<h3>Text Align within the Element</h3>
<p style="border:1px dotted #26BCCE;">Text Align: Original</p>
<p style="border:1px dotted #26BCCE">
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
</p>
<p class="outline" style="text-align: left; ">Text Align: Left</p>
<p class="outline" style="text-align: left; ">
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
</p>
<p class="outline" style="text-align: right; ">Text Align: Right</p>
<p class="outline" style="text-align: right; ">
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
</p>
<p class="outline" style="text-align: center; ">Text Align: Center</p>
<p class="outline" style="text-align: center; ">
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
</p>
<p class="outline" style="text-align: justify; ">Text Align: Justify</p>
<p class="outline" style="text-align: justify; ">
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
</p>
Open chapter11.html with a browser.
text-align
property works.You can also check the sample result here (Demo Site ).
Add the code below at the end of the <body>
section in the chapter11.html file. In this code, we are adding the parent-box
and child-box
classes to contrast the parent element and child element.
<h3> Text Align Relative to the Parent Element</h3>
<div class="parent-box">
<a class="child-box">original position</a>
</div>
<div class="parent-box" style="text-align: left;">
<a class="child-box">text-align:left</a>
</div>
<div class="parent-box" style="text-align: center;">
<a class="child-box" style="text-align: right;">text-align:center</a>
</div>
<div class="parent-box" style="text-align: right;">
<a class="child-box" style="text-align: right;">text-align:right</a>
</div>
Open the practice.css file and add new code for adding styles to the parent-box
and child-box
classes.
.parent-box{
background-color: #55D0DC;
margin:20px;
width:300px;
border:5px dotted #26BCCE;
padding:30px;
box-sizing: border-box;
}
.child-box{
background-color: #E0F7F9;
border-style: solid;
border-color:#B3EBEF;
border-width:5px;
}
text-align
property works under the nesting structure.You can also check the sample result here (Demo Site ).
Add the code below at the end of the <body>
section in the chapter11.html file.
<h3> Text Align Inheritance</h3>
<div class="parent-box" style="width:300px; text-align: right;">
<div class="child-box">
<span>ABC</span>
</div>
</div>
<hr>
text-align
property set at the ground parent level works for the grandchild level.You can also check the sample result here (Demo Site ).