I have a list of photos as such:
<ul class="gallery">
<li class="red"><img src="red.png" alt=""></li>
<li class="blue"><img src="blue.png" alt=""></li>
<li class="blue"><img src="blue.png" alt=""></li>
<li class="red"><img src="red.png" alt=""></li>
<li class="green"><img src="green.png" alt=""></li>
<li class="green"><img src="green.png" alt=""></li>
<li class="green"><img src="green.png" alt=""></li>
<li class="blue"><img src="blue.png" alt=""></li>
<li class="blue"><img src="blue.png" alt=""></li>
<li class="green"><img src="green.png" alt=""></li>
<li class="green"><img src="green.png" alt=""></li>
<li class="green"><img src="green.png" alt=""></li>
</ul>
Corresponding CSS as follows:
.gallery {
list-style-type: none;
li {
float: left;
}
.red {
width: 100%;
margin-bottom: 2%;
}
.blue {
width: 49%;
margin-right: 2%;
}
.green {
width: 32%;
margin-right: 2%;
}
.blue:nth-child(2n) {
margin-right: 0;
}
.green:nth-of-type(3n) {
margin-right: 0;
}
}
Here is a picture to illustrate this:
Every second blue element do not need a margin-right to fit the grid properly. Similarly, every third green element doesn't need a margin-right.
I tried to target them using the n-th child selector above which doesn't seem to do the trick. The second element in the list is actually the first .red element which is being set to margin-right: 0;
It seems like the class name is being overriden by the selector. Maybe the class names aren't considered and I know I can group the red items in a div and target them but that's not a solution as the list is being generated dynamically and I would like to keep its structure.
Basically, the user will be able to upload as much photos as he like in the grid with a structure as follows:
- If he decides to upload a blue picture, the next picture has to be blue as well to fit the grid.
- If he uploads a green picture, the next two pictures have to be green as well to fit the grid.
Any ideas on how to solve this?