简体   繁体   English

使用CSS / HTML / Python / SASS(但不使用JS)根据值更改表格单元格背景的颜色? 无需动态

[英]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.

相关问题 通过python根据其值将背景颜色应用于表格中的html单元格 - Apply background color to html cell in table based on its value via python Python Kivy RecycleView。 如何根据单元格的数据值改变Recycleview表格的背景颜色 - Python Kivy RecycleView. How to change the background color of the Recycleview table based on the data value of the cells 使用python-docx的Word表格单元格背景色 - Word table cell background color using python-docx 使用Jinja模板基于单元格值对表格中的单元格进行颜色编码 - Color coding cells in a table based on the cell value using Jinja templates 使用样式和 css 更改 Pandas 数据框 html table python 中文本的颜色 - Change the color of text within a pandas dataframe html table python using styles and css 更改颜色是基于数据库中列的表格单元格 - Change color is table cell based of columns in database 如何使用Selenium和python动态读取表中的特定单元格值 - how to dynamically read a specific cell value in a table using selenium and python ttk.Treeview 根据单元格值设置单元格背景颜色 - ttk.Treeview set cell background color based on cell value 我如何根据使用 to_html 从 pd.DataFrame 生成的 HTML 表的值更改颜色 - How do i change color based on value of an HTML table generated from a pd.DataFrame using to_html 更改支配表 (Python) 中单元格的样式或背景 - Change the style or background of a cell in Dominate table (Python)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM