I have a component that shows a few different values such as Low / Date
, Medium / Date
and High / Date
.
I’m trying to find a way to find the parent div named .parent
and change the color of the text inside according to its value.
For example, if div .parent
will include Low / Date
, "Low" will be green, and if the text will be High / Date
, the "High" will be colored red (and the date part will stay default black).
Is that possible with CSS only?
2
Answers
The
::before
pseudo-element is used to insert content before the element it is applied to. In this case, we are inserting the value of thedata-value
attribute before the text. We then use the color property to change thecolor
of the text depending on its value.To be honest, I am not sure if it is possible with CSS. I tried making one in CSS but it does not work how I think you would want it.
CSS:
Here is the code for attempting it in CSS. The value shown in the center of the page is set by the ‘data-number’ variable inside the body. While you can use this value, it is not easy to manipulate unless you manually do it.
JavaScript:
I personally would do it in a JavaScript. I know that is not what you are looking for but it would probably be your best bet for having the color change.
I am not a professional, but this is what I would recommend. I looked at some other forms to see if anything similar was asked and answered but to no luck. I would love to learn if it is possible in CSS but from what I can tell, it is not.