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 Display Modes Block vs. Inline Elements

Chen Wang
Chen Wang
7,371 Points

Why a border or padding can avoid margin collapsing?

I saw a short youtube video which explains this. https://www.youtube.com/watch?v=1uZeD9-r1y0

Around 5:42 in the video, it says that adding a border or padding can separate two margins and therefore there will be no collapsing.

However, according to the box model, margin is the outermost layer, and border and padding are inner layer than margin.

How come adding border or padding can separate the margins from two box?? They are still touching together.

1 Answer

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Margin and padding both separate elements from each other it's just that padding separates content from inside the border. So while padding doesn't physically separate the elements from each other it can be aware to visually separate them.

Chen Wang
Chen Wang
7,371 Points

I understand the box model, but I didn't get what you said. what do you mean by " padding doesn't physically separate the elements "? Is the "element" referring to the "content"?

The video says that padding or border can separate the margins of two box, so it can avoid margin collapsing. However, according to my understand, padding or border can only separates the content, rather than margin.

So why adding padding or border can avoid collapsing, or in other word, why it can avoid touching margins??

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

I'm not sure what that means to be honest.

But if you the know box model you'll know that each layer of the box model adds t the space that element takes up. So each layer from the content layer of the box model adds a layer of separation from elements surrounding it. :-)