Start your free trial now, and begin learning software, business and creative skills—anytime, anywhere—with video instruction from recognized industry experts.

Start Your Free Trial Now

Understanding margin collapse

Understanding margin collapse provides you with in-depth training on Web. Taught by James Williamso… Show More

CSS: Page Layouts

with James Williamson

Video: Understanding margin collapse

Understanding margin collapse provides you with in-depth training on Web. Taught by James Williamson as part of the CSS: Page Layouts
Expand all | Collapse all
  1. 4m 20s
    1. Welcome
    2. How to use the exercise files
      3m 26s
  2. 1h 39m
    1. Box model review
      8m 47s
    2. Calculating element dimensions
      11m 11s
    3. Understanding margin collapse
      7m 59s
    4. Calculating em values
      7m 41s
    5. Calculating percentage values
      7m 51s
    6. Normal document flow
      13m 3s
    7. Controlling element display
      8m 53s
    8. Using CSS Resets
      7m 11s
    9. Fixed, fluid, and responsive layouts
      9m 9s
    10. CSS debugging tools
      6m 46s
    11. Using the Firebug Inspector and the WebKit Web Inspector
      11m 5s
  3. 53m 16s
    1. Page design workflow
      3m 6s
    2. Page design tools
      4m 56s
    3. Determining page structure
      7m 18s
    4. Creating image assets
      8m 58s
    5. Creating initial page structure
      7m 4s
    6. Adding meaning with classes and IDs
      5m 23s
    7. Structuring content with HTML5
      6m 6s
    8. Building internal structure
      10m 25s
  4. 1h 36m
    1. Floating elements
      7m 50s
    2. Clearing floats
      7m 28s
    3. Containing floats
      7m 50s
    4. Clearfix technique
      10m 38s
    5. Floating inline elements
      14m 34s
    6. Two-column floated layouts
      8m 17s
    7. Three-column floated layouts
      11m 30s
    8. Column height considerations
      7m 3s
    9. Creating equal-height columns
      10m 42s
    10. Floats: Lab
      5m 25s
    11. Floats: Solution
      5m 22s
  5. 51m 42s
    1. Relative positioning
      7m 59s
    2. Absolute positioning
      8m 59s
    3. Fixed positioning
      4m 23s
    4. Controlling stacking order
      8m 31s
    5. Clipping content
      8m 21s
    6. Controlling content overflow
      5m 38s
    7. Positioning elements: Lab
      3m 59s
    8. Positioning elements: Solution
      3m 52s
  6. 48m 46s
    1. Design considerations for fixed layouts
      3m 28s
    2. Establishing the layout grid
      7m 57s
    3. Defining column spacing
      9m 30s
    4. Applying the grid through CSS
      8m 56s
    5. Creating grid-based assets
      8m 26s
    6. Grid design resources
      6m 22s
    7. Building fixed layouts: Lab
      4m 7s
  7. 44m 26s
    1. Designing for flexible layouts
      2m 30s
    2. Calculating percentage values
      8m 36s
    3. Setting flexible width values
      6m 6s
    4. Making images flexible
      8m 10s
    5. Setting minimum and maximum widths
      7m 24s
    6. Building flexible layouts: Lab
      4m 53s
    7. Building flexible layouts: Solution
      6m 47s
  8. 49m 36s
    1. Responsive layout overview
      3m 49s
    2. Using media queries
      7m 16s
    3. Organizing styles
      8m 39s
    4. Making content responsive
      8m 33s
    5. Mobile design considerations
      7m 32s
    6. Building responsive layouts: Lab
      4m 23s
    7. Building responsive layouts: Solution
      9m 24s
  9. 1h 22m
    1. Creating multi-column text
      6m 36s
    2. Using borders to enhance design
      13m 59s
    3. Rounding corners
      6m 56s
    4. Adding drop shadows
      10m 35s
    5. Working with opacity
      6m 8s
    6. Utilizing the background property
      15m 5s
    7. Working with CSS sprites
      7m 58s
    8. Enhancing page design: Lab
      6m 22s
    9. Enhancing page design: Solution
      8m 38s
  10. 6m 25s
    1. Additional resources
      6m 25s

please wait ...
Understanding margin collapse
Video Duration: 7m 59s 8h 57m Beginner


Understanding margin collapse provides you with in-depth training on Web. Taught by James Williamson as part of the CSS: Page Layouts

View Course Description

CSS: Page Layouts introduces basic layout concepts, gives advice on how to create properly structured HTML based on prototypes and mockups, and goes into critical page layout skills such as floats and positioning. Author James Williamson shows how to combine these techniques to create fixed, fluid, and responsive layouts. Designers are also shown how to enhance their pages through the creative use of CSS techniques like multi-column text, opacity, and the background property. Exercise files are included with this course.

Topics include:
  • Reviewing the box model
  • Calculating em and percentage values
  • Controlling how elements display
  • Creating fixed, fluid, and responsive layouts
  • Structuring content with HTML5
  • Floating elements
  • Using relative, absolute, or fixed positioning
  • Defining column spacing
  • Creating grid-based assets and layouts
  • Considering mobile-design-specific issues
  • Working with multi-column text
  • Enhancing page design CSS Sprites

Understanding margin collapse

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.

There are currently no FAQs about CSS: Page Layouts.






Don't show this message again
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.

Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now Already a member? Log in

* Estimated file size

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.

Mark all as unwatched Cancel


You have completed CSS: Page Layouts.

Return to your organization's learning portal to continue training, or close this page.


Upgrade to View Courses Offline


With our new Desktop App, Annual Premium Members can download courses for Internet-free viewing.

Upgrade Now

After upgrading, download Desktop App Here.

Become a Member and Create Custom Playlists

Join today and get unlimited access to the entire library of online learning video courses—and create as many playlists as you like.

Get started

Already a member?

Log in

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

You started this assessment previously and didn’t complete it.

You can pick up where you left off, or start over.

Resume Start over

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.

Sign up and receive emails about and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from

Sign up and receive emails about and our online training library:

Here’s our privacy policy with more details about how we handle your information.

submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.