Introduction

This LWC uses a set of custom CSS variables and Styling Hooks used to customize the appearance, providing a way to centralize and manage styles in the component.

This enables creating consistent and reusable styles throughout the application and easily change the look and feel of components by adjusting the values of these custom CSS variables and hooks.

Additionally, The .toggle-red selector demonstrates how specific styles can be modified or extended based on different requirements or themes.

There are 2 main techniques used in the LWC:

  1. Styling Hooks:

  2. Toggle Red Style:

<aside> 📂 You can find the LWC in this folder: force-app\\main\\default\\lwc\\stylingHooks in the Styling tab.

</aside>

<aside> 💡 If you are not familiar with the LWC Styling, review this section Styling LWC.

</aside>

CSS file

The :host selector is used to target the host element of a component, and it is the outermost element in the component's HTML template, representing the component itself.

--primary-color: #e3df00;
--primary-color-alt-shade: #a7a400;
--secondary-color: #020024;
--secondary-color-alt-shade: #343304;

The var function in CSS is used to define a custom variable, that can be used to store values and reused throughout the CSS. The var function takes two arguments: the name of the variable and the value of the variable.

The custom variables used in this component, so for example, the primary-color is defined in the Lightning Design System (LDS).

Let’s examine a usage of these variables:

--sds-c-card-text-color: var(--primary-color);

The code -sds-c-card-text-color: var(--primary-color) uses the var function to set the text color of a card to the value of the primary-color variable defined just above.

This makes it easy to change the color of the card by simply changing the value of the primary-color variable.

Next recipe

LWC GraphqlContacts