As I recently learned, in HTML5 you can use the value attribute with list items, as long as they are inside of an ordered list. http://dev.w3.org/html5/markup/li.html
I have been trying to style the list items by their value. Preferably I would like to style them like:
li[value >= "10"] { background: orange; }
li[value >= "20"] { background: green; }
However, this is currently not possible with just CSS.
Here is a fiddle I've been playing around with, trying different things. http://jsfiddle.net/Hf57v/2/
HTML:
<ol>
<li value="33"></li>
<li value="4"></li>
<li value="12"></li>
<li value="88"></li>
<li value="jadfk"></li>
</ol>
CSS:
li { width: 20px; height: 20px; margin: 20px; background: gray; }
li[value~="3"] { background: orange; } /* #1 */
li[value="4"] { background: red; } /* #2 */
li[value="12"] { background: blue; } /* #3 */
li[value^="1"] { background: green; } /* #4 */
li[value^="8"] { background: yellow; } /* #5 */
li[value="NaN"] { background: pink; } /* #6 */
1) This does not work at making the background orange for the <li value="33"></li>
.
I thought it would since it contains a 3.
2) This works at changing the <li value="4"></li>
to red.
3) This works at changing the <li value="12"></li>
to blue.
4) This overrides the <li value="12"></li>
background of blue, and changes the background to green. I thought it might not since ="12
is more specific than ^="1"
(begins with 1).
5) This works at changing the <li value="88"></li>
to yellow.
6) This does not work at making the background pink for <li value="jadfk"></li>
. I tried it since the value must be a number, so if it's just a string/text, it returns NaN
(not a number).
Also, the way the ol
works, is if there is no value for a li
, or the value is NaN
, then that li
takes on a value from the previous li. In the fiddle, the <li value="jadfk"></li>
renders as 89.
But even though it renders as 89.
It does not obey the li[value^="8"] { background: yellow; }
like the 88.
does. I wonder why that is.
Main Question : Is there any way to target the li value's by >
or <
operators in CSS?
Follow Up Question : If the answer is no, then is there any hacks or selectors that I didn't include that do help target more list items by their value attribute's value at the same time?
Bounty Info :
The bounty jsfiddle link http://jsfiddle.net/tuDBL/
In the fiddle, I created an ordered list (ol
), which contains 130 list items (li
) whose values are from 0 to 129. The li
's with values from 0-9 need to have a unique background color. The li
's with values from 10-19 need to have a unique background color. Etc, etc, all the way through 129. So in the end, there will be 12 unique background colors, and each of them representing 10 consecutive li
values. The bounty winner will most likely be the person that is able to do this with the least amount of valid CSS.
~
represents an attr "whose value is a whitespace-separated list of words, one of which is an exact value" (ref) .. this would have worked for something likevalue="3 3">
Did you mean*
([value*="3"]
)? jsfiddle.net/Lr5nJ – Sewer