This would be very easy to do with a server-side language, but as it was not put this option in the question, let’s go from CSS.
In the examples below were used img
and div
, but logic serves for any element.
Using Nth-Child
Whenever you need something that happens to every n
items, you can use the :nth-child
(or others nth
, depending on the context).
Take 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">
<img class="quatro" src="http://placecage.com/100/100">
<img class="quatro" src="http://placecage.com/100/100">
<img class="quatro" src="http://placecage.com/100/100">
<img class="quatro" src="http://placecage.com/100/100">
<img class="quatro" src="http://placecage.com/100/100">
<img class="quatro" src="http://placecage.com/100/100">
<img class="quatro" src="http://placecage.com/100/100">
<img class="quatro" src="http://placecage.com/100/100">
<img class="quatro" src="http://placecage.com/100/100">
To learn more:
What do "n" means, numbers and signals on "Nth-Child" or "Nth-last-Child" selectors"?
Make an HTML checklist
Using Divs
Depending on the desired effect, just set the width of your DIV
s for 25%. Just watch out for the box-model
:
.quatro {
width:25%;
float:left;
}
<div class="quatro"><img src="http://placecage.com/100/120"></div>
<div class="quatro"><img src="http://placecage.com/100/120"></div>
<div class="quatro"><img src="http://placecage.com/100/120"></div>
<div class="quatro"><img src="http://placecage.com/100/120"></div>
<div class="quatro"><img src="http://placecage.com/100/120"></div>
<div class="quatro"><img src="http://placecage.com/100/120"></div>
<div class="quatro"><img src="http://placecage.com/100/120"></div>
<div class="quatro"><img src="http://placecage.com/100/120"></div>
<div class="quatro"><img src="http://placecage.com/100/120"></div>
<div class="quatro"><img src="http://placecage.com/100/120"></div>
<div class="quatro"><img src="http://placecage.com/100/120"></div>
<div class="quatro"><img src="http://placecage.com/100/120"></div>
<div class="quatro"><img src="http://placecage.com/100/120"></div>
<div class="quatro"><img src="http://placecage.com/100/120"></div>
You really managed to put a limit of 4 images per line. But I tried to apply this code to mine and I got it. I updated my question with an example of the code I have. This read of my code repeats several times within ul
– jim
The element can be any one, I used IMG and DIV as a mere example. Just put the class "four" (or the name you want) in the desired element. Obviously you should start from a clean CSS, so that your current rules do not interfere with the effect (which has been proven in the above executable examples). If you try to mix with the rest of the previous CSS, it is difficult to detect the problem.
– Bacco
see working with your code: https://codepen.io/anon/pen/yxeEpB
– Bacco