I have a 4 column Susy CSS gallery grid that can contain any number of blocks. If the last row has less that 4 blocks I need it to be centred.
Using this css selector technique http://lea.verou.me/2011/01/styling-children-based-on-their-number-with-css3/ I have been able to offset specific blocks as you can see in this pen: http://codepen.io/bennyb/pen/kXVaba
Using this code:
// Total of 1 or 5
ul li:first-child:nth-last-child(1),
ul li:first-child:nth-last-child(5) + li + li + li + li {
@include push(4.5);
}
// Total of 2 or 6
ul li:first-child:nth-last-child(2),
ul li:first-child:nth-last-child(6) + li + li + li + li {
@include push(3);
}
ul li:first-child:nth-last-child(2) + li,
ul li:first-child:nth-last-child(6) + li + li + li + li + li {
@include push(6);
}
// Total of 3 or 7
ul li:first-child:nth-last-child(3),
ul li:first-child:nth-last-child(7) + li + li + li + li {
@include push(1.5);
}
ul li:first-child:nth-last-child(3) + li,
ul li:first-child:nth-last-child(7) + li + li + li + li + li {
@include push(4.5);
}
ul li:first-child:nth-last-child(3) + li + li,
ul li:first-child:nth-last-child(7) + li + li + li + li + li + li {
@include push(7.5);
}
As you can see it only works if there is less than 8 items. Does anyone know how I could improve this so it will work with any number of items, maybe with a SASS mixin.
Update
Here is updated CSS based on vals' answer:
// One widow
ul li:first-child:nth-last-child(1),
ul li:first-child:nth-last-child(4n + 1) ~ li:nth-last-child(1) {
@include push(4.5);
}
// Two widows
ul li:first-child:nth-last-child(2),
ul li:first-child:nth-last-child(4n + 2) ~ li:nth-last-child(2) {
@include push(3);
}
ul li:first-child:nth-last-child(4n + 2) ~ li:nth-last-child(1) {
@include push(6);
}
// Three widows
ul li:first-child:nth-last-child(3),
ul li:first-child:nth-last-child(4n + 3) ~ li:nth-last-child(3) {
@include push(1.5);
}
ul li:first-child:nth-last-child(4n + 3) ~ li:nth-last-child(2) {
@include push(4.5);
}
ul li:first-child:nth-last-child(4n + 3) ~ li:nth-last-child(1) {
@include push(7.5);
}