HTML Element Style How to - Create Column Layout with variable height DIVs








Question

We would like to know how to create Column Layout with variable height DIVs.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
div>div {<!--   ww  w.j a  va 2  s. c  o m-->
  background-color: lightgray;
  width: 33%;
  float: left;
  border: 1px solid white;
  box-sizing: border-box;
  height: 100px;
  text-align: center;
}
/* Add some variable height items */
div>div:nth-child(1n+2) {
  background-color: red;
  height: 40px;
}

div.wrapping-divs>div:nth-child(3n+1) {
  clear: left;
}
</style>
</head>
<body>
  MUCH BETTER!
  <div class="wrapping-divs">
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
    <div>6</div>
  </div>
</body>
</html>

The code above is rendered as follows: