Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to center header in HTML

Understanding HTML Headers

HTML headers play a key role in organizing and structifying the content on a webpage. They're like titles or sub-titles in a document or book. In HTML, we use <h1> to <h6> tags for headers. The <h1> tag is the highest level tag and <h6> is the lowest. Today, we'll focus on how to center these headers within your webpage.

Why Center Headers?

First, let's consider why we might want to center headers. Just like in a book or magazine, the position of the headers can significantly impact the design and readability of the content. Centering headers often makes them stand out more, which can help guide the reader through your content. Think of it like a big, bold sign hanging in the middle of a store aisle. It's much easier to notice and read!

The Traditional Way: Using HTML

In the early days of HTML, centering was done directly using the <center> tag. Here's an example:

<center><h1>This is a centered header</h1></center>

However, using the <center> tag is now considered outdated and not recommended. It's better to handle the presentation or styling part with CSS (Cascading Style Sheets), which we'll discuss next.

The Modern Way: Using CSS

CSS is used to style and layout web pages. In other words, while HTML provides the structure of the page, CSS makes it look good.

To center a header in HTML using CSS, we can use the text-align property with the center value. Here's what that looks like:

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

In this example, we've added a style attribute to the <h1> tag. This attribute includes CSS code. The text-align:center; part is the CSS code that tells the browser to center the text.

Using External CSS

The above method is an example of inline CSS, where the CSS code is included directly in the HTML tag. However, it's often better to use external CSS. This means creating a separate CSS file, and linking it to your HTML. This makes your code cleaner and easier to manage, especially for larger projects.

Here's how you can do it:

First, create a CSS file. Let's call it styles.css.

In styles.css, add the following code:

.center-text {
    text-align: center;

In this code, .center-text is a CSS class that we've created. Any HTML tag that uses this class will have its text centered.

  1. Next, in your HTML file, link to the CSS file. This is usually done in the <head> section of the HTML file, like so:
    <link rel="stylesheet" type="text/css" href="styles.css">
  1. Finally, apply the .center-text class to your header:
<h1 class="center-text">This is a centered header</h1>

Now, the header text is centered!

Centering Multiple Headers

What if you want to center multiple headers? Do you need to add the center-text class to every header? Thankfully, no.

There are two ways to handle this. One way is to use the <div> tag, which is a container that can hold other HTML elements. Any CSS applied to a <div> will also apply to the elements inside it. Here's an example:

<div class="center-text">
    <h1>This is a centered header</h1>
    <h2>This is another centered header</h2>

In this code, both the <h1> and <h2> headers will be centered.

Alternatively, you can edit the styles.css file to apply the center-text class to all header tags. Here's how:,,,,, {
    text-align: center;

With this code, any header from <h1> to <h6> with the center-text class will be centered.


Centering headers in HTML is a simple yet effective way to improve the design and readability of your webpage. While the <center> tag was used in the past, it's now recommended to use CSS for this task. By using either inline or external CSS, you can center a single header or multiple headers with ease. Happy coding!