This would be very easy to do with a server-side language, but since this option was not put in the question, let's go to CSS.
In the examples below we used img
and div
, but the logic is for any element.
Using nth-child
Whenever you need something that happens with each% of items%, you can use n
(or other :nth-child
, depending on the context).
See an example:
.quatro {
display:block;
float:left;
margin:10px;
}
.quatro:nth-child(4n+1) {
clear:both;
}
<img class="quatro" src="http://placecage.com/100/100"><imgclass="quatro" src="http://placecage.com/100/100"><imgclass="quatro" src="http://placecage.com/100/100"><imgclass="quatro" src="http://placecage.com/100/100"><imgclass="quatro" src="http://placecage.com/100/100"><imgclass="quatro" src="http://placecage.com/100/100"><imgclass="quatro" src="http://placecage.com/100/100"><imgclass="quatro" src="http://placecage.com/100/100"><imgclass="quatro" src="http://placecage.com/100/100"><imgclass="quatro" src="http://placecage.com/100/100">
Tolearnmore:
What the "n", numbers and signs mean in the "nth-child" or "nth-last-child" selectors "?
Make a checkered HTML list
Using DIVs
Depending on the desired effect, simply set the width of your% s from% s to 25%. Just watch out for nth
:
.quatro {
width:25%;
float:left;
}
<div class="quatro"><img src="http://placecage.com/100/120"></div><divclass="quatro"><img src="http://placecage.com/100/120"></div><divclass="quatro"><img src="http://placecage.com/100/120"></div><divclass="quatro"><img src="http://placecage.com/100/120"></div><divclass="quatro"><img src="http://placecage.com/100/120"></div><divclass="quatro"><img src="http://placecage.com/100/120"></div><divclass="quatro"><img src="http://placecage.com/100/120"></div><divclass="quatro"><img src="http://placecage.com/100/120"></div><divclass="quatro"><img src="http://placecage.com/100/120"></div><divclass="quatro"><img src="http://placecage.com/100/120"></div><divclass="quatro"><img src="http://placecage.com/100/120"></div><divclass="quatro"><img src="http://placecage.com/100/120"></div><divclass="quatro"><img src="http://placecage.com/100/120"></div><divclass="quatro"><img src="http://placecage.com/100/120"></div>