[英]Change the color of the background of a table cell based on the value using CSS/HTML/Python/SASS (but no JS)? No need to be dynamically
I have a local HTML file with a table. 我有一个带有表的本地HTML文件。 A python script is updating the HTML using some input,so when I open it via browser,I see it,and when I manually(not automatic) press refresh, the new value appears,and my goal is to based on that update the color of the cell.
python脚本正在使用一些输入来更新HTML,因此当我通过浏览器打开HTML时,我会看到它,而当我手动(而非自动)按刷新时,会出现新值,我的目标是基于该更新颜色的细胞。 Something like if the value is,say
例如,如果值是
<td id="cell_1">Blue</td>
The color will be blue: 颜色将为蓝色:
#cell_1 {
background-color: blue;
}
But if the value is 但是如果值是
<td id="cell_1">Green</td>
The color will be 颜色将是
#cell_1 {
background-color: green;
}
I know that this is possible using JS,but I'd prefer something using Python or SASS. 我知道使用JS可以实现,但是我更喜欢使用Python或SASS。 I'm using Beautiful Soup to update the HTML,maybe there is a similar library to CSS?
我正在使用Beautiful Soup更新HTML,也许有一个类似于CSS的库? Thanks in advance
提前致谢
If you can change html, you can also change the class of the cell depending on value like so: 如果可以更改html,则还可以根据值更改单元格的类,如下所示:
<td id="cell_1" class="blue">Blue</td>
or 要么
<td id="cell_1" class="green">Green</td>
and then target with CSS 然后使用CSS定位
.blue {background-color: blue; }
Otherwise I think it's not possible with pure CSS. 否则,我认为使用纯CSS是不可能的。 Also please notice "blue" and "green" are not semantic class names but without knowing what you're trying to achieve it hard to tell how to name them.
另外请注意,“蓝色”和“绿色”不是语义类名称,但在不知道您要实现的目标时很难说出如何命名它们。 "success" for example, will be better.
以“成功”为例,会更好。
In my opinion, you need to know which colors are available for the background. 我认为,您需要知道哪些颜色可用于背景。 Let's say you have green and blue;
假设您有绿色和蓝色; white class for both of them:
他们俩都是白人
.green_background {
background-color: green;
}
.blue_background {
background-color: blue;
}
And then pass the value of class to your HTML element, instead of trying to change it in CSS dynamically. 然后将class的值传递给HTML元素,而不是尝试在CSS中动态更改它。
<td class="green_background">Green</td>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.