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 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,159 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.