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 CSS Basics (2014) The Box Model Margins

Henrique Voni
Henrique Voni
12,296 Points

Question about centering with margin:auto

I noticed that sometimes margin: 0 auto doesn't center my element on the page. Are these the requirements for centering that way?

1) Must be a display:block element; 2) Must have a width value.

Are there more requirements or am i wrong?

2 Answers

Nathan Tallack
Nathan Tallack
22,158 Points

Yeah, you need a width. Try setting your width to 90%. Avoid a px value here because if your browser window is narrower then you will get the horizontal scroll bars.

Matthew Stanley
Matthew Stanley
1,071 Points

Always remember Nathan's advice, because having to scroll horizontally is Hell on Earth.