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.
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.
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.
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.
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.
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.
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.