Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to center align text in HTML

Understanding the Importance of Text Alignment

Before we delve into how you can center align text in HTML, it's important to understand why text alignment matters. Imagine reading a book where all the text is skewed to the right or left. It would be a bit difficult and uncomfortable, right? The same principle applies to web pages. Good text alignment improves readability, makes your page look organized, and enhances user experience.

The Journey Towards Center Alignment

Center alignment is like the middle ground in a tug-of-war game. It's the point where there's no more pulling to the left or right, which in this case, refers to your web page. So, how do we achieve this equilibrium? Let's find out.

Using the <center> Tag

Think of the <center> tag as the 'referee' in our tug-of-war game. It ensures that the text does not skew to either side. Here's how you can use the <center> tag:

<center>This is a centered text</center>

This isn't a magic spell, but it will make your text appear in the center of your web page. However, it's worth noting that the <center> tag is not supported in HTML5. It's like an old referee who's retired. So, we need to find another way to center align our text.

CSS to the Rescue

CSS, or Cascading Style Sheets, is like the new-age referee in our game. It's more powerful, versatile, and is supported in HTML5. CSS lets you style your HTML elements, and yes, that includes center aligning your text!

Using the text-align Property

The text-align property in CSS is your go-to tool for aligning text. To center align text, you would use it like this:

<p style="text-align:center;">This is a centered text</p>

The text-align:center; part is the CSS. It's like a command to the p element, telling it to align its text in the center.

Using CSS Classes

You can also create a CSS class to center align text. This is like a special group that your elements can join to become centered:

<style>
.center-text {
  text-align: center;
}
</style>

<p class="center-text">This is a centered text</p>

In the above example, .center-text is a CSS class that aligns text in the center. By adding class="center-text" to our p element, we're making it join the 'center-text' group.

Centering Block Elements

The methods we've explored so far work for inline elements. But what about block elements – the ones that take up the full width of the page like a div or a header?

For these types of elements, we need to use a different property in CSS: margin. Think of margin as the space around an element. By setting the left and right margins to auto, and giving the element a specific width, we can center it:

<div style="margin-left: auto; margin-right: auto; width: 50%;">
This is a centered block element
</div>

In the above example, the div element is given a width of 50% and its left and right margins are set to auto. This combination pushes the div to the center of the page.

Centering Text Vertically

To center text vertically, we can use the line-height property in CSS. This property specifies the height of a line of text:

<div style="height: 200px; line-height: 200px; text-align: center;">
This is vertically centered text
</div>

In this example, the div element has a height of 200px. By setting line-height to the same value, we ensure that the text is vertically centered within the div.

Conclusion

Center aligning text in HTML may seem challenging at first, but with an understanding of CSS and how alignment works, it becomes a walk in the park. So next time you're playing the tug-of-war game of web page design, remember the CSS properties and HTML tags we've discussed, and you'll have your text perfectly centered. Happy coding!