Watching:

How to Manage Vertical Margin Spaces in Dreamweaver CS5


show more Controlling vertical spacing with margins provides you with in-depth training on Web. Taught by James Williamson as part of the Dreamweaver CS5 Essential Training show less
please wait ...

Controlling vertical spacing with margins

When controlling typography in layout, one of the biggest challenges is in dealing with vertical spacing. Block level elements like paragraphs, headings, and lists are spaced vertically by using margins. On the surface, that sounds pretty straightforward and simple, however, there are two things that typically trip people up when attempting to control spacing through margins. First, vertical margins collapse. If you set a bottom margin on an element and a top margin on the element below it, instead of getting space equal to the two margins, you get spacing equal to the highest value.

At first, this seems pretty counterintuitive until you realize that without margin-collapse, all paragraphs, headings, and other elements would be double spaced. Now the other small little detail is that every browser has its own default style sheet with its own preset values for vertical margins between elements. Without compensating for, or even eliminating, these default values, your spacing can be hard to predict and inconsistent. We will control our vertical margins by first reviewing how we eliminate any default margins that browsers might have, and then by setting margins that take into account vertical margin collapse.

So here I have the very longly named tour_detail_bigsur_trails.htm. One of the toughest things to do is sort of get rid of the default margins and padding that come inherent within a browser. There are a lot of different opinions about doing this. And if you want to read more about this, go out to Google and do a search for CSS Reset. That's what this technique is typically referred to. There are any number of blogs, pages out there, where people are talking about the pros and cons of CSS Resets.

Some people do these really detailed ones, some people don't do them at all, and some people do sort of a small flavor of one. Essentially, what they're designed to do is this: They're designed to allow you to have control over the vertical spacing of your element, because if you leave the browser's default margins in there, chances are that margin might overwrite yours or might be larger than the one that you called for, and you don't achieve your desired spacing. But the downside of this is if you zero out all the default margins for a browser, that means that you have to manually put them back in for every single element.

And sometimes you could forget to do that, and you end up with really weird-looking spacing. So what we're doing in our file is what I like to call a limited CSS Reset. That means that only a few of the elements that we're going to be using throughout our site are reset, and that they're really just the ones that we're going to use over and over and over again. Now, let's take a look at our reset. I'm going to go right over here to our tour_detail_bigsur_trials.htm document, and I'm going to click on the main.css link. That's going to take me to my main style sheet and notice that it opened up in Split View.

I'm going to scroll down, and at about line number 48, we can see our limited CSS reset. We're doing all of our headings, our paragraphs and address tag, blockquote - those are both common and block level elements - div tag, the unordered list, and list item. Notice that we didn't do unordered list, maybe we're not using a lot of those. The key here is that we're only doing select elements. It's a much shorter list. We don't have to remember to do it for every single element.

We just have to remember to do it for all of our headings, our paragraphs, if we're using addresses or blockquotes, any div tag that we want - which by the way that's kind of a just-to-be safe, because most div tags don't have a default margin in browsers - and then unordered list and list items. Since those now have zero padding and zero margins, we're going to have to explicitly set those in our styles. I'm going to switch back over to Design view, and we can see the result of that CSS Reset. You can these headlines: Backpack Cal, Big Sur Retreat, Hiking Trail Information.

All those guys are right on top of each other. Now, these paragraphs have some spacing, but that is due to Line-height, not vertical margins. And you might also say, "Well, wait a second. I see some vertical spacing between this paragraph and this heading." Well, that's true, but that's being caused by this graphic. It has a float applied to it, which moves it to the right, and then this heading has a clear property applied to it, which says, "No I have to go below you." So that's why we are seeing the spacing there. You can see our lack of spacing returns again, between this heading and this paragraph, that paragraph and that list, and all of those list items.

So those are elements that if we want to control their spacing, we need to put those vertical margin values in there manually, and that is our next task. Now, what we're going to do first is target our main article heading right there, Backpack Cal. Now you can do this in the CSS Styles panel, you can hand-code it, whatever you'd like to do. I'm going to scroll through here until I find #mainContent #mainArticle h1. Now, I'm going to collapse my Files panel by double-clicking the tab there. It's going to give me a little bit more room here and here, and it's going to make it easier for us to find these selectors.

So I highlight the #mainContent # mainArticle h1, and I'm going to add a property to this. I want to add a margin-bottom. So we can target just the margin on the bottom of our elements by saying margin-bottom. You could also grab the pulldown menu, and you could see you have values for overall margin, margin-bottom, margin-left, margin-right and margin-top as well. All right. So for the margin-bottom here, we want to set a value of .4ems. We'll hit Return.

We can see that now we get some additional vertical spacing between Backpack Cal and Big Sur Retreat. So we've adequately spaced that out for what we're looking for. Now remember, 1em would be 100% of the size of that heading. .4 means not quite as far down as the size of the heading. Okay. Let's keep going. I am going to go down to the next selector, which is #mainContent #mainArticle h2. That's going to target our headings 2s. We're going to add a property here. Here, we want to add the margin-top.

So margin-top of 1.2ems, and I want to add the margin-bottom of 1em. Okay, so what in the world was I just doing there? Well, the 1.2em basically says, "I want more space above me than below me." And that's going to give a nice visual separation to let folks know that you are starting a section or a new subheading. So we increase the space that we see here. Now, that's not cumulative. We didn't do 1.2+.4ems.

In actuality, we're only getting 1.2ems there because of that\ collapsing vertical margin. Now remember, Hiking Trail Information is still inside that heading 2. That's all one heading 2. We're using a Span tag to move that down to the line below it. So the 1em bottom margin is occurring between this paragraph and between that heading. Now speaking of this, this little span right here, it can have its own margin if you'd like. We need a little bit of space between this text and the Span tag.

So I'm going to go right down to the next selector, which is #mainContent #mainArticle h2 span.tourCost. I know that seems really long, but it's just descriptive of where that is. Now, here I'm going to add a property to this, and I'm going to add a margin-top. So I want to give it a little bit of spacing between the Big Sur Retreat text and the Hiking Trail Information. So margin-top is going to let me to do that. And I'm just going to do a margin-top of .25ems, so a quarter of the available font size.

You can see that it gives us a little bit of spacing but still keeps them together as a unit. Let's keep going. Let's go down to #mainContent # mainArticle h3, and here I'm going to add a property to that. I'm going to add margin p - now, here is something interesting. I want to do some short-hand notations so you guys can see that. If you need more than just top or bottom, right, left, that sort of thing, it's a little bit tiring to always have to go margin-top and then margin-bottom. Well, you can do shorthand notation. So instead of saying margin-top, margin -bottom, margin-right, margin-left, you can just say margin, and then you can pass along any values that you want.

Let me show what I mean. So I'm going to type in margin, hit Tab to go over to the value section of that, and then I'm going to type in 1.25em, then a space, and the space is very important, then a zero, then another space and then do .5em and hit Return. Now, what does that mean? Let's talk about this for a moment. In the margin value, you can pass one value, two values, in our case three values, or four values. Let's talk about what those would do for you. If you pass a single value, that's a margin all the around the element.

So you're looking at top, right, bottom and left being all the same value. If you have two values, the first two values represent the top and the bottom. The second of the two values represents the right and the left. Well, what if you do three values, like we've done here? The first value 1.25em is the top margin. The middle value here represents the right and the left. And finally, the .5ems is the bottom margin. Well, what if you have four all the way around? Well, just imagine a clock.

You do top-margin first, then right, then bottom, and then left. Some people remember that with the mnemonic TRBL, top, right, bottom, left. But I just remember the hands of a clock. So it works for me. So essentially, what we've done, and I'm going to scroll down and find where heading 3 is. There we go. Notice that we have more space above the heading 3 than below it. And again, that gives us a nice separation between the section above it and then the section below it. Now finally, for our main body text, let's choose our paragraph, so #mainArticle p. I'm going to add a property to that as well.

Margin-bottom of 1em and that's going to control the spacing below the paragraph. Now you might have noticed that while it affects the spacing between the paragraph and the list, it didn't affect the spacing between the paragraph and the heading. Again, the reason for that is margin-collapse. This value of the heading top is actually higher than the value of the margin-bottom for the paragraph. Because of that, the higher value wins. They don't add up. It's not cumulative, and we're left with the same spacing we had before.

That's a really neat technique, because you can assure that paragraphs have a default spacing below them. But then any subheading that comes along, you could add a little bit extra margin above it, and give yourself that separation within your text. Okay we're almost done. The last thing we have to do are these list items. Those guys are all jammed together. So we're going to give those a little bit of extra spacing as well. So I'm going to scroll down until I find #mainContent #mainArticle li. Once I find that, I'm going to go ahead and add another property here, and the property that I'm going to add here is margin.

Once again, we're just going to do margins all the way round. So remember that four values I talked about? The top value is going to be zero, and then do a space, right value is going to be zero and then add a space. So no top margin, no right margin. But for bottom margin, we're going to do 1.2ems and then another space. And for left margin, what this is going to do is its just going to indent our entire list for us, so that's it's a little bit more like a list. We're going to 2.4ems. And when I hit Return, there's our list. We're getting 1.2ems worth of space between them.

And then right here, we're getting 2.4ems of space, which is sort of pushing that list over and indenting that. Sweet! So now our typography looks a little better. If I switch over to Live View, I can see a more fully rendered version of that. We have some spacing that's making that a little bit easier to read. Now, making sure the elements are spaced property is fairly easy to achieve by using vertical margins. Just make sure you account for both browser default margin values, which we did earlier on by zeroing those guys out, and margin collapse when planning your styles.

If you do that, you should be able to achieve consistent element spacing throughout your site.

Controlling vertical spacing with margins
Video duration: 12m 3s 15h 22m Beginner

Viewers:

Controlling vertical spacing with margins provides you with in-depth training on Web. Taught by James Williamson as part of the Dreamweaver CS5 Essential Training

Subject:
Web
Software:
Dreamweaver
Author:
please wait ...