Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Margin collapsing

From: CSS Web Site Design

Video: Margin collapsing

In this video we're going to take just a minute to talk about the process of margin collapsing. It probably sounds very boring or alternatively perhaps catastrophic but it's really neither. This is a process by which margins are in a sense, combined in order to provide the layout source flats that we might expect. The first thing we're going to do actually is get rid of the margin on paragraphs, simple enough, we're just going to say no margin on paragraphs, and if you look at the paragraphs in this main content, where the image has been removed in order to make this little easier to understand. Watch what happens to the paragraphs when we hit Reload.

Margin collapsing

In this video we're going to take just a minute to talk about the process of margin collapsing. It probably sounds very boring or alternatively perhaps catastrophic but it's really neither. This is a process by which margins are in a sense, combined in order to provide the layout source flats that we might expect. The first thing we're going to do actually is get rid of the margin on paragraphs, simple enough, we're just going to say no margin on paragraphs, and if you look at the paragraphs in this main content, where the image has been removed in order to make this little easier to understand. Watch what happens to the paragraphs when we hit Reload.

They all jam up together. There's no space between paragraphs anymore because the quote and quote blank line between paragraphs has nothing to do with line feature, character change or anything like that, it's a margin, it's a margin effect, by default paragraphs have 1em of top and bottom margin. So we have no margin here. Let's put in some padding. I said before that by default paragraphs of have 1em of 1em of top and bottom margin, why not 1em of top and bottom padding? Well, the interesting thing there is, if we do that, and we go back and reload, there's a lot of space between these paragraphs.

Ok, now what's happening is probably easiest to see if we put let's say a bottom border, I'll make it really obvious, ok, so we have dashed red border on each of these paragraphs and there's 1em of padding on the bottom of each of these and 1em of padding on the top of each of these. All right, but that means that there is 2em of space between each of these paragraphs.

The 1em of bottom padding plus the 1 em of top padding in the following paragraph. The border here is just helping us see the difference between where the, where the border is between the two, effectively where they meet up. So let's flip that around. Let's have margin of 1em 0 and padding of 0. All right. And these guys get up very nice and close to each other. Now the thing is it's not that there's no bottom margin here, remember, margins are outside of border, so these, the bottom borders now are along the bottom edge of each paragraph's content.

We'll take those out so that it's a little easier to see. There's really only 1em of space here and that's because of margin collapsing. what happens when you have two margins that are vertically adjacent effectively, so if you have a paragraph followed by a paragraph, and the first of the two paragraphs has a 1em bottom margin and the second of the two paragraphs has 1em top margin, you have them slide together until one of those margins touches the border of the other element. So, in a way you can think of margins like strips of transparent plastic that go around your elements and everything else, the borders, the padding and the content has a piece of paper around which those, the transparent plastic margins go and when you have two adjacent margins, the transparent plastic margins slide over each other until the edge of one of those strips of plastic touches the edge of paper on the other margin. At that point they stop. Ok.

They don't get any closer. So, that's in effect how margin collapsing works. That's at least the basic bit. Now, here's where margin collapsing comes in, in ways that you might not expect. I'm going to, for the moment, make sure that the masthead has no top padding and if I hit Reload, notice that suddenly blank space opened up on quote and quote space. That's actually a margin inside the masthead.

The reason that that happens is that there's an element in there, that has a top margin that's sticking out of the masthead div. That's completely legal, that can happen and there are reasons why that's a good idea, they don't make sense in this circumstance, but this isn't the circumstance for which margin collapsing was envisioned. It makes sense when you have a whole bunch of elements and lists. For example, without margin collapsing lists would get very, very, or at least could get very, very spacious and there would be a lot of margins in an strange things would happen at the beginning and end of lists. So it's just one example, but anyway, there's a margin sticking out of this masthead.

The way that we get around that, well, there are two ways that we can get around it. One is to make sure there's no top margins or bottom margin sticking out of something like a masthead div, but that's not always really practical. the other thing that you can do is put either a border or padding across the top or the bottom of something like the masthead div. So in this case, instead of a padding-top of zero, we might say padding-top 1px. I doesn't matter how much top padding there is in this case, there just needs to be some, and then reload that top padding make sure that he margin doesn't stick out of the div and give us some problems here. We're going to do the same thing with a top border. We could've had a solid top border, a one pixel solid border that was the same color as the background color of the elements, and that would've had the same effect as the one pixel top padding.

So you know, whichever one you prefer, using margins or padding, in these sort of situations is really kind of up to you, is just something to watch out for with margin collapsing. If you ever have a case where you have an element that there's blank space before it or after it, or there's a quote and quote blank line before or after it, that's something to watch out for is the possibility there might be margins sticking out of it. In those cases, if you say, geez, I've have got this box with this background color, but it has this blank space it's occurring above or below it, just give that box a border quickly, you know, just a one pixel border, just for a second to see if the one pixel border, could you know, basically fixes the problem and it does and that means you have a margin sticking out of that div. If it doesn't, that means that there's something else you need to look for, that's creating the blank space, may be an element right before it that is pushing it down. So, that's margin collapsing one of the little fiddly mechanisms inside CSS that 98% of the time makes your life easier, but the other 2% of the time is very exasperating and so knowing about it helps you eliminate that 2%.

Show transcript

This video is part of

Image for CSS Web Site Design
CSS Web Site Design

73 video lessons · 61041 viewers

Eric Meyer
Author

 
Expand all | Collapse all
  1. 14m 34s
    1. Welcome
      28s
    2. What is CSS?
      5m 34s
    3. Design tour
      2m 38s
    4. Using the exercise files
      1m 20s
    5. Installing the Web Developer toolbar
      4m 34s
  2. 25m 56s
    1. XHTML essentials
      3m 55s
    2. CSS essentials
      5m 17s
    3. Embedded style sheets
      2m 20s
    4. Linking a style sheet
      2m 19s
    5. Linking to multiple style sheets
      2m 20s
    6. Using linked and embedded style sheets together
      4m 21s
    7. Using imported style sheets
      5m 24s
  3. 57m 48s
    1. ID selector essentials
      6m 38s
    2. Class selector essentials
      4m 9s
    3. Best practices for classes
      4m 52s
    4. Grouped selection
      4m 2s
    5. Descendant selectors
      6m 44s
    6. The sources of style
      6m 38s
    7. Specificity
      8m 21s
    8. Making things important
      4m 32s
    9. Inheritance essentials
      5m 12s
    10. Making things really unstyled
      4m 2s
    11. User style sheets
      2m 38s
  4. 39m 3s
    1. Box model essentials
      7m 35s
    2. Simple floating
      5m 3s
    3. Using float for layout
      5m 5s
    4. Fixing column drop
      5m 35s
    5. Clearing essentials
      4m 20s
    6. Float containment
      6m 35s
    7. Creating a navbar from a list
      4m 50s
  5. 38m 3s
    1. Coloring text
      4m 13s
    2. Defining color in CSS
      8m 12s
    3. Coloring backgrounds
      6m 35s
    4. Applying background images
      4m 19s
    5. Manipulating the direction of background images
      2m 52s
    6. Positioning backgrounds
      7m 23s
    7. Background shorthand
      4m 29s
  6. 58m 28s
    1. Altering line height
      7m 32s
    2. Font style and weight
      5m 45s
    3. Sizing fonts
      9m 59s
    4. Using font families
      10m 38s
    5. Font shorthand
      6m 5s
    6. Justifying text
      4m 56s
    7. Vertically aligning text
      4m 22s
    8. Transforming text
      3m 49s
    9. Text decoration
      5m 22s
  7. 44m 40s
    1. Margin essentials
      14m 21s
    2. Adding borders
      6m 52s
    3. Padding
      9m 17s
    4. Using negative margins
      7m 19s
    5. Margin collapsing
      6m 51s
  8. 20m 38s
    1. Styling tables and captions
      5m 23s
    2. Styling table cells
      6m 30s
    3. Styling a column with classes
      4m 51s
    4. Styling links inside table cells
      3m 54s
  9. 30m 40s
    1. Styling for specific mediums
      4m 3s
    2. Creating a print style sheet
      6m 35s
    3. Hiding layout for print
      4m 11s
    4. Styling for print
      6m 34s
    5. Complex styling for print
      4m 37s
    6. Creating a footer
      4m 40s
  10. 37m 23s
    1. Getting started
      1m 11s
    2. Setting global styles
      4m 4s
    3. Defining masthead and navbar colors
      3m 27s
    4. Layout of the navlink bar
      3m 38s
    5. Using columns
      4m 49s
    6. Setting content styles
      1m 53s
    7. Creating the sidebar boxes
      5m 42s
    8. Creating the sidebar form
      3m 23s
    9. Completing the sidebar
      3m 29s
    10. Making a table
      3m 12s
    11. Creating a footer
      2m 35s
  11. 1m 28s
    1. Goodbye
      1m 28s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
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

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

Congratulations

You have completed CSS Web Site Design.

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


OK
Become a member to add this course to a playlist

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

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

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:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com 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 lynda.com.

Sign up and receive emails about lynda.com 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.