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:center property

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

Center text using <center></center> tags

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

Center text using CSS text-align:center property

Explanation

  • Lines 5–7: We created a text using the <p> tag and added the text-align:center property to it to center it.

Free Resources