Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

CSS

Klemen Horjak
Klemen Horjak
4,737 Points

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

James Matthews
James Matthews
21,609 Points

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!

Klemen Horjak
Klemen Horjak
4,737 Points

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

Simon Duchaine
Simon Duchaine
14,441 Points

Hi Klemen,

Simply use margin: auto on your heading selector.

Klemen Horjak
Klemen Horjak
4,737 Points

Thanks for answer, but it doesnt work.