Lightning : Icon color is not changing using the fill CSS

  • According to the documentation ( ) we can change the icon color by suing the CSS. I am not able to do it. It always overrides with the Salesforce style.

    <lightning:icon class="icn" iconName="utility:favorite" size="small" />

    My Style class is as below:

     .THIS .icn{ fill: white; }

    Other css property like background color are applying but fill is always getting overridden by salesforce default color ( grey). Anyone have any idea on how to fix this?

  • Actually the issue is which element you are applying the "fill" property to. Meaning when you declare something like <lightning:icon class="icn" iconName="utility:favorite" size="small" />.

    It gets rendered as follows

    HTML Render

    If you notice the "icn" is applied to the outer span container rather than svg itself. Hence you will have to do Something as follows to apply your custom styles. ( applying the css to the svg element using heirarchies )

    Svg Icon Colored

    Updated for LWC components. (18/03/2021)

    The above mentioned approach does would not work for lightning web components such as lightning-icon as the web components use a shadow root property "closed" - which prevents us from modifying styles or accessing attributes from different namespaces.

    Luckily Salesforce has introduced Styling Hooks for us to customize the icon styles.

    Reference :

    <lightning-icon icon-name="utility:comments" alternative-text="comments" size="large" title="comments" class="comments-icn"></lightning-icon>
    • Add a class attribute to the lightning-icon component.
    • And add the following properties to your css class to customize icons.
       --sds-c-icon-color-foreground-default: orange; //utility icons
       --sds-c-icon-color-foreground: orange; //action icons or custom icons
       --sds-c-icon-color-background: white; //action icons or custom icons

    For more info on Styling Hooks -

    Styling Hooks

    That Helped. It works now!

    Somehow this didn't work for me. Instead, I had to style the `svg use` element.

    Could be a release related thing (some update to the DOM structure) or if there is a shadow root intervention for the out of box components - then this approach might cause discrepancies as this relies on the DOM heirarchy & CSS Specificity. Nevertheless please feel free to update your approach with an example for the future audience.

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM