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 Layout Techniques Flexbox Layout Build a Navigation with Flexbox

Pavle Lucic
Pavle Lucic
10,801 Points

Margin:auto on main logo - explanation?

I dont understand, how margin:auto set on main-logo, push all other list items on the right?

How is that calculated?

3 Answers

Hi Pavle,

margin-right: auto is pretty much the same as margin-right: 0. Just to be clear, Guil is setting margin-right, not margin.

Pavle Lucic
Pavle Lucic
10,801 Points

Yes, my mistake for margin.

But why is logo pushing all other list item to the right, when defining margin-right:auto?

That's a great question Pavle and now I think I can properly answer it.

Setting margin-right: auto on the first li element is giving it the maximum amount of available margin - the result is that it pushes the other li elements to the right until they contact the parent containers right side.

Here is my Codepen showing the same effect, but applied to the 2nd li child. So, Logo and Home remain left, while all the others get pushed right. The Home li is given maximum right margin pushing the others to the right.

Here is a relevant quote from a CSS-Tricks Margin article.

a value of auto will be equivalent to a value of 0 (which is the initial value for each margin property) or else whatever space is available on that side of the element.

Pavle Lucic
Pavle Lucic
10,801 Points

So, basicaly it will push all elements on right, until last of pushed elements touch the right edge of parent container?

Yes, that is what's happening.

Pavle Lucic
Pavle Lucic
10,801 Points

I have modify your pen in chrome inspector, and it seems to margin-right:auto, only push elements if main-nav is set to display:flex;

The pushing elements stops when setting .main-nav to display:block, and list items to display:inline (or inline-block)

I tried my best to figure this out - without flex, but I cannot at the moment. Sorry Pavle.

Pavle Lucic
Pavle Lucic
10,801 Points

Well, we figured out something for sure! :D

Pavle Lucic
Pavle Lucic
10,801 Points

Maybe Guil Hernandez can help us to figure out why margin-right:auto push all other elements to the edge?

Serge Honderdos
Serge Honderdos
8,918 Points

Margin:auto has a new behavior within a fexbox. Margins set to auto get all the free space left. So that is why all the content is pushed to the right.

Serge Honderdos
Serge Honderdos
8,918 Points

When you have specified a width on the object that you have applied margin: auto to, the object will sit centrally within it's parent container.

Specifying auto basically tells the browser to automatically determine the left and right margins itself, which it does by setting them equally. It guarantees that the left and right margins will be set to the same size.

Pavle Lucic
Pavle Lucic
10,801 Points

If you watch this video, https://teamtreehouse.com/library/css-layout-techniques/flexbox-layout/build-a-navigation-with-flexbox you will see that margin:auto, only push list items from logo on far right side...