Styling af CSS childs afhængigt af antallet af childs
Her er et eksempel, på hvordan man udelukkende via css kan style childs forskelligt, afhængigt af antallet af childs:
/* 1 child element */
li:first-child:nth-last-child(1) {
width: 100%;
}
/* 2 child elements */
li:first-child:nth-last-child(2),
li:first-child:nth-last-child(2) ~ li {
width: 50%;
}
/* 3 child elements */
li:first-child:nth-last-child(3),
li:first-child:nth-last-child(3) ~ li {
width: 33.3333%;
}
/* 4 child elements */
li:first-child:nth-last-child(4),
li:first-child:nth-last-child(4) ~ li {
width: 25%;
}
Denne snippet blev indsendt af inspo.dk