CSS Layout How to - Create flow down layout








Question

We would like to know how to create flow down layout.

Answer


<!--  w  w  w .  java 2  s . co  m-->
<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
.container, .container-with-classes {
  width: 340px;
}

.container:after, .container-with-classes:after {
  clear: both;
  display: table;
}

.container div, .container-with-classes div {
  width: 150px;
  margin: 10px 10px;
}

.container div:nth-child(2n+1), .container-with-classes .odd {
  float: left; 
  clear: left; 
  background-color: red;
}

.container div:nth-child(2n), .container-with-classes .even {
  display: inline-block; 
  vertical-align: top;
  background-color: green;
}

.container div:before, .container-with-classes div:before {
  margin-left: 5px;
  line-height: 25px;
  color: #FFF;
}
</style>
</head>
<body>
  <!-- with classes -->
  <div class="container-with-classes">
    <div class="odd" style="min-height: 300px;"></div>
    <div class="even" style="min-height: 260px;"></div>
    <div class="odd" style="min-height: 200px;"></div>
    <div class="even" style="min-height: 100px;"></div>
  </div>
  <!-- without classes -->
  <div class="container">
    <div style="min-height: 300px;"></div>
    <div style="min-height: 260px;"></div>
    <div style="min-height: 260px;"></div>
    <div style="min-height: 200px;"></div>
    <div style="min-height: 260px;"></div>
    <div style="min-height: 200px;"></div>
    <div style="min-height: 200px;"></div>
    <div style="min-height: 100px;"></div>
  </div>
</body>
</html>

The code above is rendered as follows: