Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Blank line with FF and Opera. Oh the same of it. 2

Status
Not open for further replies.

SunnyByfleet

Technical User
Feb 24, 2003
146
0
0
GB
I have a minor irritation on one of my sites. It renders fine in IE6 and 7, but inserts an extra blank link in FF and Opera.

I have produced a skeleton bit of code below which demonstrates the problem. it is self contained, and if you run it you should get the following in IE:

Green body background, with a centred pink box displaying the phrase "one line of random text". There are no gaps between the pink box and the top of the browser on all browsers, which is what I want.

Below the line of text is a grey box, with loads of random text.

Now, on IE, the grey box is directly below the "one line of random text". However, on FF and Opera, there is an extra blank pink line above the box. It is almost as if a rogue <BR> has been inserted, which of course it hasn't.

Now, I fully appreciate the FF and Opera are probably the ones behaving themselves, in which case, what am I doing wrong? I can get rid of the blank line by giving the grey box a negative top margin, but that just seems wrong. What is happening?

Anyway, here is the code:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]

<html>
<head>
<style>

body {
	padding: 0px;
	margin: 0px Auto;
	width: 600px;
	color: #000;
	background-color: #aaffaa;
}


#outer 
	{
	padding: 0px;
	margin: 0px;
	background-color: #FF8888;
	width: 600px;
	font: 9pt "Lucida Grande", Arial, Sans-serif;
	}
	
#inner
	{
	width:570px;
	margin: 0px auto; 
	padding: 0px;
	text-align: center;
	background-color: #ddddff;
	}
	

</style>

    <title>Border Test</title>
</head>

<body>
    <div id="outer">
        One line of random text
		
        <div id='inner'>
                <h3>Let's go!</h3>

                <p>Caesar adsum iam forte Caesar adsum iam forte Caesar adsum iam forte Caesar adsum iam forte Caesar adsum iam forte Caesar adsum iam forte Caesar adsum iam forte Caesar adsum iam forte Caesar adsum iam forte Caesar adsum iam forte Caesar adsum iam forte Caesar adsum iam forte Caesar adsum iam forte Caesar adsum iam forte Caesar adsum iam forte Caesar adsum iam forte Caesar adsum iam forte Caesar adsum iam forte Caesar adsum iam forte Caesar adsum iam forte Caesar adsum iam forte Caesar adsum iam forte Caesar adsum iam forte Caesar adsum iam forte Caesar adsum iam forte Caesar adsum iam forte Caesar adsum iam forte Caesar adsum iam forte Caesar adsum iam forte Caesar adsum iam forte Caesar adsum iam forte Caesar adsum iam forte Caesar adsum iam forte Caesar adsum iam forte Caesar adsum iam forte Caesar adsum iam forte Caesar adsum iam forte Caesar adsum iam forte Caesar adsum iam forte Caesar adsum iam forte</p>

        </div>
    </div>
</body>
</html>
 
I think it's the top margin of the h3 poking out of the top of div#inner. If you have multiple nested elements, with no borders or padding, their margins are combined. It's a phenomenon known as "margin collapsing", and, yeah, it's standard-compliant behaviour.

Take a look at for a good explanation

-- Chris Hunt
Webmaster & Tragedian
Extra Connections Ltd
 
Thanks for the links. I've actually got that chap's book as well, so perhaps I need to reread it?

It was indeed a rogue H3 at work and margin collapsing. From what I read there are various possible solutions to try:

- Add a border (with a border the two margins are no longer adjacent so don't collapse)
- Position the element, either relative or absolute.
- float the element.

All the above worked in my full example to a greater or lesser extent. It was all a bit like plugging a dam though. IE you repair one crack only to find your repair causes another thing to break.

I eventually solved the issue using the overflow tag. I had read somewhere that you could use overflow: auto in your layout as an easy way of clearing floats. I tried using it on the containing element in my example, and it worked! On all three browsers!!

The only problem was that there was now a scroll bar at the right of the container.

So, I changed it to overflow: hidden (out of desperation rather than anything else) and the scroll bar went away, as I knew it would. However, the formatting remained consistent!

Thank you for your help. At the very least it told me that I wasn't going mad...

Personally I hate collapsing margins, or anything where the computer doesn't do exactly what you tell it to do, but each to their own.

 
From what I read there are various possible solutions to try:

You missed the obvious one, which I pointed out in my first post: set the top margin on that H3 to be 0.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Well I could have done that, but it would have only worked in that instance. On my site, there are any number of elements that will be in the place of the H3. Using overflow: hidden on the container works on all of them.
 
If you start off by resetting the padding and margin on all elements you'll be in a much better position that leaving it up to each browser to add their own defaults e.g.
Code:
* {padding:0;margin:0;}

Mark,

[URL unfurl="true"]http://lessthandot.com[/url] - Experts, Information, Ideas & Knowledge
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Website Design
[URL unfurl="true"]http://aspnetlibrary.com[/url] - An online resource for professional ASP.NET developers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top