Forum Moderators: not2easy
Any suggestions?
I don't know if this is your issue, but I will try to find what my solution was.
XtendScott
img {
display:block;
border:0px;
margin:0px;
padding:0px;
}
It was resolved for me with the "display:block;". It is/was an irritant to me for quite some time until I finally found that it was the XHTML 1.1 making the difference. I have a single header image, not sure what would happen with multiple images.
XtendScott
XtendScott.. thanks, if I remember there was an article on "Mysterious Gaps" on "Devedge" and used to be an allowable link, but Devedge is no longer.. the article pertained to the gaps caused by images
If it's a small (2-3px) gap then it is most likely the images and their alignment.
display:block; or vertical-align: bottom; should fix it. (using vertical-align should still allow you to use your two seperate images BonRouge)
An image being an inline element is aligning itself to the baseline of text by default, but that is not the bottom.. the baseline is the bottom of letters with no descenders like a, n, m whereas the bottom is where letters like g, j, p finish
If the gap is larger than about 10px then it's possibly down to a margin on an internal element (e.g. the list) collapsing/ combining with the divs margin and rendering on the div, this is natural behaviour although the browsers differ on where they actually render the collapsed margin.
To test if this is what's happening putting a border on the div should stop any gaps between divs by keeping the internal margin on it's internal element.. This is also a fix, but if you don't want a border (which could be the same as the background color) you can use 1px padding on the div instead..
Suzy
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Test Page</title>
<style type="text/css">
<!--body{background:black;}
#content{background:red;margin:0;border:0;padding:0;}
#footer{background:red;margin:0;border:0;padding:0;}
-->
</style></head><body>
<div id="content"><p>content</p></div> <!--content-->
<div id="footer"><p>Copyright 2004 austravel.com. All Rights Reserved.</p></div> <!--footer-->
</html>
it is collapsing margins. The margin (default) of the <p> elements are collapsing/combining with with their parent <div> element.
In order to stop the margin collapsing you need to put something that stops the two margins "touching" each other.. padding or border on the div will do it
div {background:red; margin:0;padding:0; padding: 1px 0;}
Suzy
it is how it's meant to be.. it's not a bug
btw only vertical (top/bottom) margins collapse, and I often think it helps to think of them as combining margins the word collapse suggests they should disappear?
references
CSS2 recs for Collapsing Margins [w3.org]
CSS2.1 recs for Collapsing Margins [w3.org]
and more talk here [google.co.uk]
Adding the padding does not bring them closer together (it actually moves them 1px apart).. but it is acting to force the combined margin to display on the child <p> instead of the parent <div>.
the Margins belong to the <p> (it has margins by default) but the parent <div> margin (even though it's zero) adjoins to(touches) the <p> margin, which is approx 1em by default).. collapsing rules state that it is the adjoining margin which should render so the margins are compared and the larger margin wins, and what you were seeing was that it is the parent (classed as "adjoining to" the child) which gets the margin.. in your case the <div> takes on the largest margin = your gap :)
from the recs:
The top margin of an in-flow block-level element is adjoining to its first in-flow block-level child's top margin if the element has no top border, no top padding, and the child has no clearance.
Why it's meant to be...if you simply had:
p {margin: 20px 0;}
<p>some text</p>
<p>some text</p>
those two paragraphs both have a 20px top and bottom margin so theoretically that means there should be a 40px gap between them. But in most cases that is not the desired effect.. you expect that the gap should be 20px so that's where collapsing does it's job
wrapping them <p>'s in a div does not alter this perception because <div> is a generic styling element it has a zero margin by default..
but becasue of the rules of collapse that puts it into the parent/child collapse scenario
paradiv
The art doesn't do justice here because the div has no padding but just imagine the top two lines are touching touch..
then to the box model
marginborderpaddingcontent box
If you were to see the box model as applied to <div><p> scenario it would look something like this
div marginp marginboth content boxes
that is the two margin edges are touching.. (therefore they will collapse/combine)
by adding either padding or border you stop the margin edges from touching by putting something between them..
div margindiv borderdiv paddingp marginboth content boxes
so the collapsing rules are being thwarted..
It can happen with anything that has a default margin.. <hn>, <p>, <form>, <ul>.. etc.. but it's only when divs are being used to provide background color, borders or images that people actually see the "gaps" because otherwise the space around the inner element is still correct.
Also a point to note that during all this IE does have a quirk (surprised?) in certain cases it doesn't handle collapsing very well, I've found that not relying on default margins is the best way to get it to conform.. e.g. specifically give all your elements margins do not rely on the default..
e.g. p {margin: 1em 0;}
Well have fun reading, and excuse the art ;)
Suzy
[edit] spelling, collapse collapse
[edited by: SuzyUK at 10:30 am (utc) on Oct. 26, 2004]