1

I have a <div> with an <h1> tag in it (at the moment that is the only thing. I am building this site top-down). I am getting a very large space above the title. When drilling down with firebug, the space is not included in the <body> or the <div>, but is a margin above the <h1> tag. Shouldn't the whole tag, margin and all, be surrounded by the parent container? is there any way to do this? At the moment that space is completely unstyleable, not belonging to any container.

For example:

<html style="background:green">
<head>
</head>
<body style="background:blue">
    <div style="background:red">
        <h1 style="background:yellow">Hello world!</h1>
    </div>
</body>
</html>

You can clearly see when displaying this that neither the <body> nor the <div> surround that margin.

Josh Darnell
  • 11,304
  • 9
  • 38
  • 66
Baruch
  • 20,590
  • 28
  • 126
  • 201

4 Answers4

5

you can try this code cause h1 has a default margin

h1{margin:0;}
Reda
  • 711
  • 6
  • 17
  • The question is really about why that margin is not included in the parent container. – Baruch Jun 06 '11 at 06:01
  • really I tried to figure it out but I did not fined a clear answer but by the way if you set a hight to the container and set h1 as position:absolute the margin will be add to the container really I don not know an explanation for that situation h1{position:absolute} div.container{height:200px;} – Reda Jun 06 '11 at 12:57
4

I found a similar question here, and following the links a found the answer at these sites:

Basically, it has to do with collapsing margins. To solve the problem I had to add a 1px padding to the container.

Community
  • 1
  • 1
Baruch
  • 20,590
  • 28
  • 126
  • 201
0

Using line-height: 0.65em; //tends to reduce or eliminate this over-lapping margin issue.

Darpan
  • 5,623
  • 3
  • 48
  • 80
0

If you're running into trouble with margin and padding from nowhere, you might want to try using a CSS Reset.

* { 
    margin: 0; 
    padding: 0;
} 

The above reset usually works for me.

Wex
  • 15,539
  • 10
  • 64
  • 107