How do I center an h1 in the body

user3182981 picture user3182981 · Jan 10, 2014 · Viewed 64.4k times · Source

I tried centering my h1 in the body tag like this:

h1 {margin:0 auto}

http://jsfiddle.net/qPDwY/

But it doesn't center. How do I fix it?

Answer

j08691 picture j08691 · Jan 10, 2014

In this case:

h1 {
    text-align:center;
}

jsFiddle example

The margin:auto rule is used when you set a width on the element, which you haven't done.