CSS - margin and float property

Posted by David Casillas on Stack Overflow See other posts from Stack Overflow or by David Casillas
Published on 2010-04-29T22:32:33Z Indexed on 2010/04/29 22:37 UTC
Read the original article Hit count: 560

Filed under:
|

1.- We have a div with static positioning. Inside we have a parragraph with a margin.

The heigth of the div will be the parragraph without the margin

2.- We have a div with float:left. Inside we have a parragraph with a margin.

The heigth of the div will be the parragraph plus its margin.

What is the explanation of this? Here is the html code and the CSS code. And here is a link to the test site. http://prueba.davidcasillas.es/

<!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="es">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<link href="index.css" rel="stylesheet" type="text/css" />
</head>

<body>

<div id="nivel1">
<div id="nivel21">
    <p>Este es el primer parrafo</p>
</div>
<div id="nivel22">
    <p>Este es el primer parrafo</p>
</div>
</div>

</body>

</html>

body {
margin:0;
padding:0;
}

#nivel1 {
border:solid;
border-color:#333;
margin:0;
background-color:#0F3;
margin:2em; 
}

#nivel21 {
margin:2em;
float:left;
background-color:#C00;

}

#nivel22 {
margin:2em;
background-color:#FC0;
}

© Stack Overflow or respective owner

Related posts about css-layout

Related posts about css-positioning