Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Alignment in HTML
3.
What is the <center> Tag Used for?
4.
Methods of Center Aligning in HTML
4.1.
Center Aligning in HTML Using <center> Tag
4.1.1.
Syntax
4.1.2.
Code
4.1.3.
Output
4.1.4.
Explanation
4.2.
Center Aligning in HTML Using "align" Attribute
4.2.1.
Syntax
4.2.2.
Code
4.2.3.
Output
4.3.
Center Aligning in HTML Using CSS "text-align"
4.3.1.
Code
4.3.2.
Output
4.3.3.
Explanation
5.
Best Practices for using HTML Align Center
5.1.
Using CSS instead of HTML attributes
5.2.
Accessibility 
5.3.
Text Centering
5.4.
Responsiveness
5.5.
Alternative Techniques
5.6.
Using Frameworks
5.7.
No Overuse of HTML Align Center
6.
Differences between HTML <center> Tag and CSS “Text-Align: Center;” Property
7.
Frequently Asked Questions
7.1.
How can I center align images in HTML?
7.2.
How do you center align a form in HTML?
7.3.
Why is it not recommended to use <center> tag to center align HTML elements for layout designing?
7.4.
How can I center align multiple elements in HTML by using aligning only once?
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

HTML Align Center

Author Aayush Sharma
0 upvote

Introduction

Center alignment is a way of horizontally aligning elements in the center of the content in HTML. Center alignment is applied to images, text, tables etc. in HTML to make the content presentable.

HTML center align

But one might wonder what are the different ways to center align elements in HTML and which of them are better than others. We will discuss these ways in the further sections, but first let us see about alignment in HTML.

Alignment in HTML

Alignment refers to the positioning of an element (horizontally and vertically) in a webpage. Alignment can be done in many ways in HTML.

  • Horizontal Alignment - It is used to align the elements in HTML with respect to the left and right borders of its parent container.
     
  • Vertical Alignment - It is used to align the content with respect to the top and bottom borders.

Alignment is an important aspect of web designing. Its correct use makes the webpage look presentable and easier to read. It also increases the readability of the webpage.

Horizontal alignment is of three types:

  1. Left alignment
  2. Right alignment
  3. Center alignment


In this article, we will discuss center alignment. As the name suggests it means to align the elements in the center of the page. In the upcoming sections, we will see different methods to center align elements in HTML.

What is the <center> Tag Used for?

<center> Tag in HTML is used to align text or any other document in its parent member in the center. The <center> tag in HTML is mostly used in the block-level document to center align the text, images, or any other element within the <div> or <body> element. The <center> tag is a block level element. The <center> tag is no longer supported in HTML5, and it's deprecated from using the  <center> tag. Styling the elements in the center is now generally used by CSS styling. 

Methods of Center Aligning in HTML

There are various ways to center-align elements in HTML. These ways depend on the type of elements. Center alignment works differently for block and inline elements. Since inline elements do not take extra width than required, these cannot be centered.

For example, block-level elements like <div>, which take up the full width of the container, can be easily center aligned by using align attribute in the parent container, shown in the next sections. 

But inline elements such as <span> also need the parent container's display property set to "block". This makes the inline element behave like a block element and allows them to be centered.

Now we will discuss some ways to center align elements in HTML.

Center Aligning in HTML Using <center> Tag

One of the easiest ways to center align text or any other content within its parent element is to enclose it in the HTML <center></center>  tag. The center tag works by  

enclosing the content between <center></center> tags.

The correct syntax of using <center> tag along with a complete example is shown below.

Syntax

<center> Hello Ninja! </center>

Code

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>HTML Center Align</title>
	<style>
		body{background-color:powderblue;}
	</style> 
</head>
<body>
	<center>
		<h1>Center Align using center tag</h1>
	</center>
</body>
</html>

Output

center align using acenter tag

Explanation

We have simply enclosed the required elements under the <center> tags, and inside the <style> tag, we simply set the background color to make the center-aligning easier to see. Although the <center> tag is an easy-to-use method to center align, it is now deprecated and unsupported in HTML5. Hence it is recommended not to use the <center> tag to HTML align center elements.

Center Aligning in HTML Using "align" Attribute

Another method to center align elements in HTML is to use align attribute in HTML and set it to the value "center." This is another easy way to center align in HTML. The correct syntax of using "align" attribute along with a complete example is shown below.

Syntax

<h1 align="center">Hello Ninja!</h1>


Code

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>HTML Center Align</title>
 	<style>
		body{background-color:powderblue;}
	</style>   
</head>
<body>
   <h1 align="center">Center Align using align attribute</h1>
</body>
</html>

Output

center align using align attribute

Like the <center> tag, the "align" attribute is also deprecated in HTML5 and not recommended for Center aligning.

Center Aligning in HTML Using CSS "text-align"

After the depreciation of <center> and "align" attribute in HTML, it is recommended to use CSS to center align elements.

There are many ways to center align elements in HTML using CSS, but the most prominent one to center align text is to use the "text-align" attribute in CSS.The correct syntax of using CSS to center align elements is shown below:

Code

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>HTML Center Align</title>
	<style>
		body{background-color:powderblue;}   
		h1{text-align:center;}
	</style>
</head>
<body>
	<h1>Center Align using CSS</h1>
</body>
</html>

Output

cente align using css

Explanation

In this example, we have added the text-align style to every <h1> element using the <style> tag in the head. This applies center alignment to each element in any <h1> element throughout the document.

Best Practices for using HTML Align Center

Although using center alignment in HTML is straightforward, we should try to follow some practices for making a readable and accessible website. Some of the most common practices are listed below.

Best practices

Using CSS instead of HTML attributes

It is recommended to use CSS nowadays for styling HTML documents instead of using HTML attributes like "align" because CSS provides more flexibility in designing as compared to HTML. Also, it makes it easy to separate content sections from the styling sections.

Accessibility 

When center aligning elements in HTML, we should make sure that it does not impact the accessibility of the user negatively. For example, it may be harder to click or access elements in the middle of the screen on large devices. So we should use it in a helpful manner.

Text Centering

In the case of text elements, usually, it is preferred to use left or right alignment to center text as it is easy to read. Centering large paragraphs might affect readability. So it is advised not to center large sections of text. Instead, we should only center the important headings of a document.

Responsiveness

Most websites are made to be used on devices of different resolutions. We should make sure that alignment on one device must not hamper the readability on other devices.

Alternative Techniques

In some cases, center alignment may not be the best approach to use; instead, other CSS methods, like Flexboxgrid, etc., may be better.

Using Frameworks

Frameworks like Bootstrap provide pre-built components that are already styled to look presentable. Such frameworks can make it easy to implement the same alignment and save time.

No Overuse of HTML Align Center

Lastly, we should ensure we do not overuse center aligning in HTML, as it can reduce the presentability of the website instead of increasing it.

Differences between HTML <center> Tag and CSS “Text-Align: Center;” Property

Basis of Comparison HTML <center> Tag CSS “Text-Align: Center;”
Usage HTML is an <center> Tag The CSS text-align is a property in CSS.
Application HTML <center> tag is applied on a section of a web page. If not applied to any element, it works for the entire page.  The CSS text-align is applied to the specific element to which the property is attached. 
Affected Elements  HTML <center> tag is a block-level element and gets applied to all the text attached to it.  CSS text-align is an inline property that gets applied to a specific section or on a specific element. 
Flexibility Today, we do not use the <center> tag as it is not feasible, and using them on a specific element requires additional methods.   The text-align center property is more feasible inline element that helps style the specific word or image in the center. 
Depreciation  HTML <center> tag is no longer supported in HTML 5; using this tag is not the best practice.  The CSS text-align center property is supported by HTML 5 and further versions. 

Frequently Asked Questions

How can I center align images in HTML?

We can align many HTML elements like text, images, tables, form elements, etc., horizontally with center aligning in HTML documents. However, we can not align it vertically. For vertical alignment, we have to use CSS techniques.

How do you center align a form in HTML?

To center align a form in HTML, you can make use of CSS. The various styling and layout features of CSS help to center align a form in  HTML. There are various methods in CSS through which you can center align a form in CSS. 

Why is it not recommended to use <center> tag to center align HTML elements for layout designing?

Instead of <center> tag in HTML, CSS is recommended to use for center aligning layout because it gives more flexibility over the design. Moreover CSS also helps to separate content elements from the styling elements which makes it easier to design websites.

How can I center align multiple elements in HTML by using aligning only once?

We can center align multiple elements at once by wrapping all of them in a single parent container and applying center aligning to the parent element. This will center all the elements within the container.

Conclusion

In this article, we discussed center aligning in HTML and various methods to apply it. In the end we concluded with some beneficial standard practices to remember while designing a webpage. So now that you have learnt about center aligning in HTML, you can also refer to other similar articles.


You may refer to our Guided Path on Code Studios for enhancing your skill set on DSA, Competitive Programming, System Design, etc. Check out essential interview questions, practice our available mock tests, look at the interview bundle for interview preparations, and so much more!

Live masterclass