You can select half the elements in pure CSS ... to the point.
The only drawback is that you must know the maximum number of common items. It may be 150, but then it will not work with 151.
Here's the demo: http://jsfiddle.net/tcK3F/ (*)
Minimal CSS:
li:first-child:last-child, li:nth-child(n+2):nth-last-child(-n+2), li:nth-child(n+3):nth-last-child(-n+3), li:nth-child(n+4):nth-last-child(-n+4), li:nth-child(n+5):nth-last-child(-n+5), li:nth-child(n+6):nth-last-child(-n+6) { color: white; background: darkblue; } li:nth-child(n+2):last-child, li:nth-child(n+3):nth-last-child(-n+2), li:nth-child(n+4):nth-last-child(-n+3), li:nth-child(n+5):nth-last-child(-n+4), li:nth-child(n+6):nth-last-child(-n+5), li:nth-child(n+7):nth-last-child(-n+6){ font-style: italic; border: 2px solid red; }
Based on an idea from:
A trick from Andre Luis and a note in a post from Lee Vero: Style elements based on counting the number of sisters . I adapted it to the need for separation.
Short description:
:nth-last-child(-n+3) will select the last 3 elements from the parent; :nth-child(n+3) will select all the elements except the first 3. Combine them, and you can select elements in pure CSS based on what follows them (or how many children are in the parent element). In addition, you will have to combine 75 of them with 74 commas if you want this trick to work with 150 elements ... :)
IE9 + compatibility (JS multi-threading exists)
(*)
The first part of the HTML code: an even number of list items; second part: odd number of list items
CSS first rule: selects the last N of 2N elements or the last N + 1/2 element of 2N + 1 and their styles in white on blue (for example: 3 elements for a total of 5 or 6).
CSS second rule: select the last N of 2N elements or the last N-1/2 elements of 2N + 1 and their styles with a red border and italics (for example: 2 elements for a total of 4 or 5)
Felipeals
source share