admin管理员组文章数量:1401925
I have table data which shows football team statistics.
There is column which have letters like:
- "W" (win)
- "L" (lose)
- "D" (draw)
That's letters are rendered as a separate column, thing is that value is dynamically, and I need to apply different colors:
- In case with "W" that text should be green
- In case with "L" color red
- In case with "D" color grey
Is there are way to do that trough CSS?
.table td:nth-child(3) {
if letter is === W apply green color?
}
<td>
"W"
</td>
<td>
"L"
</td>
<td>
"D"
</td>
I have table data which shows football team statistics.
There is column which have letters like:
- "W" (win)
- "L" (lose)
- "D" (draw)
That's letters are rendered as a separate column, thing is that value is dynamically, and I need to apply different colors:
- In case with "W" that text should be green
- In case with "L" color red
- In case with "D" color grey
Is there are way to do that trough CSS?
.table td:nth-child(3) {
if letter is === W apply green color?
}
<td>
"W"
</td>
<td>
"L"
</td>
<td>
"D"
</td>
Share
Improve this question
asked May 21, 2020 at 15:42
Michel JamesMichel James
752 silver badges7 bronze badges
3
- Add a class to their container when outputting them to the page dynamically. I'm not sure how they're pulled and inserted so it's difficult to say exactly. – DannyXCII Commented May 21, 2020 at 15:50
- Can I add a class to the <td> and then trough class get access to content of filed and then change the color based on value of content? – Michel James Commented May 21, 2020 at 15:52
-
You can just style the class
.letter-w { color: green }
etc. are you using JavaScript or PHP to dynamically insert the data? Guessing JS and you're hitting an external API for the scores? – DannyXCII Commented May 21, 2020 at 15:54
3 Answers
Reset to default 2if you can pass those letters as an attribute to the td element you can do it in CSS like this:
<td color_code='Your letter'>Your letter</td>
here 'your letter' is the letter that's generated dynamically. Now, simply in CSS, just specify that attribute when calling the td element:
td[color_code='L']{ color: red}
and the same for the rest of the letters.
If you can add an attribute, you can target the attribute in CSS for different style rules. Example
<td data-result-val="W">W</td>
<td data-result-val="L">L</td>
<td data-result-val="D">D</td>
in css
td[data-result-val="L"] { color: red;}
td[data-result-val="W"] { color: green;}
td[data-result-val="D"] { color: grey;}
It's not possible any more. The contains pseudo class can do this, but this is deprecated and will not work any more.
What you can do:
Best solution:
When rendering the page, you can render a class at the td: < td class="color-W">W> < /td>.
Not so good:
Or you can set the classes after rendering with javascript or jQuery.
JQuery:
$('td:contains("W")').addClass('color-W');
P.S.: the jQuery :contains pseudo class selector is parsed by jQuery and works fine.
本文标签: javascriptHow to change color of text based on textvalue by using cssStack Overflow
版权声明:本文标题:javascript - How to change color of text based on text-value by using css? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744284637a2598818.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论