CSS Layout How to - Get div tags side-by-side by float left








Question

We would like to know how to get div tags side-by-side by float left.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
.child_div {<!--from w  ww  .  j  a  va 2s  .  c  o  m-->
  float: left;
}

.parent_div {
  clear: both;
}

div div {
  border: thin black solid
}
</style>
</head>
<body>
  <div class='parent_div'>
    <div class='child_div'>text1</div>
    <div class='child_div'>text2</div>
  </div>
  <div class='parent_div'>
    <div class='child_div'>asdf</div>
    <div class='child_div'>asdf</div>
    <div class='child_div'>asdf</div>
  </div>
</body>
</html>

The code above is rendered as follows: