I am mapping a data and sends to the component where it passes different colors for the background of the icon. But I want to change the color when I hover on the component that is rendered.
``
<div className="service-card--wrapper">
<div className="service-card">
<div className="service__icon" style={{backgroundColor:`${service.color}`}}>
{service.icon}
</div>
<div className="service__title">{service.title}</div>
<p className="service__para">{service.paragraph}</p>
<button className="service__btn"><FaArrowRight/></button>
</div>
</div>
``
I want to change the color when it hovers on the service-card but the CSS doesn’t overwrite the HTML inline-style, is there a way I can do this?
3
Answers
With the assumption that you want styles from a CSS declaration to override the ones in the
style
attribute, you need the CSS to have a higher specificity. Unfortunately the only way to do that in this case (that I’m aware of) is to add!important
to the style in the CSS (or remove the style attribute and use CSS for that as well).Using
!important
is usually reserved for certain definite overrides or as a last resort, so if you can avoid it that would be good. For example, if there aren’t too many options forservice.color
you could have a class for each color, and then assign the corresponding classname based on the variable instead of the color directly.Another option is to change the color programmatically in the JS in response to the corresponding mouse events.
Sounds like you want to do something like this:
Note however that both inline style and the use of !important is not considered a good practice
One possible approach to achieve this is to use