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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is this collapsing margins coming into play? 1

Status
Not open for further replies.
Dec 8, 2003
17,047
GB
I've got an issue whereby a floated DIV is being pushed down by the height of an adjacent paragraph's top margin.

This happens in Fx 2 and Safari 3, but doesn't happen in Opera 9 or IE 6... so I'm not quite sure which behaviour is correct. All the HTML & CSS validates, so I know there's no issue there.

I've tried many searches on Google and here, and keep coming back to collapsing margins, but I'm not sure if this is a red herring or not. Perhaps I've been staring at this code for far too long!

Anyway, I've made a cut down a test harness to illustrate the problem.

Is this collapsing margins, or is something else at play here that my brain-fart moment won't quite let me see?

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "[URL unfurl="true"]http://www.w3.org/TR/html4/strict.dtd">[/URL]
<html lang="en">
<head>
	<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
	<meta http-equiv="content-language" content="en">
	<title>Test</title>

	<style type="text/css">
		#mainDiv {
			background-color: red;
		}

		#sideDiv {
			float: right;
			width: 195px;
			height: 110px;
			margin: 0px 10px 5px 10px;
			background-color: blue;
		}

		p {
			font-size: 0.8em;
			margin: 10px 0px;
			padding: 0px;
		}

		p#title {
			font-size: 1.5em;
			margin: 0px;
			padding: 10px;
		}
	</style>
</head>

<body>
	<div id="mainDiv">
		<p id="title">A Title</p>
	</div>

	<div>
		<div id="sideDiv">A Side Box</div>
		<p>Lorem ipsum dolor sit amet, <a href="#">consectetuer adipiscing elit. Ut ac mi vitae elit congue iaculis.</a> Vestibulum ultrices interdum ante. Curabitur sit amet erat vel lacus adipiscing porta. Fusce id nibh vitae libero facilisis condimentum. Curabitur augue lectus, pretium non, egestas non, feugiat quis, urna. Ut suscipit lacinia erat. Cras hendrerit fringilla odio. Suspendisse diam. Pellentesque non arcu. Donec varius ligula non tellus.</p>
		<p>Phasellus auctor. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Cras mollis libero sed enim. Nunc et eros. Ut in lectus a lectus luctus condimentum. Proin at tortor eget purus venenatis placerat. Suspendisse id ipsum at enim lacinia porta. Vestibulum nonummy elementum magna. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Donec ullamcorper faucibus arcu. Aliquam luctus nisl hendrerit elit. Duis turpis erat, consequat ac, viverra a, lacinia id, pede. In hendrerit pede vel nisi. Ut vitae sem. Integer eleifend. Ut non pede id purus imperdiet pulvinar. Sed porta ipsum non nibh interdum congue. Nam felis libero, consequat non, feugiat eget, rutrum sit amet, nisl.</p>
	</div>
</body>
</html>

Thanks!

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Yup, that is the margin from the paragraph element that's pushing your floated div. Putting padding or border in the container div would work. For the test harness, the 1px red border will give a solution that is virtually unnoticeable.

___________________________________________________________
[small]Do something about world cancer today: Comprehensive cancer control information at PACT[/small]
 
That's great - I can mostly stop banging my head now :).

I can see myself needing to anchor the floated DIV snugly to the upper DIV without putting a border or padding on either of them. Now to work out how to do that!

Thanks,

Dan




Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Got it... I'm going with relatively positioning the floated DIV and giving it a -1px top value.

Works like a dream with the 1px top padding (which without, I didn't have a level playing field).

Nice!

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top