Styling children based on their number with CSS3
http://leaverou.me/2011/01/styling-children-based-on-their-number-with-css3
by kougiland
HTML
<ul>
<li>1</li>
</ul>
<ul>
<li>1</li>
<li>2</li>
</ul>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
CSS
ul {
padding:10px 1px;
overflow:hidden;
}
li {
float:left;
outline:1px solid gray;
text-align:center;
line-height:2;
}
/* 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%;
}