If I have the following html:
<body>
<div id="mainTop">
</div>
<div id="main">
<h2>
<%= Html.Encode(ViewData["Message"]) %></h2>
<p>
To learn more about ASP.NET MVC visit
http://asp.net/mvc</a>.
</p>
</div>
<div id="mainBottom">
</div>
</body>
With the following CSS:
#mainTop
{
background: url('/Content/Images/bg_top.png') no-repeat;
width: 963px;
height: 65px;
margin: 0 auto;
text-align: left;
color: #5d676d;
}
#main
{
background: url('/Content/Images/bg_middle.png') repeat-y;
width: 963px;
min-height: 50px;
margin: 0 auto;
}
#mainBottom
{
background: url('/Content/Images/bg_bottom2.png') no-repeat;
width: 963px;
height: 128px;
margin: 0 auto;
}
It looks like this:
Why do certain tags like <p
> and the heading tags cause gaps in my layout? Ideally, I would like to not have those huge spaces in between my content.
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.
To avoid situations like this, use something like YUI Reset. It sets the default CSS to something predictable across browsers.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With