Instead of focusing on setting classes, the question revolves around reacting to the content within certain elements.
According to the current CSS Spec, this is generally not achievable. However, there are some exceptions:
The :empty pseudo-selector, which determines if an element is completely empty or not.
td:empty {
background-color: lightgreen;
}
For a more detailed example, check out: http://codepen.io/MattDiMu/pen/pbJbOx
- One workaround is to place the content in a data-attribute and style based on it.
HTML
<td data-val="Name"></td>
CSS
td::after {
content: attr(data-val);
}
[data-val="John"] {
background-color: lightgreen;
}
For a detailed example of this method, visit: http://codepen.io/MattDiMu/pen/OXVXaO
While this approach can be considered a "hack," it should be used cautiously because:
- Most screen readers may not recognize the value as they disregard CSS content.
- You may lose the ability to use a different content in the ::after pseudo-element.
- This method is limited to displaying text and cannot showcase HTML structures.
It's worth noting that the ":contains" pseudo class was ultimately removed and did not become a part of the Recommended CSS Spec. As far as I know, it is not supported in any major browsers.