How to center text in HTML?

HTMLWeb DevelopmentFront End Technology

The text in an HTML document can be horizontally centred by using the <centre> HTML tag. Since HTML5 did away with this tag, it is advised that you format the document's text horizontally instead by using the CSS text-align property. The <centre> element is another name for this tag.

Note − Not supported in HTML5

Syntax

<p><center>……………..</center></p>

Following are the examples…

Example

In the following example we are using <center> tag to make our text aligned to the center.

<!DOCTYPE html> <html> <body> <p><center>Welcome to TutorialsPoint</center></p> <p>The Best E-Way learning.</p> </body> </html>

Output

On executing the above script, the text "welcome to tutorialspoint" will get displayed at the centre of our webpage.

Example: Using CSS

In the following example we are using css style (text-align:center) to make our text display at center.

<!DOCTYPE html> <html> <style> h1 {text-align: center;} p {text-align: center;} div {text-align: center;} </style> <body> <h1>TUTORIALSPOINT</h1> <p>The E-Way Learning.</p> <div>Welcome To Tutorials.</div> </body> </html>

Output

When we run the above script, the text is displayed in the centre of our webpage, as specified by the CSS property text-align: center.

raja
Updated on 05-Sep-2022 12:42:32

Advertisements