I know how to make a <td>
cell background color reflect the value in the enclosed <input type=number>
field using JavaScript (below is a snippet to do this). But is there a way to accomplish this without JavaScript? Using only CSS? I have full control over CSS, but not JavaScript. I can assume bleeding-edge browser versions for the latest CSS features.
function updateBgColor(td) {
const input = td.getElementsByTagName('input')[0]
td.style.backgroundColor = `rgb(96,${128+input.value*127},96)`
}
for (const input of document.getElementsByTagName('input')) {
input.addEventListener('input', event => updateBgColor(event.target.parentElement))
}
[...document.getElementsByTagName('td')].forEach(updateBgColor)
input[type=number] {
max-width: 6em;
background-color: transparent;
}
<table>
<tr>
<td><input type=number value="0.9097174053213968"></td>
<td><input type=number value="0.8408987079229189"></td>
<td><input type=number value="0.7024472413860736"></td>
<td><input type=number value="0.5185149759852405"></td>
</tr>
<tr>
<td><input type=number value="0.51746948952139"></td>
<td><input type=number value="0.23249116318958274"></td>
<td><input type=number value="0.24954372649914003"></td>
<td><input type=number value="0.7852880719259674"></td>
</tr>
<tr>
<td><input type=number value="0.006423826937380195"></td>
<td><input type=number value="0.18798660892180274"></td>
<td><input type=number value="0.33787422244115817"></td>
<td><input type=number value="0.2012761039221167"></td>
</tr>
<tr>
<td><input type=number value="0.6635851618352506"></td>
<td><input type=number value="0.4795899245078339"></td>
<td><input type=number value="0.6457512930088372"></td>
<td><input type=number value="0.6226533953736908"></td>
</tr>
</table>
2
Answers
The short answer is no.
CSS selectors can access many things about elements, their attributes, relationship with other elements in the DOM, and the position of text content within elements. What it doesn’t do is provide selectors based on some or all of the
.textContent
property of an element in the DOM.For example, CSS rules for the first line of an element using the pseudo element
::firstline
selector apply to text content identified by being rendered as the first line in layout, not by what the text rendered (on the first line) actually says.CSS Standard Refererence without third party interpretation: W3C Selectors Level 3
This is possible, but do be advised that it is (very) ugly, and onerous.
To achieve the required result we can take advantage of the
[attribute^="value"]
notation, but this will – depending on the required precision – increase the size, and complexity, of your CSS. A simple example is below:JS Fiddle demo.
With reference to the CSS required to style a cell based on a decimal value to one single digit, below:
We have ten rulesets, if you wish to extend that to two decimal places – to include both tenths and hundredths – that will require a hundred rules. To move beyond that, to three decimal places – tenths, hundredths, and thousandths – would require a thousand rules.
With every degree of precision we’re increasing the number of rules to the power n, where n is the number of decimal places required. This would lead to a very large CSS stylesheet. Ultimately, while this is possible, it’s important to remember the – paraphrased – words of the illustrious, though sadly fictional, Dr. Ian Malcolm:
References:
@supports
.selector()
function.var()
.Array.from()
.Array.prototype.forEach()
.Array.prototype.map()
.document.querySelector()
.document.querySelectorAll()
.Element.setAttribute()
.