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

Margin collapsing and borders

What does applying a border to a parent container do to prevent the margin of a first child element from collapsing?

From a box model perspective, a border is not needed to apply a margin between sibling elements. Is it just a means of creating a specific context with a parent container specific to the first child element?

1 Answer

Steven Parker
Steven Parker
229,644 Points

The rule for parent-child margin collapse is that the margins must overlap with no separation to collapse. Separation would include border, padding, inline elements, and even specific dimensioning. So applying a border to the parent separates the parent margin from the child margin and will prevent margin collapse.

The rule for adjacent siblings is different, margins are always collapsed. Either or both of them may have a border and it will make no difference. But when you think about it, borders on siblings don't create separation, nor would padding or dimensioning.