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 Basics Getting Started with CSS Layout Why Vertical Margins Collapse

What is the purpose of collapsing vertical margins?

Why do browsers collapse vertical margins? I can't think of why this would be a desired behavior.

2 Answers

The function a margin serves is to ensure that an element should have X pixels of space on the top/bottom of it. If you had two paragraphs stacked in a div, each with margin:10px 0; without collapsing you would have 20 pixels between them. It makes sense when you think of it in the context of "I want paragraphs to always have 10px of space above and below them".

Here's an excellent post on the topic: http://complexspiral.com/publications/uncollapsing-margins/

Thanks for the response and the resource, guess I'll just have to remember to do box-sizing and add a little padding to avoid surprises.

Million Asseghegn
Million Asseghegn
3,845 Points

when there is no border padding that protects it