Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to center something in HTML

Understanding the Concept of Centering

Before we dive into the technicalities of centering in HTML, let's first establish a foundational understanding of what centering is. Imagine you're hanging a picture on your wall. You'd probably want it to feel balanced and proportional in relation to the wall, right? That's exactly what we're trying to achieve when we center elements in HTML. We want to balance our text, images, or other elements in relation to the space they're in.

The Old School Way: Center Tag

In the early days of HTML, centering an element was straightforward. There was a <center> tag that you could use. Here is how it would look:

<center>
  <p>This text is centered.</p>
</center>

However, as HTML has evolved, the <center> tag has been deprecated, meaning it's no longer recommended for use. It might still work, but it's considered poor practice to use it, and eventually, it may stop working altogether.

The Modern Approach using CSS

Today, the recommended way to center elements is by using CSS (Cascading Style Sheets). CSS is a language used alongside HTML to style the appearance of your web pages. Think of HTML as the skeleton of your webpage, while CSS is the skin and clothing that makes it look nice.

Centering Text

To center text, you can use the text-align: center; property in CSS. Here is an example:

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

In this example, style="text-align: center;" is the CSS we're using to center the text.

Centering Blocks

To center block-level elements (like divs, headers, and paragraphs), we utilize the margin property. In CSS, margin is the space around an element. By setting both the left and right margins to auto, you can center a block-level element. Here's an example:

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

In this example, margin-left: auto; margin-right: auto; tells the browser to automatically adjust the left and right margins of the div. The width: 50%; sets the width of the div to be 50% of its containing element.

Centering with Flexbox

Flexbox is a modern layout model in CSS that makes it easier to design flexible responsive layout structures. To center elements both vertically and horizontally using Flexbox, you can use justify-content: center; and align-items: center;.

Here's an example:

<div style="display: flex; justify-content: center; align-items: center; height: 200px;">
  <p>This text is centered both horizontally and vertically.</p>
</div>

In this example, display: flex; tells the browser to treat the div as a Flex container. justify-content: center; centers the text horizontally, and align-items: center; centers the text vertically.

Conclusion

Centering in HTML might seem confusing at first, especially because there are several ways to do it. While it's good to understand different methods, remember, the best method to use will depend on your specific situation. As a rule of thumb, use text-align: center; for centering text, margin: auto; for centering block elements, and flexbox for centering both horizontally and vertically. As you continue to learn and experiment with HTML and CSS, you'll become more comfortable with these concepts and more adept at knowing which method to use when.