How to center a header in HTML without using the center tag

The HTML <center> tag is one of those funny little tags left over from the early days of websites when designers often mixed the publishing of web content with design. These days, it’s much easier to separate content and design by using effective style sheets.

To center any text in HTML and CSS, we can simply add a couple of lines of code to our stylesheet file as follows:

[source lang="css"].center {
text-align:center;
}[/source]
This little code snippet will center the content within a block tag (such as a <p> or <h1>) but only if it already knows the width of the object you’re centering too! To get around this, we can simply add the display:block tag to our CSS:

[source lang="css"].center {
text-align:center;
display: block; 
}[/source]
Now our code knows to center text.

To apply it to our HTML code, you need to simply apply the code to an object as a class. For example let’s turn a heading into a centered heading by applying the new class like so:

[source lang="html"]
<h1 class=’center’>This is my header.</h1>
[/source]

 

It is of course also possible to simply align all <h1> tags to the center by using the code:

[source lang="css"]h1 {
text-align:center;
display: block;
}[/source]

What about converting existing pages?

Luckily if you already have a bunch of documents which use <center> tags there’s fairly simple fix for you to clean up those files. Using a tool like Adobe Dreamweaver you can do bulk search and replace functions across multiple files.

The replace is a two step process. (please backup your site before doing this)

First, let’s replace the phrase </center> with </div> across the entire site. This will change the closing tag (remember all tags come in pairs right?) to be a normal div tag.

Now, let’s replace the phrase <center> with <div class=’center’> to turn the opening tag into a div tag with the center class assigned.

You can now add the .center details to your CSS file and presto, your website is now using the proper center formating!

5 Responses to “How to center a header in HTML without using the center tag”

  1. Adis says:

    The link in your twitter for this article took me to a 404 page..

  2. Abhijeet says:

    Actually a quick good tip. I may not be using it for center, but I have some code that I use often and have to write it myself which I can add in CSS. For example, image alignment. Everytime I write the complete code to align the image in the blog post on right or left, but if I create that code in CSS, I think it would be much more convenient.

    Abhijeet’s last blog post..How to Download and Watch High Definition (HD) YouTube Videos

  3. personal Injury Lawyer says:

    The div#header and div#footer styles set margins and
    padding for those divs. In addition, div#header includes
    the text-align: center rule to center the header text, and
    div#footer includes the border-top: thin solid #000000 rule
    to create a border along the top edge of the div to replace
    the horizontal rule above the footer in the table-based
    layout.

  4. Toronto Personal Injury says:

    Good HTML align in center but if we want to control it with css then what the procedure.

  5. Acmeous says:

    Hi Christopher,
    A good explanation here on this.
    But I think we need to work this well in good browsers like Firefox, which is generally done by the – margin: 0 auto;
    Here is my technique to align any html element without the center or align tags – http://acmeous.blogspot.com/2008/05/how-to-center-website-or-any-element.html

Leave a Reply