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 trialmohamedhalabi
1,968 Pointslong story short- Please correct me if im wrong !
is it right to say that if there is no padding between the elements, and the only thing separating them are margins, these margins will collapse to the highest value ?
IE: if there is a margin of an element A ( a div box per say) of 20 px ; and another margin of an element B of 30 px ;
the 2 above margins will collapse to form one margin of 30 px ?
i am just trying to verify what i understood from this vid, appreciate your help! thanks !
2 Answers
Steven Parker
231,248 PointsYou have the right basic idea, but be aware that there are different types of margin collapse: between adjacent siblings, and between parent and first or last child elements, and empty blocks. These each have slightly different rules.
For complete details, see this MDN page on Mastering Margin Collapsing.
mohamedhalabi
1,968 Pointsthank u both !
Danny Alder
9,119 PointsDanny Alder
9,119 PointsThe moral of your story is correct. The margin will always collapse to the highest margin value. Even negative margins are affected by this behaviour. A more detailed resource can be found here: https://css-tricks.com/what-you-should-know-about-collapsing-margins/.
A good habit to get into, especially when it comes to typography, is to only ever use margin-top OR margin-bottom in your css. Never both. I usually only use margin-bottom as this mimics spacing-below attributes of page layout software that I am used to using for print work. By only setting the bottom margin of type, you can be sure that your typographic rhythm on the page will almost always be consistent while avoiding collapsing margins between headers and paragraphs.