+14  A: 

By default <p> and <h2> (among others) have margins and paddings associated with them. Trying adding the following to your CSS:

p, h2 {
  margin: 0px;
  padding: 0px;
}

Having padding won't affect the border and background of the elements, but depending on what you want, you might want to try removing them as well as I do here.

Edit: as Guffa pointed out in an answer below, the reason that the margins of the paragraph and heading are reaching outside their container DIVs is due to collapsing margins.

JoeCool
@KingNestor: Also ... DIVs don't have margin (or padding) by default, so you can get rid of those instructions.
rpflo
The sample code suggests that the gaps are between the divs, not the H1/P
Mr. Shiny and New
hmmm, you're right. I was targeting his written question at the end. Regarding the DIVs, I'm unfamiliar with this "margin: 0 auto" notation and perhaps that is the problem. @King have you tried just using "margin: 0px"?
JoeCool
Margin is showing on div level because of margin collapsing. You should combine your answer with Guffa's.
buti-oxa
@JoeCool: actually margin:0 on those elements would have solved the problem because of the collapsing margins issue. I didn't notice this at first because I tested a sample with borders, which un-collapses.
Mr. Shiny and New
This is NOT a good answer. You should be using a css reset to reduce the number of cross browser issues
gonzohunter
Rob Allen
@Joe Cool - margin: 0 auto; sets the left/right margins to automatic - essential centering the element in it's parent container. I think the over arching issue is the <p> tags are pushing the heights of the divs to be larger than the images. If you change the repeat value to repeat-y then you will likely see the image start again.
Rob Allen
@gonzohunter, this IS a good answer because it helped explain what was wrong. I went ahead and used a CSS reset file as suggested below, but that doesn't make this answer inaccurate.
KingNestor
A: 

Have you tried a css reset before adding your own css?

gonzohunter
+3  A: 

To avoid situations like this, use something like YUI Reset. It sets the default CSS to something predictable across browsers.

scvalex
A: 

I (usually) start every CSS file with the following to avoid getting caught by auto-padding/margins:

*
{
   margin-top: 0;
   margin-right: 0;
   margin-bottom: 0;
   margin-left: 0;
   padding-top: 0;
   padding-right: 0;
   padding-bottom: 0;
   padding-left: 0;
}

edit for clarity:

*
{
   margin: 0;
   padding: 0;
}

does the same thing.

Chuck
Doesn't "margin: 0; padding: 0;" do the same? Or are there some browsers that require enumeration?
JoeCool
It's just a matter of preference for me. Worked with too many people who didn't realize that and needed it spelled out to understand it.
Chuck
That's appalling. Again, I posit that such people have no business working with CSS. (although as an aside, I think root * selectors are something of a CSS code smell)
annakata
annakata - are you referring to me or the people who didn't realize that padding: 0; margin: 0; was the same thing?
Chuck
+3  A: 

That is because of collapsing margins. Margins aren't something that surround a single element, like padding. Instead the margin determines the distance between elements. If the parent element doesn't have margin or padding (like the div elements in your code), the margin of the child elements (h2 and p) determine the distance between the parent elements rather than the child elements.

For now you should generally avoid collapsing margins when the parent elements have a background set, as IE (at least up to version 7) doesn't handle collapsing margins correctly.

If you just set some padding on your div elements (it looks like you should have that anyway), the margins won't collapse outside them.

Guffa
+2  A: 

The problem is because of the collapsing margins. The margins on the h1 and p elements are contributing to the margins of the main div. You can fix this by either removing the margins on these elements:

h1,p { margin: 0; }

or by adding a border or padding to the div:

#main { padding: 1px; }

Also make sure your HTML is well-formed, you have a stray </a> in your sample.

Mr. Shiny and New