Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Styling the first 10 items in a list

Tags:

css

Can something like li:nth-child be used to style the first ten items in a list?

<ol>
  <li>item 1</li>
  <li>item 2</li>
  <li>item 3</li>
  <li>item 4</li>
  <li>item 5</li>
  <li>item 6</li>
  <li>item 7</li>
  <li>item 8</li>
  <li>item 9</li>
  <li>item 10</li>
  <li>item 11</li>
  <li>item 12</li>
</ol>

so 1 to 10 will be fancy and 11 and 12 will be normal. I'd rather not use a class if possible.

like image 366
Trevor Nowak Avatar asked Dec 13 '22 05:12

Trevor Nowak


2 Answers

nth childs example:

:nth-child(-n+10)

this in works here: link.

more on understanding this check out this site.


I guess if you want IE support, i can't really make this any prettier. Atleast I don't know how with this cheap hack.

ul>li + li + li + li + li + li + li + li + li + li + li{
    text-align: center; /*makes everything after 10 centered*/

}​

http://jsfiddle.net/TzLqZ/ for an example of this above


Here is the IE way with first 10 being center and the last 2 being normal: http://jsfiddle.net/TzLqZ/3/

ol>li{
    text-align: center;
    color: blue;
}
ol>li+li+li+li+li+li+li+li+li+li+li
{
    text-align: left;
    color: red;
}

like image 180
John Riselvato Avatar answered Dec 28 '22 14:12

John Riselvato


I think you are looking for the nth child pseudo-selector seen here http://css-tricks.com/how-nth-child-works/ I think for what what you want to do:

select all but top ten:

ul li:nth-child(n+11){}

or just top ten:

ul li:nth-child(-n+10){}

should do the trick.

However, Internet Explorer does not support this at least until ie8 according to the article. So don't rely on it for anything critical (although I don't know what child specific styling would be critical).

like image 22
Hawken Avatar answered Dec 28 '22 16:12

Hawken