CSS Layout How to - Float div according to 2nd div in hierarchy








Question

We would like to know how to float div according to 2nd div in hierarchy.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
#container {<!--from   w w w.ja v  a 2 s.  c o m-->
  width: 500px;
  height: 400px;
  border: 1px solid black;
}

#div1and2 {
  float: left;
}

#div1 {
  width: 50px;
  height: 50px;
  background: red;
}

#div2 {
  width: 50px;
  height: 100px;
  float: left;
  background: yellow;
}

#bigdiv {
  width: 350px;
  height: 250px;
  float: left;
  background: blue;
}
</style>
</head>
<body>
  <div id="container">
    <div id="div1and2">
      <div id="div1">div1</div>
      <div id="div2">div2</div>
    </div>
    <div id="bigdiv"></div>
  </div>
</body>
</html>

The code above is rendered as follows: