How to center text in HTML
Overview
We can use the following two methods to center a text in HTML:
- The
<center></center>tag - The CSS
text-align:centerproperty
Let's look at a few examples.
The <center></center> tag
To center a text, we can enclose it between the <center></center> tag.
Syntax
<center>
<p>This text is in center.</p>
</center>
Example
Explanation
- Line 6: We create a text using the
<p>tag.
- Lines 5–7: We use the
<center></center>tag to wrap the<p>tag and center the text.
Note: The
<center></center>tag was deprecated in HTML 4 and is no longer recommended to be used. Some browsers may still support this tag. However, it can stop working at any moment.
The CSS text-align:center property
To center a text using CSS, we can add the text-align:center property to the element to be centered.
Syntax
<p>
This text is in center.
</p>
Example
Explanation
- Lines 5–7: We created a text using the
<p>tag and added thetext-align:centerproperty to it to center it.