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 Layout Techniques Positioning Schemes Absolute Centering

Joel Pendleton
Joel Pendleton
19,230 Points

Absolute Centering

Why is he able to center the icon with 'margin: auto;' Are you able to center floats with margin: auto? Why does he have to set the offsets to zero? Couldn't he just use 'margin: auto;' by itself? Is there a relationship between offsets and margins?

If anyone could answer these questions it would be greatly appreciated :)

1 Answer

Emma Davis
Emma Davis
4,760 Points

You can position any element centrally horizontally my using margin: auto (well actually, margin: 0 auto as the auto is relevant to the left and right margins only for horizontal positioning).

There is no reason why you would want to centrally align a floated element, even if it was possible, as the point of using float is to remove an element from the normal flow of the layout it is in and position it over to the left or right (there are 4 values you can use for floated elements: left, right, none (to remove a previously applied float) and inherit (which will take on the float value from the parent element).

The reason for the positioning offsets set to zero and not just using margin: auto is that it is the only way to get an element to be centrally aligned when using position absolute and positioning this way is so that the icon is central vertically. If position: absolute wasn't being used and there was no need to position vertically, then just using margin: auto would be sufficient.

Joel Pendleton
Joel Pendleton
19,230 Points

Thank you very much for explaining that to me.