How to Center Text in HTML

HTML is the markup language used to create web pages. It provides a way to structure content on the web, including text, images, and other media. One common task that web developers may need to do is center text on a web page. This can be done in a few different ways in HTML using CSS. In this article, we will explore these methods and provide step-by-step instructions for centering text in HTML.

Method 1: Using Text-Align Center

The simplest way to center text in HTML is to use the CSS property text-align. This property can be applied to any HTML element with text content, including headings, paragraphs, and other block-level elements. To center text using text-align, follow these steps:

<p style="text-align: center;"> This text will be centered. </p>

Text-Align CenterSource:

The code above creates a paragraph element and applies the style attribute with the value text-align: center. This centers the text within the paragraph element. You can also apply this property to other elements that contain text, such as headings or lists.

Method 2: Using Margin Auto

Another way to center text in HTML is to use the margin property with the value auto. This method works by setting the left and right margins of an element to be equal, causing it to be centered within its container. To center text using margin auto, follow these steps:

<div style="width: 50%; margin: auto;"> This text will be centered. </div>

Margin AutoSource:

In the code above, a div element is created with a width of 50% and the margin property set to auto. This centers the div element horizontally within its container. You can also use this method to center other elements, such as images, by applying the margin property to them.

Method 3: Using Flexbox

Flexbox is a powerful layout tool in CSS that allows you to align and distribute content within a container. It provides several properties for centering content, including align-items and justify-content. To center text using Flexbox, follow these steps:

<div style="display: flex; justify-content: center; align-items: center;"> This text will be centered. </div>


In the code above, a div element is created with the display property set to flex and the justify-content and align-items properties set to center. This centers the content both horizontally and vertically within the div element. You can also use this method to center other types of content, such as images or lists.


Centering text in HTML can be done in several ways using CSS. Whether you choose to use text-align, margin auto, or Flexbox, each method provides a simple and effective way to center text within a container. By following the steps outlined in this article, you can easily add centered text to your web pages and create a more visually appealing layout.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *