How To Center or Align Text On Your Webpage with HTML

Aligning your content to the center, left, or right can be useful for arranging content on your page. In this tutorial, we’ll learn how to align text using HTML.

To align text on a webage, we can use the style attribute and the property text-align.

For example, the following code snippet would center the text “Sample text”:

<p style="text-align:center;">Sample text</p> 

To align your HTML content to the left or right, you would replace center with left or right.

In this tutorial, we’ll go through the process of using the text-align property to center the images and text in the top section of our webpage as illustrated in our demonstration website.

To center this content, we’ll add the text-align property to the <div> element that contains the background image, profile image, title, subtitle, and link in the top section of the homepage.

Locate this <div> element in your index.html file and add the highlighted text like so:

... <!--First section--> <div style="background-image: url(''); background-size: cover;; height:540px; text-align: center;"> <img src="" style="height:150px; border-radius: 50%; border: 10px solid #FEDE00; margin-top:80px;"> <h1 style="font-size:100px; color:white; margin:10px;">Sammy the Shark</h1> <em> <p style="font-size:30px; color: white;"> Senior Selachimorpha at DigitalOcean </p> </em> <p style="font-size: 20px; color:#1F9AFE;">     <a href="Webpage FilePath";> About this site </a>     </p> </div> ... 

Only copy and add the highlighted text-align attribute as other parts of this HTML code will not be specific to your project. Save your file and reload it in the browser. You should receive something like this:

Centered content

You should now understand how to center and align text and have a section that looks like the top section of the demonstration site. In the next tutorial, we will recreate the middle section of the demonstration site.