Centering text is generally used for a title of a website or document. To center text using HTML, you can use the
Using the tags
One way to center text or put it in the middle of the page is to enclose it within
- Using the
tags. - Using a style sheet property.
Inserting this text within HTML code would yield the following result:
Center this text!
Using a style sheet property
You can center the text of a website with CSS by specifying the text-align property of the element to be centered.
The
Centering a few blocks of text
If you only have one or a few blocks of text to center, add the style attribute to the element’s opening tag and use the “text-align” property. In the example below, we’ve added them to the
tag.
Center this text!
The “text-align” property is set to “center” to indicate the element is centered in the middle of the page or containing div.
Multiple blocks of text
If you have many blocks of text to center, use CSS inside tags in the head section or in an external style sheet. See the example code below for how to set all text inside the
tags to be centered.The text in every set of
tags is centered on the page. If you would like some paragraphs centered, while others are not, you can create a style class, as seen in the code below.If you’re creating a center class, as shown in the example above, a paragraph can be centered using the code below, which “calls” the center class.
Center this text!
Once a class is created, it can be applied to any HTML tag containing words, images, and most other elements. For example, if you wanted the heading to be centered, you could add class=“center” to the