Sub topic 11
Styling components to match site branding
This chapter assumes you can build a basic Lightning web component and that you know how Experience Builder exposes components to a page. You should also know the difference between Aura and LWR sites from earlier chapters. Here you will make a component pick up a site's brand colors and fonts instead of hard-coding its own.
How branding reaches your component
Experience Builder stores brand choices in a theme. Each choice, such as the brand color or the font, is published to the page as a CSS custom property (a variable written as --name). Your component can read these variables with var().
Lightning web components use shadow DOM, which blocks outside CSS selectors from reaching inside the component. CSS custom properties are the exception. They inherit through the shadow boundary, so a theme variable defined on the page is available in your stylesheet.
Using styling hooks
Salesforce publishes a set of styling hooks. These are CSS variables with the --dxp- prefix, such as --dxp-g-brand for the brand color and --dxp-g-root-contrast for text on the root background. Which hooks exist depends on the site runtime and theme, so check the hook list for your runtime before relying on one.
Always give var() a fallback value as its second argument. The fallback applies when you preview the component outside a site, in Jest, or on a theme that lacks the hook.
.case-card {
border: 1px solid var(--dxp-g-neutral-3, #c9c9c9);
border-radius: var(--dxp-s-button-radius-border, 0.25rem);
padding: 1rem;
}
.case-card h2 {
color: var(--dxp-g-brand, #0176d3);
}
.case-action {
background-color: var(--dxp-g-brand, #0176d3);
color: var(--dxp-g-brand-contrast, #ffffff);
}Letting authors choose styles
Hooks cover the global theme. If page authors need to override a color for one instance, expose a property in the meta file and apply it as an inline custom property. This reuses the property pattern from the chapter on configurable properties.
import { LightningElement, api } from 'lwc';
export default class CaseCard extends LightningElement {
@api accentColor = '';
get cardStyle() {
return this.accentColor ? `--card-accent: ${this.accentColor};` : '';
}
}<template>
<div class="case-card" style={cardStyle}>
<h2>Case summary</h2>
</div>
</template>The stylesheet then reads var(--card-accent, var(--dxp-g-brand, #0176d3)). The author's color wins if set. Otherwise the theme color applies, and the literal value is the last resort.
Common mistakes
- Hard-coding hex values. The component looks right in your org and wrong after a rebrand.
- Targeting SLDS or Lightning base component internals with selectors. Shadow DOM prevents it, and internal class names can change.
- Omitting fallbacks, which leaves properties unset in previews and tests.
- Assuming Aura hooks exist in LWR. Verify each hook on the runtime you ship to.
Exercise
Exercise
A component uses color: #0176d3; for its heading. After the site owner changes the brand color in Experience Builder, the heading stays blue. How do you fix it, and why does the fix work across the shadow DOM?
Show answer
Replace the literal with a styling hook and a fallback, for example color: var(--dxp-g-brand, #0176d3);. CSS custom properties inherit through the shadow boundary, so the theme's value reaches the component's stylesheet, while ordinary selectors from outside cannot.