CSS Layout How to - Collapse Margin Float








Question

We would like to know how to collapse Margin Float.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
html, body {<!--  w w  w . java2  s  .  c  o m-->
  margin: 0;
  padding: 0;
}

.test {
  background: black;
  height: 200px;
  width: 200px;
}

#footer {
  background: yellow;
  margin: 20px 0 0 0;
}

#containing-block {
  width: 700px;
  background: #eee;
  float: left;
  margin: 50px;
}
</style>
</head>
<body>
  <div id="containing-block">
    <div id="left" class="test" style="float: left;"></div>
    <div id="right" class="test" style="float: right;"></div>
    <div id="footer">footer</div>
  </div>
</body>
</html>

The code above is rendered as follows: