I have a basic table with two columns: name and value. I’d like to shade each row in the table an appropriate percentage of the width based on the size of the value (to essentially create a sideways histogram!). I can write code to calculate the appropriate percentage to set, but I can’t figure out the CSS to actually shade each row appropriately. It seems like the whole row can be shaded, but not a percentage. Is that true?
For what it’s worth, I’m using Twitter Bootstrap and I can use jQuery too if need be. This is only running on Chrome so CSS3 & Webkit only is fine! Here’s the 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>
Any tips on how to make this happen? Hope this question makes sense.
You could use linear-gradients.
If the percentage is 40%:
Demo
So, in JavaScript,
Demo
If you want to apply this to each row differently:
The problem is that setting the background to the
trworks well on Firefox and Opera, but on Chrome the gradient is applied to each cell.This problem can be fixed adding this code (see https://stackoverflow.com/a/10515894):
Demo