Forum Moderators: not2easy

Message Too Old, No Replies

Simple float and margin issue

         

markwm

4:28 pm on Mar 16, 2007 (gmt 0)

10+ Year Member



Can anyone please explain why in the following code the margin gets applied to both the image and paragraph?

I just wanted the image floated left, and then the paragraph to start half way down the image on the right but it seems the margin-top applied to the paragraph pushes the image down as well?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>untitled</title>

<style type="text/css">
img {
float: left;
}
p {
margin-top: 150px;
}
</style>

</head>
<body>

<img src="header.png" height="300" width="200" alt="" />
<p>text</p>
</body>
</html>

Fotiman

5:08 pm on Mar 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This behavior is known as "Collapsing Margins" and is documented in the CSS Spec [w3.org]. You probably find more information if you Google for Collapsing Margins, but there's also info on this site.

If you were to add a top border or top padding to the image, then the margins would no longer collapse.