LearnFrenzy
Sign in Take a free test

Lifecycle, Slots and Styling · Lesson 13 of 25

Styling LWC: SLDS, Styling Hooks and CSS

4 min read Updated Nov 8, 2026 Saurabh Samir

Components should look like the rest of Salesforce – same spacing, colours and typography – and keep working when Salesforce updates its design. The tools for that are the Salesforce Lightning Design System (SLDS), the variants built into base components, styling hooks for controlled customisation, and your own CSS, which is automatically scoped to your component.

In this lesson

  • SLDS utility classes and base component variants
  • Scoped CSS and :host
  • Styling hooks: customising base components safely
  • Dynamic classes and light DOM

SLDS first

SLDS is available in every Lightning page – no import needed. Use its utility classes for spacing, layout, text and colour, and base components' variants before writing CSS:

<!-- kpiTile.html -->
<template>
    <article class="slds-box slds-theme_default slds-p-around_medium">
        <p class="slds-text-title_caps">{label}</p>
        <p class="slds-text-heading_large">{value}</p>
        <p class={trendClass}>{trendText}</p>
        <lightning-button variant="brand-outline" label="View report" class="slds-m-top_small"></lightning-button>
    </article>
</template>
// kpiTile.js
import { LightningElement, api } from 'lwc';

export default class KpiTile extends LightningElement {
    @api label = 'Pipeline';
    @api value = '$4.2M';
    @api trend = 12;

    get trendText() {
        return `${this.trend > 0 ? '▲' : '▼'} ${Math.abs(this.trend)}% vs last quarter`;
    }

    get trendClass() {
        return this.trend > 0 ? 'slds-text-color_success' : 'slds-text-color_error';
    }
}

Result

Closed this month
$860K
▼ 4% vs last quarter
[View report]
ClassDoes
slds-p-around_medium, slds-m-top_smallpadding and margin on the spacing scale
slds-grid, slds-col, slds-size_1-of-2responsive grid layout (or lightning-layout)
slds-text-heading_large, slds-text-title_capstypography
slds-text-color_success, slds-text-color_errorsemantic colours
slds-box, slds-theme_shade, slds-hidecontainers, backgrounds and visibility

Scoped CSS and :host

A component's CSS file only affects its own template – shadow DOM keeps styles in and out. :host targets the component's own element:

/* kpiTile.css */
:host {
    display: block;                    /* custom elements are inline by default */
}

.slds-text-heading_large {
    font-variant-numeric: tabular-nums;
}

:host(.compact) article {              /* when the parent adds class="compact" */
    padding: var(--slds-g-spacing-2, 0.5rem);
}

Because of scoping, a parent's CSS can't reach inside a child component, and you can't restyle the internals of base components with selectors like .slds-button – which is exactly what styling hooks solve.

Styling hooks

Base components expose CSS custom properties – styling hooks – for the things you're allowed to change. Set them on your own elements, and they flow into the base component:

/* brandButton.css */
.danger-action {
    --slds-c-button-brand-color-background: #ba0517;
    --slds-c-button-brand-color-border: #ba0517;
    --slds-c-button-radius-border: 2rem;
}
<!-- brandButton.html -->
<template>
    <lightning-button class="danger-action" variant="brand" label="Delete all drafts"></lightning-button>
</template>

Styling hooks are part of the supported API, so they survive Salesforce releases. Global hooks (--slds-g-…) describe the theme – colours, spacing, fonts – and are the right values to use in your own CSS too, so your component follows the org's theme and dark-mode settings.

Light DOM

For Experience Cloud sites with a global stylesheet, or when you need third-party CSS to reach inside, a component can render without shadow DOM:

import { LightningElement } from 'lwc';

export default class SiteBanner extends LightningElement {
    static renderMode = 'light';             // and <template lwc:render-mode="light"> in the HTML
}

Light DOM gives up style encapsulation – global CSS can then change your component – so use it deliberately, mostly on Experience Cloud.

Styling checklist

  • Base component variants and SLDS classes first; your own CSS second.
  • Customise base components only through styling hooks – never by targeting their internal classes.
  • Use --slds-g-* global hooks instead of hard-coded colours and sizes.
  • Add :host { display: block; } to block-level components.

Practice

Try it yourself

  1. Make a status badge whose class switches between slds-badge slds-theme_success and slds-badge slds-theme_error from an @api isHealthy property.

    Show solution
    <!-- healthBadge.html -->
    <template>
        <span class={badgeClass}>{text}</span>
    </template>
    // healthBadge.js
    import { LightningElement, api } from 'lwc';
    
    export default class HealthBadge extends LightningElement {
        @api isHealthy = false;
    
        get badgeClass() {
            return `slds-badge ${this.isHealthy ? 'slds-theme_success' : 'slds-theme_error'}`;
        }
        get text() {
            return this.isHealthy ? 'Healthy' : 'At risk';
        }
    }

    Result

    At risk
    
    › parent sets isHealthy = true
    Healthy

Quick check

Can a parent's CSS style elements inside a child component?

No – shadow DOM scopes styles. Use styling hooks or properties.

How do you change a lightning-button's background colour safely?

Set its styling hook, e.g. --slds-c-button-brand-color-background.

What does :host select?

The component's own host element.

Summary

  • Use SLDS classes and base component variants first.
  • Component CSS is scoped; :host styles the component element.
  • Customise base components with styling hooks; use light DOM only deliberately.

Finished this lesson?

Mark it complete to track your progress, then carry on with the next lesson.

Next lesson

Search LearnFrenzy

Esc

Quick links