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 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?