Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
One CSS concept that can really trip up new designers is vertical margin collapse. Essentially, when two vertical margins touch, they collapse to the larger of the two values. Although I covered this in the CSS: Core Concepts course, not fully understanding how it works can cause some real problems in your layouts, so it's important enough to take another look at. To do that, we are going to open up the margin-collapse.htm file, and you can find that in the 01_03 directory, which is inside the Chapter_01 folder of your exercise files.
Now, just going over the structure of this page for a moment, if I scroll down, I can see that inside the HTML code we have a heading which is followed by four paragraphs. Now the bottom two paragraphs are wrapped inside of div tags, so keep that in mind that we have four paragraphs in a row but the bottom two are wrapped inside of inside of div tags. And then if I just look at the default styles, I can see that we're using some transparent backgrounds, so we can see through them. So our paragraphs have a background, the div tags have a background, and then I have a background applied to the HTML, which is the root element, which is a grid.
So if we look at this in the browser before we start working with this, you will see what I am talking about. So we have this grid structure that looks a little odd, but what that's going to allow us to do, these are 10-pixel increments on this grid, and it's going to allow us to sort of really calculate the margins that we have between these elements--right now we have none obviously--and really tell when this sort of collapsing is occurring. Okay. So I am going to go ahead and go back into my HTML, and we are just going to add some margins to some of our elements. So we are going to start with our paragraph rule. So I am going to scroll down until I find my paragraph rule and just below my line-height, I am going to go ahead and add a margin. And we are going to do 10px 0.
Now if you remember your shorthand syntax, the first of these two values applies to the top and the bottom of the paragraph, whereas the second value applies to the left and right. So essentially, we're saying, "Hey, go ahead and apply 10 pixels' worth of margin to the top and to the bottom of the paragraphs, but none, right and left." Okay, so I am going to save this, go back in my browser, and let's refresh that, and you can see we are getting exactly, more or less, 10 pixels' worth of space between each of our paragraphs. Now here is what's interesting. Remember, we applied the margin to both the top and the bottom of our paragraphs, so the bottom of this paragraph should have 10 pixels and the top of this one should have 10.
And if our vertical margins didn't collapse, we would have a total of 20 pixels' worth of space there, but as you can see, we only have 10. So you're seeing vertical margin happen right there, and this is actually the number one reason why we have vertical margin collapse. You can see, we are only getting 10 pixels' worth of space between the two of them and that presents sort of double spacing our paragraphs, if you will, and that's the whole reason for vertical margin collapse, or at least the main reason for it. So I am going to go back into my code, and let's see what happens when we have an unequal amount of margins on the top and bottom of elements.
So we are going to go back to our paragraph and just below the existing margin rule, I am going to write another margin, but this time I am going to do margin-top. And we are just going to do 20 pixels' worth of space margin top. So essentially what we are doing is we are overriding the previous top, but the bottom remains, so essentially now on our paragraphs, we have 20 pixels' worth of margin on the top of them and 10 pixels' worth of margin on the bottom. So let's see how that is resolved. So I am going to go ahead and save this, go back into my browser, refresh my page, and I can see that the larger of the two values, the 20 pixels, is the one that's being used.
So if you have two margins touching each other and one is 20 pixels and one is 10 pixels, you are going to get the 20 pixels. You are always going to get the larger of the two values when the two of the collapse. Now you will notice that the bottom two paragraphs here have a slightly different background color, and the reason for that is because you are seeing the background of the paragraph and the background of the div blending together, because they are semi-opaque, which is why we can sort of see the grid behind them. Okay, but what happens when margins between child elements and parent elements react with each other? That's a little bit trickier.
So let's go experiment with that. All right! So going back in our code, I am going to go down and I am going to go into my div selector and just below that I am going to type in margin, and once again, 10px 0. So, just as we did the paragraphs, that's going to give us 10 pixels above, 10 pixels below of margin on our div tags, 0 side to side. Now keep in mind our paragraph still have 20 pixels of margin on the top, 10 pixels to the bottom. So if I save this and refresh this in my browser, nothing happens. Now why does nothing happen? Well, if you consider the fact that each of these paragraphs have 20 pixels' worth of margin to the bottom and our div tags have 10 pixels here, here, here, and here, the 10 pixels and the 20 pixels are touching each other.
Those margins are touching. So again, they collapse down to 20 pixels. Now what's particularly interesting is I wanted you to take just a moment to visualize the space between these two div tags, if you will. This top div tag has 10 pixels' worth of bottom margin. This bottom div tag has 10 pixels' worth of top margin. So if we didn't have margin collapse, that's at least 20 pixels between the two of them; however, this paragraph inside of it has an additional 20 pixels of margin to the bottom. This paragraph inside of this div tag has 10 pixels of margin to the top.
So if we didn't have margin collapse, instead of looking at just the 20 pixels' worth of the space between this, we would have 30 pixels pushing down this way and 20 pixels pushing down this way, meaning there'd be 50 pixels' worth of space between them. So now you can sort of see why margin collapse is so important. Without it, our elements would begin to really start to stretch and grow apart from each other, and it would be very limited in terms of how we could apply our margins. Now the reason that the margins between the divs and the paragraphs are collapsing are because the margins are touching.
That means that there's no padding to get in the way. There's no borders to get in the way. There's nothing to interrupt those two margins touching each other. That is the only time that margins collapse. If margins have something separating them, then they do not collapse. All right! So if I go back, for example, into the code and let's say we go into our div tag again and I just come in and add a border to them. So let's just say border: 1 pixel solid black. Okay, so if I save this and test it, you can see that now we are getting our margins.
We are getting the 10 pixels between the div tags. We are getting the 20 pixels of margin at the top of the paragraph and then 10 pixels of margin at the bottom of the paragraph. Those margins are not collapsing, because they are not touching anymore. They have this border separating them. So if you'd really need that spacing, you need to make sure that something is interrupting those margins from touching each other. It could be border or even it could be padding. For example, if I go back into my code and I replace border with padding, and let's say I just do one pixel's worth of padding, top and bottom.
So if I save this and test it, you can see we are still no longer getting the margin collapse and we don't have to worry about the border showing up anymore. You know, the only difference there is of course we do have to account for that one pixel's worth of padding top and bottom. They do add to the overall height of our element there, so in certain layouts you may want to be careful about that. But if you are trying to prevent margins from collapsing, that's one of the easy ways to do it. You just put a little bit of padding or a border between the two of them to prevent those two margins from touching. Now vertical margins can be a little bit tricky, so if you'd like, go ahead and take this file, experiment with it a bit. Add a little bit more structure, add additional margin values, and really kind of play around and explore with how this works.
The more you work with those vertical margins, the more comfortable you'll get understanding when they collapse. And there are certain instances such as with floated elements and absolutely positioned elements that we'll see later on in this title, when those vertical margins are ignored. Now the good news here is that we only have to worry about vertical margins in terms of collapsing, because horizontal margins do not collapse. Overall, just keep margin collapse in mind as you begin to plan your layout, and if you're not getting the vertical spacing between elements that you expect to be getting, go ahead and explore whether or not margin collapse is the culprit.
Get unlimited access to all courses for just $25/month.Become a member
58 Video lessons · 59898 Viewers
61 Video lessons · 92618 Viewers
82 Video lessons · 104164 Viewers
56 Video lessons · 107108 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.