Home Top Ad

How to center text in HTML.

Share:

To center text in HTML, you can use the <center></center> tags or use a style sheet property.

Using the <center></center> tags

One way to center text is to enclose it within <center></center> tags, as in the following example.
<center>Center this text!</center>
The above example would center the text on the page as seen in the example below.
Center this text!
Note: The <center> tag is now considered deprecated. Although it may still work, it is expected to be removed in favor of using CSS. We recommend you use the style sheet method (mentioned below) to center text in HTML.

Using a style sheet property

You can center text using CSS (Cascading Style Sheets) using the text-align property on the element that contains the text you need to center.
If you have only one or only a few blocks of text you need to center, you could include the style in the HTML code for the element, such as a <p></p> element, using the style attribute:
<p style="text-align:center;">Center this text!</p>
If you have many blocks of text you would like to center, you can use CSS inside <style></style> tags within the head section on the page (or in an external style sheet) to declare that every element be centered.
<style>
p { text-align:center; }
</style>
The text will be centered within every set of <p></p> tags on the page. If you would like some paragraphs centered while others are not, you can create a style class as seen in the below code.
<style>
.center { text-align: center; }
</style>
If you're creating a center class, a paragraph can be centered using the below code.
<p class="center">Center this text!</p>
Tip: Once a class has been created it can be applied to any HTML tag. For example, if you wanted the heading to be centered you could add class="center" to the <h1> or other heading tag.

Aucun commentaire