Styling children based on their number, with CSS3

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.

  • http://www.sareez.com/ Indian Sarees

    Wow,,, childish CSS huh ..!! nice..

  • http://www.smashingmagazine.com/2011/02/16/css-useful-coding-techniques-and-design-solutions/ CSS: Innovative Techniques and Practical Solutions – Smashing Magazine

    [...] [...]

  • http://www.smashingmagazine.com/2011/02/16/css-useful-coding-techniques-and-design-solutions/ CSS: Innovative Techniques and Practical Solutions – Smashing Magazine

    [...] [...]

  • http://www.ewok.fr/blog/?p=285 CSS: Innovative Techniques and Practical Solutions « e-wok.fr, le blog

    [...] Styling children based on their number, with CSS3Lea Verou presents an interesting technique for styling children based on their number. It is based on the relationship between :nth-child and :nth-last-child. With the technique, 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. [...]

  • http://www.ruturaj.in/css-innovative-techniques-and-practical-solutions/ CSS: Innovative Techniques and Practical Solutions | Ruturaj Pradeep Kohok | Your Web Advisor

    [...] Styling children based on their number, with CSS3Lea Verou presents an interesting technique for styling children based on their number. It is based on the relationship between :nth-child and :nth-last-child. With the technique, 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. [...]

  • http://www.broadyswebdesign.com/webdevelopment/css-innovative-techniques-and-practical-solutions/ CSS: Innovative Techniques and Practical Solutions – Broadys Web Design

    [...] [...]

  • http://wpremake.com/css-innovative-techniques-and-practical-solutions/ CSS: Innovative Techniques and Practical Solutions | Remake Wordpress Theme

    [...] Styling children based on their number, with CSS3Lea Verou presents an interesting technique for styling children based on their number. It is based on the relationship between :nth-child and :nth-last-child. With the technique, 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. [...]

  • http://www.rcfreak.com/?p=88 CSS: Innovative Techniques and Practical Solutions | RC Freak

    [...] Styling children based on their number, with CSS3Lea Verou presents an interesting technique for styling children based on their number. It is based on the relationship between :nth-child and :nth-last-child. With the technique, 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. [...]

  • http://www.jackchen.cn/blog/?p=3920 关于CSS创新技术和实际解决办法 | Jackchen Design 1984

    [...] 有关他们的人数根据造型的儿童,用CSS3 利Verou提出了一个有趣的数字技术为基础的造型上的儿童。 它是基于之间的关系 :第n -儿童 和:第n -最后的孩子 。 有了这项技术,规则的数量总仍然是O(n),但规则的编号在每个选择器就像变成2,制作这一招较大的儿童的实际人数为远。 [...]

  • http://snippetmagazine.com/css-innovative-techniques-and-practical-solutions CSS: Innovative Techniques and Practical Solutions | Snippet Magazine

    [...] Styling children based on their number, with CSS3Lea Verou presents an interesting technique for styling children based on their number. It is based on the relationship between :nth-child and :nth-last-child. With the technique, 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. [...]

  • http://www.iknowidea.com/designing-idea/css-innovative-techniques-and-practical-solutions/ CSS: Innovative Techniques and Practical Solutions | i know idea

    [...] Styling children based on their number, with CSS3Lea Verou presents an interesting technique for styling children based on their number. It is based on the relationship between :nth-child and :nth-last-child. With the technique, 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. [...]

  • http://bevertimer5.comuf.com/2011/03/03/css-innovations-techniques-and-more-8/ CSS innovations, techniques, and more 8 | Hanzoro5's blog

    [...] Styling children based on their number, with CSS3 Lea Verou presents an interesting technique for styling children based on their number. It is based on the relationship between :nth-child and :nth-last-child. With the technique, 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. [...]

  • http://blog.desmaksolutions.com/2011/03/css-innovative-techniques-and-practical-solutions/ CSS: Innovative Techniques and Practical Solutions | desmaksolutions.com

    [...] Styling children based on their number, with CSS3 Lea Verou presents an interesting technique for styling children based on their number. It is based on the relationship between :nth-child and :nth-last-child. With the technique, 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. [...]

  • http://thchong.wedo-wd.com/v2/2011/03/css-useful-coding-techniques-and-design-solutions/ I'm TH Chong » Css-useful-coding-techniques-and-design-solutions

    [...] Styling children based on their number, with CSS3 Lea Verou presents an interesting technique for styling children based on their number. It is based on the relationship between :nth-child and :nth-last-child. With the technique, 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. [...]

  • http://redbey.com/?p=290 Powerful New CSS Techniques and Tools | redbey

    [...] Styling children based on their number, with CSS3Lea Verou shows how to style children of elements based on their order. [...]

  • http://www.ewok.fr/blog/?p=432 Powerful New CSS Techniques and Tools « e-wok.fr, le blog

    [...] Styling children based on their number, with CSS3Lea Verou shows how to style children of elements based on their order. [...]

  • http://www.buildin9.com/?p=285 Powerful New CSS Techniques and Tools

    [...] Styling children based on their number, with CSS3 Lea Verou shows how to style children of elements based on their order. [...]

  • http://wpremake.com/powerful-new-css-techniques-and-tools/ Powerful New CSS Techniques and Tools | Remake Wordpress Theme

    [...] Styling children based on their number, with CSS3Lea Verou shows how to style children of elements based on their order. [...]

  • http://barraponto.blog.br Capi Etheriel

    Let’s write it in SASS (actually, SCSS):

    @for $i from 1 through 4 {
    li:first-child:nth-last-child(#{$i}),
    li:first-child:nth-last-child(#{$i}) ~ li {
    width: 100% / #{$i} } }

  • http://leaverou.me Lea Verou

    Good, but I don’t think this is a good idea for making elements the same width, there are better techniques for that, like CSS table layout. It’s very useful for a number of other things though, for example see how I used this on http://bytesizematters.com to make the textareas shorter as their total number increases.

  • http://qeqnes.wordpress.com/2011/04/27/powerful-new-css-techniques-and-tools/ Powerful New CSS Techniques and Tools « qeqnes | Designing. jQuery, Ajax, PHP, MySQL and Templates

    [...] Styling children based on their number, with CSS3 Lea Verou shows how to style children of elements based on their total number (that is, their total count). [...]

  • http://www.cricketkafunda.com/2011/04/28/powerful-new-css-techniques-and-tools/ Powerful New CSS Techniques and Tools | CRICKET ka FUNDA

    [...] Styling children based on their number, with CSS3Lea Verou shows how to style children of elements based on their total number (that is, their total count). [...]

  • Anonymous

    Hello,

    That’s a good id.
    There’s a little mistake in the first code :  the second width: 33.3333%; has to be width: 25%

  • http://leaverou.me Lea Verou

    Oops, thanks. Fixed now!

  • Anonymous

    Oh, actually there’s an other very simple tip to select one item :

    /* one item */
    li:only-child {
    width: 100%;
    }
    ;)

  • http://sejix.com/blog/design/powerful-new-css-techniques-and-tools/ Powerful New CSS Techniques and Tools | Sejix Technologies Blog

    [...] Styling children based on their number, with CSS3 Lea Verou shows how to style children of elements based on their total number (that is, their total count). [...]

  • Anonymous

    Nice idea, its sorta concise than previous, but some1 could tell me what does the operator (~) mean? I searched a lot in google but fail to get results :)

  • http://leaverou.me Lea Verou

    When in doubt, always check the specification: http://www.w3.org/TR/css3-selectors/#general-sibling-combinators

    In a nutshell, it’s a generalization of +.

  • http://www.inspiretechs.com/blog/?p=312 Powerful New CSS Techniques and Tools | Inspire Technologies

    [...] Styling children based on their number, with CSS3 Lea Verou shows how to style children of elements based on their total number (that is, their total count). [...]

  • http://www.wdesignlove.com/coding/new-css-techniques-and-tools/ New CSS Techniques and Tools | W Design Love | Graphic and Web Design Blog | Coding | tutorials | freebies | Wordpress | inspiration | Blog

    [...] Styling children based on their number, with CSS3 Lea Verou shows how to style children of elements based on their total number (that is, their total count). [...]