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 Content

Hello,

In my Code I am trying to center the class called. ".center" which is an h2 element.

The h2 will not center, even though I have displayed the margins as "auto" on both sides. It is inside a parent element that has been centered. How can I center the h2 element?

Here is my code: https://w.trhou.se/92a9g0lp06

Thank you for your help!

2 Answers

margin: auto; is correct for centering - does the element have a width assigned to it?
If not, try putting a % or fixed width on it, see if the element centers: h2 { margin:auto; width: 50%; }

Alternately, if the element can be full width, this will center text within an h2:
h2 { margin:auto; text-align:center; }

Hi Michael,

I assigned a width to the element. It moved it over, but did not center it using a 50% width. I also tried 100%, but it did not center the element either.

You can just use text-align: center because all you are doing is aligning text with an element. In your case you want align your h2 to the center of your #bottomnav class.

Your code edited: https://w.trhou.se/s7w1gssll4