tags:

views:

56

answers:

3

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"&gt;
<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;
}
A: 

I think When you use the float property the browser pads the element to show is floating.

Ravi Vyas
What you mean with "pads the element"?What exectly seems to happen is that the floated div calculates its heigth (as you can see by the background color) as the inner parragraph plus its margin, while the non floated div just the parragraph without its margin. There is no padding here.
David Casillas
what I meant was some the browser may be internally increasing the top and bottom padding to visually show that the element is floating and is not inline with others, But I am just shooting in the dark :P
Ravi Vyas
A: 

Well, since there is no question and no clear naming of your elements i'll just assume you want to put 2 columns in a container. Cleaned up code with some more clear naming and use of classes results in this: (hope this is what you were looking for)

<html>
  <head>
    <title>divs</title>
    <style>
      .container{
      float: left;
        background-color: #0F3;
      }
      .column {
        margin: 2em;
        float: left;
        padding: 5px;
      }
      #lefty{
        background-color: #C00;
      }
      #righty{
        background-color: #FC0;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div id="lefty" class="column">
        <p>Column number 1</p>
      </div>
      <div id="righty" class="column">
        <p>Column number 2</p>
      </div>
    </div>
  </body>
</html>
Bastiaan Linders
I'm really sorry I did not explain myself clearly.I'm just experimenting to learn the CSS properties. I discovered that when I switched a div from static to floated it changed the way its heigth was calculated, so I just made a test page with two equal div , just one static and one floated to study the diferent behaviours. I know now clearly what is happening with Safari Web Insector, the floated div adds to its heigth the inner parragraph plus its margin, while the static one don't adds the inner parragraph margin. Why this happens?
David Casillas
I think you misunderstand margins. A margin is added to the outside of a box, increasing it's distance from it's parant or siblings. Padding is a sort of inner margin, increasing the distance it's children have from it's borders.So, the question really is why the red one is bigger, not why the yellow one is smaller ;)
Bastiaan Linders
Ok, now that we have a good question, would be nice to hear an answer if any of you can give an explanation. Anyway thanks, and I will try to be more precise next time.
David Casillas
A: 

Try reading this blog about CSS Positioning, it may help you frame your question more effectively.

Mike
Thanks, I have been reading "The Ultimate CSS Reference", and have discovered that there should be a margin collapsing issue. In your link this is confirmed. This is what its said:"Second, unlike boxes in the normal flow, the vertical margins of a floated box are not collapsed with the margins of boxes either above or below it."So this seems to close the subject.
David Casillas