New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

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

Controlling the space between elements

From: CSS: Core Concepts

Video: Controlling the space between elements

Most of the time we'll start off with an entirely new exercise file, but in this case I wanted to pick up where we left off in our last movie, since it's really a continuation of what we were just doing with line spacing. So if you didn't watch the previous movie, go back and watch that really quickly, so that you get to know what we are doing here. So if line spacing allows us to control the spacing between multiple lines of text what about the space between the elements themselves? Now for that we typically use margins. Now we're going to talk about margins and padding in a little bit more detail later on in another chapter.

Controlling the space between elements

Most of the time we'll start off with an entirely new exercise file, but in this case I wanted to pick up where we left off in our last movie, since it's really a continuation of what we were just doing with line spacing. So if you didn't watch the previous movie, go back and watch that really quickly, so that you get to know what we are doing here. So if line spacing allows us to control the spacing between multiple lines of text what about the space between the elements themselves? Now for that we typically use margins. Now we're going to talk about margins and padding in a little bit more detail later on in another chapter.

I just want to introduce them here as a means of controlling text formatting. So I'm going to preview this page before we do anything to it in a browser, just to sort of refresh our memory. And so in the last movie we set the line height property, which is dealing with the spacing between these lines of text. However, the spacing between the elements themselves is controlled by margins. I'm going to go back into our file that we were working with here, and we're going to be working with the element- spacing.htm and this is found in the 04_14. Now the structure of this page is exactly the same as the one we had in the last one, and pretty much to the syntax of the CSS is, exactly where it was when we left off with the last movie as well.

Now you'll notice that we don't have any margins going on in our document right now. So you might be wondering well, wait a minute what's controlling the spacing between them right now? Well, remember every single element has default styling that the user agent applies to it and those margins are part of that. So one of the first things you might want to do if you're really going to over ride these is to go ahead and strip them out. So I'm going to go all the way up to the top of my styles and just below, the sort of grouped aside in the article style, I'm just going to come in and I'm going to say, okay, body, h1, h2, h3, p, and then I'm just going to do margin of 0, and that's a very quick down and dirty, no-nonsense CSS reset.

We're not doing any padding or anything else, we're just sort of stripping out all of the margins. Now if I save this page, and I test this again in the browser, you can see what this does for us. It just goes ahead and gets rid of all the margins between it. Now the only thing controlling any spacing whatsoever is the line height, so now we need to go back in and sort of put in the proper margins that we want for our pages. Now one of the strategies that's really served us well up until this point with font sizing and line height is to just go ahead and apply one of the values that we want to the body tag, allow that to be sort of the default and let it inherit to everything else.

Doesn't quite work that way with margins, let's just experiment with that and see why? Now I do have a margin already applied to body, but I'm going to go down to the very bottom and do this again. And I'm just going to come in and I'm going to say margin: 1em. All right. Save that, and then I'm going to test this on my page. That's not we were expecting at all. Nothing happened with the elements, but everything else on the page gets sort of moved around and looks kind of weird now. Well, that's one of the things that you learned very early on about margins and padding. Those properties, unless you tell them to, do not inherit, and if you think about that that makes sense.

If you put 10 pixels worth of margin on the body and then every other element got 10 pixels worth of margins, pretty soon everything will just sort of be spreading away from each other in ways that you don't want. So I'm going to get rid of this. Now we need to go into the individual elements themselves and control other margins. So I'm just going to go right down here to the h1, h2, and p selectors. So here's what we're going to do. I'm going to start off by using instead of just margin, the margin-bottom property. So this is one of the nice things about margins and padding. You can apply them to whatever aside you want.

In this case since we're controlling the spacing between these elements I don't want to have any extra space above them so we're just going to do the margin-bottom, which is going to control the spacing below these paragraphs and headings. Okay, so here I'm going to change this to 24 pixels and I'm going to do that consistently for each one of these, margin-bottom: 24px margin-bottom: 24px. So we have the heading 1, heading 2 and the paragraph, all now have bottom margins of 24 pixels. So if I save this and preview this in my browser, okay, our spacing comes back which is nice, and then the spacing between the elements is all exactly 24 pixels, they're all exactly the same.

Now you might be saying to yourself, well, they don't look exactly the same. Well, do keep in mind that we do have a line-height, so the line-height basically is also on these bottom lines sort of pushing down a little bit below that as well. So what we have here is instead of a margin that is relative to the actual element itself or the size of that element, they're just all consistent and that doesn't give us a very nice flow to our page content. So I'm going to go back into our file and let's change this to a relative unit of measurement. So instead of 24 pixels, I'm going to go ahead and say 1em, and then I'm going to do the same thing here, 1em on the h2, and then a 1em for a paragraph and I'm going to save this.

Now you might be saying yourself, well, yeah, but you just swapped that out with 24 pixels with 1em, so you're still going to get the same space between them right? Not exactly. If I save this and test this in the browser, you can see that now our spacing is adjusted a little bit and the reason that it adjusts is because those em values are being calculated based on the size of that particular element. If we go to an h1, its font size is going to be 1.8ems, which is 1.8 times 16, and that's what the value of the bottom margin is going to be.

Same thing for h2, 1.4 times 16, that's going to be that value, and the paragraph spacing will be exactly, say 16 pixels if that's the default, because it's spaced off that as well. So even if you use a relative unit of measurement on an element like a heading 1 or heading 2, remember that its value is calculated based on that element's font size, not the default font size of the page, and that's a very important thing to keep in mind. Now you can also make these independent of each other. So if I go down to the h2, I could make that say maybe 0.2ems, and if I save this and test it, you can see what this does for us.

It just creates tighter spacing between our heading 2s and our paragraphs, whereas, the paragraphs and other elements and the heading 1 in our paragraphs have a greater amount of spacing. So you can tweak those values to sort of get that vertical rhythm that you're going for. Now there is a little bit more to using margins than this. We still haven't talked about vertical margin collapse, for example, but for now it's enough for us to know that the ability to control spacing between elements lies with the margin property and then if we begin to use relative units of measure for those margins, that the size of the margin will be based upon the size of that particular element, not the default size of the page.

Show transcript

This video is part of

Image for CSS: Core Concepts
CSS: Core Concepts

81 video lessons · 39382 viewers

James Williamson
Author

 
Expand all | Collapse all
  1. 4m 57s
    1. Welcome
      55s
    2. Using the exercise files
      4m 2s
  2. 1h 7m
    1. Exploring default styling
      4m 56s
    2. CSS authoring tools
      2m 29s
    3. CSS syntax
      4m 45s
    4. Writing a selector
      4m 10s
    5. Setting properties
      8m 40s
    6. Common units of measurement
      7m 47s
    7. Inline styles
      5m 1s
    8. Embedded styles
      5m 19s
    9. Using external style sheets
      10m 34s
    10. Checking for browser support
      8m 48s
    11. Dealing with browser inconsistencies
      5m 30s
  3. 2h 15m
    1. Structuring HTML correctly
      2m 51s
    2. Element selectors
      4m 52s
    3. Class selectors
      6m 4s
    4. ID selectors
      3m 27s
    5. Using classes and IDs
      10m 7s
    6. Element-specific selectors
      4m 35s
    7. The universal selector
      5m 42s
    8. Grouping selectors
      4m 49s
    9. Descendent selectors
      7m 32s
    10. Child selectors
      5m 7s
    11. Adjacent selectors
      5m 30s
    12. Attribute selectors
      12m 43s
    13. Pseudo-class selectors
      3m 54s
    14. Dynamic pseudo-class selectors
      8m 29s
    15. Structural pseudo-class selectors
      6m 45s
    16. Nth-child selectors
      13m 10s
    17. Pseudo-element selectors
      12m 40s
    18. Targeting page content: Lab
      8m 56s
    19. Targeting page content: Solution
      7m 59s
  4. 42m 39s
    1. What happens when styles conflict?
      4m 0s
    2. Understanding the cascade
      5m 47s
    3. Using inheritance
      6m 11s
    4. Selector specificity
      6m 55s
    5. The !important declaration
      4m 5s
    6. Reducing conflicts through planning
      3m 33s
    7. Resolving conflicts: Lab
      6m 45s
    8. Resolving conflicts: Solution
      5m 23s
  5. 1h 47m
    1. Setting a font family
      7m 10s
    2. Using @font-face
      9m 18s
    3. Setting font size
      7m 35s
    4. Font style and font weight
      6m 52s
    5. Transforming text
      3m 58s
    6. Using text variants
      2m 49s
    7. Text decoration options
      4m 26s
    8. Setting text color
      3m 2s
    9. Writing font shorthand notation
      8m 49s
    10. Controlling text alignment
      6m 33s
    11. Letter and word spacing
      9m 11s
    12. Indenting text
      4m 30s
    13. Adjusting paragraph line height
      10m 30s
    14. Controlling the space between elements
      6m 41s
    15. Basic text formatting: Lab
      8m 45s
    16. Basic text formatting: Solution
      7m 14s
  6. 2h 1m
    1. Understanding the box model
      16m 53s
    2. Controlling element spacing
      14m 29s
    3. Controlling interior spacing
      10m 49s
    4. Margin and padding shorthand notation
      6m 27s
    5. Adding borders
      8m 57s
    6. Defining element size
      10m 7s
    7. Creating rounded corners
      6m 58s
    8. Background properties
      2m 51s
    9. Using background images
      5m 10s
    10. Controlling image positioning
      10m 25s
    11. Using multiple backgrounds
      7m 5s
    12. Background shorthand notation
      5m 25s
    13. Styling container elements: Lab
      7m 55s
    14. Styling container elements: Solution
      8m 17s
  7. 47m 51s
    1. Color keyword definitions
      5m 4s
    2. Understanding hexadecimal notation
      6m 5s
    3. Using RGB values
      4m 58s
    4. Using HSL values
      5m 17s
    5. Working with opacity
      2m 23s
    6. Using RGBa and HSLa
      3m 8s
    7. Styling drop shadows
      5m 38s
    8. CSS gradients
      6m 32s
    9. Working with color: Lab
      4m 26s
    10. Working with color: Solution
      4m 20s
  8. 1m 58s
    1. Additional resources
      1m 58s

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

Are you sure you want to delete this note?

No

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.