• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Html » Html Center Text
Next →
← Prev

How to center text in HTML

By Dinesh Thakur

In this tutorial, we’ll learn how to center text in the HTML. To align text in HTML, You may use HTML <center> tag and HTML attributes on block elements or use a CSS property. The HTML center tag is simpler when compared with the CSS text-align. It is possible to discover how to center text using these two approaches.

Note: Centering in HTML used to do with the tag. However, But, Rather than tag CSS is used to center-align text, because the tag did not support in HTML5. We advise that you use the style sheet to center text in HTML. The alignment of the text in HTML5 can accomplish in various ways.

We’ll be covering the following topics in this tutorial:

  • Center Text in HTML Without Using CSS
  • To center text in an HTML using CSS Property.

Center Text in HTML Without Using CSS

Using HTML Center Tag

In HTML, the <center> tag doesn’t have particular attribute. But, it supports all HTML global attributes.

If you would like to align your text using HTML, put the <center> tag before beginning the content. After that, put the </center> tag following the conclusion of the text. Don’t contain any spaces.

Here method describes how to use the HTML <center> tag, which can be currently obsolete. The following is the syntax of the <center> tag.

 <center>center text html</center>

Your html code should look something like this:

<!DOCTYPE html>
<html>
 <body>
  <h1><center>Welcome to Computer Notes</center></h1>
  <center>Center Text in html</center>
  <p1><center>html center text</center></p1>
</body>
</html>

How to center text in HTML

Using HTML align Center Method

Again that was deprecated in HTML 4 and also won’t operate in HTML5.

<div align="center">
 <p>html center align</p>
</div> 

To center text in an HTML using CSS Property.

how to center text in cssUsing inline CSS

The first method we’ll look using inline CSS directly into the HTML tags. Inline CSS signifies your HTML and CSS will put together in the body part of your HTML code.

It could achieve using the style tag within HTML. CSS code is added over HTML code since both markup languages work together.

This property is a substitute for the HTML align attribute. You can center text using CSS text-align property. HTML Text alignment utilizes to align the text (such as left, right, center, etc.).

Note: By default, the text-align on the left side of the webpage.

The syntax for the text-align CSS property is:

To apply the text-align property, you need to define one out of six available values:

text-align: left | right | center | justify | initial | inherit

Check out an example of using CSS text-align below:

Example:

h1 { text-align: center; }
h2 { text-align: right; } 
h3 { text-align: left; }

All the available properties for using text-align are explain in the table below:

ValueDescription
leftText is left align div { text-align: left; }
rightText is right align div { text-align: right; }
centerText is center div { text-align: center; }
justifyText is justify (ie: line up the left and right edges of the paragraph) div { text-align: justify; }
startIf direction is left-to-right, the text will be left align. If direction is right-to-left, the text will be right align. div { text-align: start; }
endIf direction is left-to-right, the text will be right align. If direction is right-to-left, the text will left align. div { text-align: end; }
inheritElement will inherit the text-align from its parent element div { text-align: inherit; }
Note: The text-align property doesn’t control the alignment of block elements itself, just their inline content.

Example of HTML Center Text Alignment:

<html>
 <head>
  <style>
   h1 {text-align: center;}
   p {text-align: center;}
  </style>
</head>
<body>
  <h1> How to center text in HTML </h1>
  <p> align center html </p>
</body>
</html>

Example Explanation:

From the above, we’ve demonstrated an example of the HTML Center Text Alignment.

• Center attribute aligns the text to the center of the Webpage.

• It will help to align the heading into the center, which provides the better appearance of heading to the Webpage.

Centering a few blocks of text

In case you have just one or a couple of text blocks you want to center-align, you can do this by incorporating the style attribute to the opening tag of this element and deciding upon the property “text-align.” From the example below, it added to the <p> tag:

We can utilize style as an attribute and the text, which includes a string that needs to be aligned. Bear in mind; we’re still using the CSS attribute to align our text. The example code below shows how ‘style’ use inside the <p> tag. To align to the left or right, we can substitute the ‘center’ in text-align: center with left/right.

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

Note that the value of the property “text-align” is set to “center” to signify the element is to center.

Multiple blocks of text

It could be more useful to style multiple blocks with one principle, Then use CSS inside <style></style> tags in the head area or an external style sheet. It is possible to either style the tag by tag name, class name, or id, as shown below.

<style>
/* styles by tags*/
p { text-align:center;}
</style>

The text in each set of <p></p> tags centered on the page. If You’d like any paragraphs centered, while some aren’t, you can create a style class, as noticed From the code below.

<style>
/* styles by class*/
.center { text-align: center }
</style>

If you are developing a center class, as shown in the example above, a paragraph could center using the code below, which”calls” the center class.

<style>
/* styles by id*/
#centerbyid { text-align:center;}
</style>
Tip: After a class generates, it may apply to almost any HTML tag. By way of example, if you want to go to a center, then you can add class="center" into the tag or a different heading tag.

You’ll also like:

  1. How to center an image in HTML
  2. Html Center
  3. HTML text on Swing Components Java Example
  4. What is cipher text?
  5. Text Effect in Java Example
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

HTML Tutorials

HTML Tutorials

  • HTML - Home
  • HTML - History
  • HTML - Lists
  • HTML - Unordered List
  • HTML - Ordered Lists
  • HTML - Frame Tag
  • HTML - Input Type
  • HTML - Input Type="File"
  • HTML - Input Type="Hidden"
  • HTML - Form Tag
  • HTML - Img Tag
  • HTML - Anchor Tag
  • HTML - Table Tag
  • HTML - Span Tag
  • HTML - Button Tag
  • HTML - Doctype
  • HTML - Fieldset Tag
  • HTML - Links Tag
  • HTML - Drop Down Menu
  • HTML - Accesskey
  • HTML - Cgi
  • HTML - Post Method
  • HTML - Background Image
  • HTML - Text Area
  • HTML - TabIndex
  • HTML - Align Center
  • HTML - Address Tag
  • HTML - Thumbnails
  • HTML - Marquee Tag
  • HTML - Line Break
  • HTML - Blockquote
  • HTML - HR Tag
  • HTML - Image Center
  • HTML - Center Text
  • HTML - Link CSS
  • HTML - Html Vs Html5

Other Links

  • HTML - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW