html

How to center text in HTML

Choosing the right alignment for your elements that appear on the website is very important as this alignment defines the overall structure of a web page. When it comes to aligning text then developers often align text to the center to highlight the title or heading of a web page or a document. If you desire to learn how to center a text in HTML then you are the right place. Here we have summed up ways using which you can align your text to center.

How to center text in HTML

There are two ways with which you can center your text in HTML. Both of these ways have been discussed below.

How to center text using <center> tag

If you wish to center your text then simply wrap your desired text within the <center> tag and the text will be centered. However, the <center> tag has been eliminated from HTML5 in favor of using CSS instead of this.

HTML

<!DOCTYPE html>
<html>
   <head>
      <title>How to center text in HTML</title>
   </head>
   <body>
      <p>This paragraph is not centered.</p>
      <p><center>This paragraph is centered.</center></p>
   </body>
</html>

In the code above, we have created two paragraphs ,however, to depict the difference between default alignment and center alignment of the text we have wrapped up the text of the second paragraph within the <center> tag.

Output

The text has been centered using the <center> tag.

How to center text using the style attribute

The preferred way of centering a text is by using the style attribute which gives an inline style to a particular element.

HTML

<!DOCTYPE html>
<html>
   <head>
      <title>How to center text in HTML</title>
   </head>
   <body>
      <p>This paragraph is not centered.</p>
      <p style="text-align: center;">This paragraph is centered.</p>
   </body>
</html>

Through the usage of style attribute we are setting the text-align property of the second paragraph to center. It will generate the following output.

Output

This is how you use the style attribute to center text in HTML.

However, if you do not want to use inline CSS to center your text then another way of doing so is by using the internal CSS. Here we have shown how it is done.

HTML

<!DOCTYPE html>
<html>
   <head>
      <title>How to center text in HTML</title>
      <style>
         #para{
            text-align: center;
         }
      </style>
   </head>
   <body>
      <p>This paragraph is not centered.</p>
      <p id="para">This paragraph is centered.</p>
   </body>
</html>

In the above code, since we only want to center the second paragraph therefore we are first of all assigning it an id and then using this id we are setting the text-align property to center in the <style> tag in the <head> section. This code will also give the same output.

Conclusion

In order to center text in HTML either use the <center> tag or the style attribute. If you are using the <center> tag then simply place your text inside this tag and it will be centered, meanwhile, set the text-align property of the style attribute to center to center a text using the style attribute. Note that the <center> tag has been eliminated from HTML5 in favor of using CSS.

About the author

Naima Aftab

I am a software engineering professional with a profound interest in writing. I am pursuing technical writing as my full-time career and sharing my knowledge through my words.