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 Flexbox Layout Building a Layout with Flexbox Aligning Items to the Bottom of a Column

Antonija Kasum
Antonija Kasum
4,918 Points

Auto value

Can someone please explain to me the "auto" value? What exactly does it do?

4 Answers

Michal Janecek
Michal Janecek
8,194 Points

As I understand it, the auto value tells the browser to calculate margin so that en element is centered in its containing element. If you use the margin: 0 auto; that means that the browser won't insert any margin on the top and bottom side of an element, but it will calculate the right and left margin. If you are interested in how exactly the browser calculates the values here is the specification https://www.w3.org/TR/CSS21/visudet.html#blockwidth

As to why is centers I do not know, this is just the way auto works on the margin property. I have always wondered why

div {
  margin: auto 
}

didn't center the div in the middle of the page. Maybe @Guil Hernandez or one of the other instructors can chime in on this one.

I have done some research online and even searched Mozilla Developer Network and cannot find anything that links to a direct answer about auto. I believer this is because it acts differently on different properties. I am sure there is a white-paper out there somewhere..if I find it I will post it back here.

I have also found this discussion about it here on StackOverflow

Auto just simply means that the browser is going to do the calculation.

Antonija Kasum
Antonija Kasum
4,918 Points

Yes but WHAT does it calculate? Like when we use margin: 0 auto; how does it center the content on the page?