The original idea belongs to André Luís, but I think it could be improved to be much less verbose.
André’s solution is like this:
/* one item */
li:nth-child(1):nth-last-child(1) {
width: 100%;
}
/* two items */
li:nth-child(1):nth-last-child(2),
li:nth-child(2):nth-last-child(1) {
width: 50%;
}
/* three items */
li:nth-child(1):nth-last-child(3),
li:nth-child(2):nth-last-child(2),
li:nth-child(3):nth-last-child(1) {
width: 33.3333%;
}
/* four items */
li:nth-child(1):nth-last-child(4),
li:nth-child(2):nth-last-child(3),
li:nth-child(3):nth-last-child(2),
li:nth-child(4):nth-last-child(1) {
width: 25%;
}
It’s based on the relationship between :nth-child and :nth-last-child. As you can see, the number of total rules is O(N) and the number of selectors in every rule is also O(N).
However, what you really want, is to just target the first element. The others can be targeted with just a sibling selector. With my improvement, the number of total rules is still O(N), but the number of selectors in every rule becomes just 2, making this trick practical for far larger numbers of children:
/* one item */
li:first-child:nth-last-child(1) {
width: 100%;
}
/* two items */
li:first-child:nth-last-child(2),
li:first-child:nth-last-child(2) ~ li {
width: 50%;
}
/* three items */
li:first-child:nth-last-child(3),
li:first-child:nth-last-child(3) ~ li {
width: 33.3333%;
}
/* four items */
li:first-child:nth-last-child(4),
li:first-child:nth-last-child(4) ~ li {
width: 25%;
}
And here’s a fiddle to prove it:
Yes, I know that with Flexbox and the other layout modules, techniques such as these are soon becoming obsolete, but I think they are still useful right now.
I’m also aware that you can emulate this particular example with table display modes, but a) Table display modes have other implications that are sometimes undesirable and b) Widths are just an example, you could come up with other ways to style the elements based on their total count, which can’t be emulated by CSS tables.
Pingback: CSS: Innovative Techniques and Practical Solutions - Smashing Magazine
Pingback: CSS: Innovative Techniques and Practical Solutions - Smashing Magazine
Pingback: CSS: Innovative Techniques and Practical Solutions « e-wok.fr, le blog
Pingback: CSS: Innovative Techniques and Practical Solutions | Ruturaj Pradeep Kohok | Your Web Advisor
Pingback: CSS: Innovative Techniques and Practical Solutions - Broadys Web Design
Pingback: CSS: Innovative Techniques and Practical Solutions | Remake Wordpress Theme
Pingback: CSS: Innovative Techniques and Practical Solutions | RC Freak
Pingback: 关于CSS创新技术和实际解决办法 | Jackchen Design 1984
Pingback: CSS: Innovative Techniques and Practical Solutions | Snippet Magazine
Pingback: CSS: Innovative Techniques and Practical Solutions | i know idea
Pingback: CSS innovations, techniques, and more 8 | Hanzoro5's blog
Pingback: CSS: Innovative Techniques and Practical Solutions | desmaksolutions.com
Pingback: I'm TH Chong » Css-useful-coding-techniques-and-design-solutions
Pingback: Powerful New CSS Techniques and Tools | redbey
Pingback: Powerful New CSS Techniques and Tools « e-wok.fr, le blog
Pingback: Powerful New CSS Techniques and Tools
Pingback: Powerful New CSS Techniques and Tools | Remake Wordpress Theme
Pingback: Powerful New CSS Techniques and Tools « qeqnes | Designing. jQuery, Ajax, PHP, MySQL and Templates
Pingback: Powerful New CSS Techniques and Tools | CRICKET ka FUNDA
Pingback: Powerful New CSS Techniques and Tools | Sejix Technologies Blog
Pingback: Powerful New CSS Techniques and Tools | Inspire Technologies
Pingback: New CSS Techniques and Tools | W Design Love | Graphic and Web Design Blog | Coding | tutorials | freebies | Wordpress | inspiration | Blog