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
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 )
Updated for LWC components. (18/03/2021)
The above mentioned approach 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 : https://developer.salesforce.com/docs/component-library/bundle/lightning-icon/documentation
<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.
.comments-icn{
--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
}
Update (18/03/2024) - Styling hooks update
Thanks for bringing this up @Kelly Shoemake
Styling hooks have been updated from --sds
to --slds
. For more info on styling hooks https://www.lightningdesignsystem.com/components/icons/#Styling-Hooks-Overview
.comments-icn{
--slds-c-icon-color-foreground-default: orange; //utility icons
--slds-c-icon-color-foreground: orange; //action icons or custom icons
--slds-c-icon-color-background: white; //action icons or custom icons
}