Start learning with our library of video tutorials taught by experts. Get started
Viewed by members. in countries. members currently watching.
CSS gives Web designers control over the appearance of their web sites by separating the visual presentation from the content. It lets them easily make minor changes to a site or perform a complete overhaul of the design. In CSS Web Site Design, instructor and leading industry expert Eric Meyer reviews the essentials of CSS, including selectors, the cascade, and inheritance. The training also covers how to build effective navigation, how to lay out pages, and how to work with typography, colors, backgrounds, and white space. Using a project-based approach, Eric walks through the process of creating a Web page, while teaching the essentials of CSS along the way. By the end of the training, viewers will have the tools to master professional site design. Exercise files accompany the training videos.
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%.
There are currently no FAQs about CSS Web Site Design.
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.