Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Overriding color styles for icon from antd does not work

I have global styles:
global.css

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: Inter, sans-serif;
    color: #fff;
}

Next, I use the icon from antd and style it.

import cl from './Dish.module.css'
import { LeftCircleOutlined } from '@ant-design/icons'

<div className={cl.imageBox}>
   <LeftCircleOutlined className={cl.rotateLeftIcon}  />
</div>

and the corresponding styles for the icon:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

.imageBox {
    position: relative;
}
.rotateLeftIcon {
    color: rgb(239, 3, 3) !important;
    position: absolute;
    font-size: 44px;

    left: 0;
    top: 46%;
}

The !important; note here should override the style, indicating that this is the highest style in the style hierarchy, so it should be set, but no, for some reason the styles from global.css are applied.

I’ve tried specifying a more specific selector like

.rotateLeftIcon  svg{
    color: rgb(239, 3, 3) !important;
    position: absolute;
    font-size: 44px;

    left: 0;
    top: 46%;
}

in this case in developer mode, indeed, the style with red color went higher in the hierarchy, and the style with white color even started to show crossed out, but it still shows it in white color, what could be the problem?

>Solution :

Use fill to colour SVGs. The @ant-design/icons SVGs have fill:currentColor; to make them inherit from the text colour of their parent. So any one of the following will set the SVG’s colour:

Option 1. Set the color of the container.

<div className={cl.imageBox} style={{color:"rgb(239, 3, 3)"}}>
   <LeftCircleOutlined className={cl.rotateLeftIcon} />
</div>

Option 2. Set the color of the LeftCircleOutlined element inline…

<div className={cl.imageBox}>
   <LeftCircleOutlined className={cl.rotateLeftIcon} style={{color:"rgb(239, 3, 3)" }} />>
</div>

… or in your stylesheet:

.rotateLeftIcon {
    color: rgb(239, 3, 3);
}

Option 3. Override the SVG’s fill in your stylesheet:

.rotateLeftIcon svg {
    fill: rgb(239, 3, 3);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading