Sub topic 11
Styling components to match site branding
This chapter assumes you can build a Lightning web component with its own CSS file. It also assumes you have used Experience Builder's page editor from the earlier chapters. The running example is the CaseList component. Here you will make it follow the site's colors and fonts without hard-coding them.
How branding reaches your component
An Experience Cloud site has a single source of branding: the Theme panel in Experience Builder. An admin picks colors, fonts, and button styles there. The site turns those choices into CSS custom properties (also called CSS variables). It sets them on the page root.
Your component should read those variables instead of using fixed colors. When the admin changes the brand color and republishes, every component that reads the variable updates. You don't have to edit or redeploy the component.
CSS custom properties are inherited, and they cross the shadow DOM boundary. Normal selectors don't cross it. A page stylesheet can't restyle the inside of your component, but a variable set on an ancestor can be read inside it. This is the mechanism the platform intends for branding.
Styling hooks in the site
The site exposes its branding as variables with the prefix --dxp-. Commonly used ones include:
| Variable | Meaning |
|---|---|
| `--dxp-g-brand` | Primary brand color |
| `--dxp-g-brand-contrast` | Readable text color on top of the brand color |
| `--dxp-g-root` | Page background color |
| `--dxp-g-root-contrast` | Readable text color on the page background |
| `--dxp-g-neutral` | Neutral surface color, such as card backgrounds |
The exact set of variables depends on the site template and runtime. LWR sites document their styling hooks more completely than Aura sites. Open the Theme panel and the Salesforce documentation for your template to confirm a variable exists before relying on it.
You can also read the variables live. In the browser developer tools of a published site, select the html element or the page root and look at the computed styles. This is often the fastest way to find which variable holds a value you want.
Using the variables with fallbacks
Always give var() a fallback value. The fallback is used when the variable is undefined. That happens in a Lightning Out context, in Jest tests, and in a site template that doesn't define the variable.
Here is a stylesheet for CaseList. Save it as caseList.css next to the other component files:
:host {
display: block;
padding: 1rem;
background-color: var(--dxp-g-root, #ffffff);
color: var(--dxp-g-root-contrast, #181818);
}
ul {
list-style: none;
margin: 0;
padding: 0;
}
li {
padding: 0.5rem 0.75rem;
border-left: 4px solid var(--case-accent, var(--dxp-g-brand, #0176d3));
margin-bottom: 0.5rem;
background-color: var(--dxp-g-neutral, #f3f3f3);
}
.login-prompt {
color: var(--dxp-g-brand, #0176d3);
font-weight: 600;
}Three things to notice:
:hoststyles the element that wraps your component. Use it for outer layout (display, padding, background). Withoutdisplay: block, a custom element renders inline.- The
liborder uses nested fallbacks. It tries--case-accentfirst, then the site brand variable, then a hard-coded color. - The stylesheet contains no
font-family. Fonts inherit from the site, so your text matches the surrounding page by default. Only set a font if you have a reason to override it.
Add class="login-prompt" to the guest message in the template:
<template>
<template lwc:if={isGuest}>
<p class="login-prompt">Log in to see your cases.</p>
</template>
<template lwc:elseif={hasCases}>
<ul style={listStyle}>
<template for:each={cases.data} for:item="c">
<li key={c.Id}>{c.CaseNumber}: {c.Subject}</li>
</template>
</ul>
</template>
<template lwc:else>
<p>No cases found.</p>
</template>
</template>Letting the page editor override a color
Sometimes an admin wants one component to use an accent color different from the brand. You can expose this as a design property, using the mechanism from the chapter on configurable properties. Add a String property to the meta file's targetConfig:
<targetConfig targets="lightningCommunity__Default">
<property name="maxRows" type="Integer" default="5" label="Maximum rows" />
<property name="accentColor" type="String" label="Accent color (CSS value)" />
</targetConfig>In the JavaScript class, add the @api property and a getter that sets the custom property inline. Only the changed parts of the class are shown here:
@api accentColor;
get listStyle() {
return this.accentColor ? `--case-accent: ${this.accentColor};` : '';
}When the property is empty, the getter returns an empty string and the stylesheet falls through to the site brand color. Setting a variable inline, instead of editing a class, keeps the CSS in one file and the override in one place.
Scoping and what shadow DOM does to your CSS
Component CSS is scoped to the component. A rule such as li { ... } in caseList.css affects only list items in your template. This has several consequences:
- Global stylesheets can't restyle you. A rule in the site's custom CSS, such as
.my-class { color: red; }, doesn't reach inside your template. Use variables for anything the site should control. - You can't restyle child components. If you place
lightning-buttonin your template, you can't target its inner elements with a selector. Use the styling hooks that the base component documents. - Parent CSS doesn't leak in. The reverse also holds: your styles don't leak out to the rest of the page.
- Elements you insert with
lwc:dom="manual"aren't styled. The scoping attributes aren't applied to nodes you add by hand. Style them from a parent or set styles in JavaScript.
SLDS and base components
The Lightning Design System (SLDS) is Salesforce's default stylesheet and class library. Whether its utility classes (such as slds-m-around_medium) are available to your component depends on the site. Aura sites built on the Customer Service template load it. In an LWR site, check what the template includes. Don't assume it is present.
Components in the lightning-* family, such as lightning-button and lightning-card, carry the styles they need. They also read the site's branding through hooks when the site template supports it. Prefer them over hand-built buttons when you want branding to apply automatically.
If your component must work in both a site and a plain Lightning Experience page, set its own spacing and colors in its CSS file. Use fallbacks. Then it looks acceptable in either place.
Responsive layout
Site visitors use phones, so test your component at narrow widths. Prefer flexible layouts, such as CSS grid with auto-fit, over fixed pixel widths:
.card-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
gap: 1rem;
}Media queries based on the viewport work. Be aware that a component in a narrow column of a wide page is not itself narrow, so a viewport query can choose the wrong layout. Fluid grids avoid that problem.
Common mistakes
- Hard-coding brand colors. The component looks right until the admin changes the theme.
- Omitting fallbacks. The component shows transparent backgrounds or default black text in tests and previews.
- Using a variable name from another template. An undefined variable silently falls back, so the bug is easy to miss. Check the computed styles.
- Targeting global classes. Selectors like
.slds-buttondon't match across a shadow boundary and have no effect. - Forgetting contrast. A brand color can be light. Pair it with its
-contrastvariable when text sits on top.
Exercise
A site admin changes the brand color in the Theme panel and republishes. Your component's li border still shows the old blue. The CSS contains border-left: 4px solid #0176d3;. What change fixes this, and why does it work without redeploying the component again after future theme changes?
Show answer
Replace the hard-coded color with border-left: 4px solid var(--dxp-g-brand, #0176d3);. The site publishes the brand color as the --dxp-g-brand custom property on the page root. Custom properties are inherited across the shadow boundary, so the component reads whatever value the Theme panel currently sets. The hex value is only a fallback for when the variable is undefined. After this one deployment, later theme changes need only a republish of the site.
Exercise
You add .slds-button { background: red; } to caseList.css to restyle a lightning-button in your template. Nothing changes. Why, and what should you do instead?
Show answer
Component CSS is scoped, and the button's internal elements sit inside its own shadow boundary. A selector in your stylesheet can't reach them. Use the styling hooks the base component documents, or set site-level branding through the Theme panel so the button picks it up. If you need full control, build your own button element and style it with your own CSS and var() calls.