If you prefer to continue using the float
property (as shown in your initial query), you can apply this specific CSS rule:
.box:nth-child(5n+1) {
clear: left;
}
This directive will shift every fifth plus one instance of the .box
element (e.g., the 6th, 11th, 16th, and so on) onto a new line. Please keep in mind that there should be no other elements interspersed among them at the same HTML level for this method to function correctly.
(please note: I reduced the size of the boxes to better demonstrate the outcome within the snippet window)
.box {
border-color: #32ff00;
border-width: 4px;
border-style: dotted;
width: 80px;
height: 100px;
float: left;
margin: 5px;
}
.box:nth-child(5n+1) {
clear: left;
}
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>