Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS

Centering heading

Hello!

I would like to center my h1=class"heading". Display: inline-block is because i want to wrap border around heading. .heading text-align: center; doesnt seem to work. Any solution? (one way is to set the body text-align: center; why?)

Thanks in advance!

<header class="main-header"> <nav class="navbar"> <ul> <li>home</li> <li>about</li> <li>contact</li> </ul> </nav> <div class="heading"> <h1>heading</h1> </div> </header>

body { margin: 0; }

.navbar { text-align: right; background: black; color: white; padding: 10px;

}

ul { list-style-type: none; padding: 0; text-align: right; }

li { display: inline-block; }

.heading { border: 1px solid black; display: inline-block; text-align: center; }

3 Answers

Hi Klemen,

Set your h1's display property to block, and then you can set a margin of 0 auto:

.heading {
    display: block;
    margin: 0 auto;
}

You can then give it a standard width if you do not want it to span the entire width of the page.

Hope this helps!

Thank you for answer, but it doesn't work.

Hi Klemen,

Simply use margin: auto on your heading selector.

Thanks for answer, but it doesnt work.