0

Hello everybody I want to style the background color of a lightning button with a color so I went to check the Salesforce documentation and found out this: enter image description here

So I did something like that:

.greenBackgroundButton{
      background-color: #3bcf56;
}

<lightning-button class="greenBackgroundButton" label="Crear paso" onclick={handleValidacion}  ></lightning-button> 
   

But the background color is not changing. Could anybody help me? Thanks

2 Answers 2

3
.greenBackgroundButton{
    background-color: #3bcf56;
}

Should be:

.greenBackgroundButton{
  --sds-c-button-brand-color-background: #3bcf56;
  --sds-c-button-brand-color-border: #3bcf56;
  --sds-c-button-brand-color-background-hover: #3bcf56;
  --sds-c-button-brand-color-border-hover: #3bcf56;
}

Note that this is called a "styling hook." These are styles that affect components using CSS variables. This is the only way to style some properties of a child component in a parent component, explained by the Shadow DOM and Styling Hooks documentation.

Since this hook uses the brand color, make sure you specify variant="brand".

Demo.

4
  • Hello I also tried this way with the --sds-c-button-brand-color-background but did not worked. The background color did not changed
    – user120821
    Commented Dec 22, 2022 at 10:57
  • 1
    @JaimeGonzalez Make sure that the variant is brand. Click the last link to see my demo of this property.
    – sfdcfox
    Commented Dec 22, 2022 at 10:58
  • Yes but the problem is that I don't want to use the variant brand because when the button appears is comes with the my green colour but when the mouse is above the button it turn blue. That is what I don't. I want that the background color to be green even when the mouse is on it and also the button border because it is in blue colour.
    – user120821
    Commented Dec 22, 2022 at 13:13
  • @JaimeGonzalez There are additional hooks you can use. I've updated the demo link to include the full CSS you could use to achieve your specific goals.
    – sfdcfox
    Commented Dec 22, 2022 at 14:28
0

The actual button you are seeing is an element inside the LWC component lightning-button. Hence you have to target the button element in the lightning-button.

Just change your css to below code:

.greenBackgroundButton button.slds-button {
      background-color: #3bcf56;
}

You must log in to answer this question.