Yet another approach, alongside other solutions, is to include overflow: hidden;
in your .row
CSS.
The reason behind the observed behavior is that using float
pushes the div outside of the normal document flow. As a result, the div occupies no space within the document.
This logic becomes clear when considering the typical use case of floating an image to wrap text around it. The following p
tag, for instance, is positioned as if the floated image didn't exist, potentially overlapping with the image. Subsequently, the browser wraps the text within the 'p' tag around the image. (Without removing the floated image from the flow, the p
tag would naturally display below the image—failing to achieve the desired effect.)
Below is a sample code implementation:
HTML:
<div class="row">
<div class="col">1</div>
<div class="col">2</div>
<div class="col">3</div>
<div class="col">4</div>
</div>
<div class="row">
<div class="col">5</div>
<div class="col">6</div>
<div class="col">7</div>
<div class="last">8</div>
</div>
CSS:
.col
{
float: left;
width: 25%;
}
.row{
border: 1px solid green;
overflow: hidden; /* Alternatively, "overflow: auto;" can be used */
width:100%; /* Aiding older IE versions */
}