CSS仅为表格行宽度的百分比设置背景颜色

ton*_*nic 11 css jquery html-table twitter-bootstrap

我有一个包含两列的基本表:名称和值.我想根据值的大小为表中的每一行加上适当的宽度百分比(基本上创建一个横向直方图!).我可以编写代码来计算要设置的适当百分比,但我无法弄清楚CSS是否能够适当地对每一行进行着色.似乎整行可以加阴影,但不是百分比.真的吗?

对于它的价值,我正在使用Twitter Bootstrap,如果需要我也可以使用jQuery.这只能在Chrome上运行,所以CSS3和Webkit才行!这是HTML:

<table class="table">
  <tbody class="lead">              
    <tr> 
      <td>
        Joe
      </td>
      <td>
        10
      </td>
    </tr>              
    <tr> 
      <td>
        Jane
      </td>
      <td>
        20 
      </td>
    </tr>              
    <tr> 
      <td>
        Jim
      </td>
      <td>
        2
      </td>
    </tr>
  </tbody>
</table>
Run Code Online (Sandbox Code Playgroud)

有关如何实现这一目标的任何提示?希望这个问题有道理.

Ori*_*iol 25

你可以使用线性渐变.

如果百分比是40%:

table{
    background: -webkit-gradient(linear, left top, right top, color-stop(40%,#F00), color-stop(40%,#00F));
    background: -moz-linear-gradient(left center, #F00 40%, #00F 40%);
    background: -o-linear-gradient(left, #F00 40%, #00F 40%);
    background: linear-gradient(to right, #F00 40%, #00F 40%);
}
Run Code Online (Sandbox Code Playgroud)

演示

所以,在JavaScript中,

var percentage=40,
    col1="#F00",
    col2="#00F";
var t=document.getElementById('table');
t.style.background = "-webkit-gradient(linear, left top,right top, color-stop("+percentage+"%,"+col1+"), color-stop("+percentage+"%,"+col2+"))";
t.style.background = "-moz-linear-gradient(left center,"+col1+" "+percentage+"%, "+col2+" "+percentage+"%)";
t.style.background = "-o-linear-gradient(left,"+col1+" "+percentage+"%, "+col2+" "+percentage+"%)";
t.style.background = "linear-gradient(to right,"+col1+" "+percentage+"%, "+col2+" "+percentage+"%)";
Run Code Online (Sandbox Code Playgroud)

演示


如果要以不同方式将其应用于每一行:

var col1="#F00",
    col2="#00F";
var els=document.getElementById('table').getElementsByTagName('tr');
for (var i=0; i<els.length; i++) {
    var percentage = Number(els[i].getElementsByTagName('td')[1].firstChild.nodeValue);
    els[i].style.background = "-webkit-gradient(linear, left top,right top, color-stop("+percentage+"%,"+col1+"), color-stop("+percentage+"%,"+col2+"))";
    els[i].style.background = "-moz-linear-gradient(left center,"+col1+" "+percentage+"%, "+col2+" "+percentage+"%)" ;
    els[i].style.background = "-o-linear-gradient(left,"+col1+" "+percentage+"%, "+col2+" "+percentage+"%)";
    els[i].style.background = "linear-gradient(to right,"+col1+" "+percentage+"%, "+col2+" "+percentage+"%)" ;
}
Run Code Online (Sandbox Code Playgroud)

问题是tr在Firefox和Opera上将背景设置得很好,但在Chrome上,渐变应用于每个单元格.

添加此代码可以解决此问题(请参阅/sf/answers/736112611/):

#table td {display: inline-block;}
Run Code Online (Sandbox Code Playgroud)

演示


Chr*_*ris 12

无需使用渐变.首先,使用要为行着色的颜色创建一个简单的图像文件.在我的例子中,我创建了一个.png完全着色的黑色(black.png在下面的例子中).现在,只需使用background-imagebackground-size属性为行的相应部分着色.

例如,HTML:

<table cellspacing = "0px">
    <tr class = "row"><td>Hello Hello</td><td>Bye Bye</td></tr>
</table>
Run Code Online (Sandbox Code Playgroud)

CSS:

.row {
    background-color: white; /*fallback color*/
    background-image: url(black.png);
    background-size: 75% 100%; /*your percentage is the first one (width), second one (100%) is for height*/
    background-repeat: no-repeat;
    color: rgb(0, 140, 200);
    font-weight: bold;
}
Run Code Online (Sandbox Code Playgroud)

结果:

结果

这是一个简单的片段,介绍如何为您的示例自动执行此操作:

var tbl = document.getElementsByClassName("table")[0];
var rws = tbl.rows;
for(var i = 0; i < rws.length; i ++) {
    var percentage = parseInt(rws[i].getElementsByTagName("td")[1].innerHTML, 10);
    rws[i].style.backgroundImage = "black.png";
    rws[i].style.backgroundSize = percentage + "%" + " 100%";
    rws[i].style.backgroundRepeat = "no-repeat";
    rws[i].style.color = "rgb(0, 140, 200)";
}
Run Code Online (Sandbox Code Playgroud)

小演示:百分比宽度bg颜色(基于非渐变).

我希望有所帮助!