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

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
221,070 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.