Sub topic 3
What SLDS is and how it relates to base components
This chapter assumes you can read an LWC's HTML template, JavaScript class and CSS file, and that you know basic CSS (classes, selectors, custom properties). It explains what the Salesforce Lightning Design System (SLDS) is, how base components use it, and what that means for your own markup and styles.
What SLDS is
SLDS is the design system for the Salesforce user interface. It is a set of design guidelines plus a CSS framework. The CSS framework gives you:
- Component blueprints. HTML and CSS patterns for things such as cards, modals and badges. A blueprint is a recipe: you write the markup and apply the classes.
- Utility classes. Single-purpose classes for spacing, text, alignment, visibility and sizing, such as
slds-m-top_small. - Layout classes. The grid (
slds-grid,slds-col,slds-size_1-of-2) for arranging content. - Design tokens. Named values for colors, spacing and fonts. They keep Salesforce screens visually consistent.
- Styling hooks. CSS custom properties, such as
--slds-c-button-brand-color-background, that base components expose so you can change their appearance safely.
Every class in the framework starts with the slds- prefix. Variants and modifiers use a double hyphen or an underscore, for example slds-button_brand. If you see a class beginning with slds-, it comes from this framework.
How SLDS relates to base components
Lightning base components are the lightning-* elements, such as lightning-button, lightning-input and lightning-card. Salesforce builds them, and they already use SLDS internally. When you write <lightning-button variant="brand">, the component renders its own <button> with the SLDS classes that give it the brand look. You never write those classes.
That gives you two ways to get an SLDS look:
| Approach | What you write | Who applies SLDS | Typical use |
|---|---|---|---|
| Base component | `<lightning-button label="Save">` | The component, internally | Standard controls where a base component exists |
| SLDS blueprint on plain HTML | `<button class="slds-button slds-button_brand">` | You, by hand | A look that no base component provides |
Base components are the first choice. They apply the correct classes for you, handle keyboard behavior and ARIA attributes, and stay current when Salesforce updates the design. A hand-built blueprint makes you responsible for all of that.
The two approaches mix freely in one template. A common pattern is to use base components for controls and SLDS utility and layout classes on your own div elements around them.
<template>
<div class="slds-grid slds-grid_vertical-align-center slds-var-p-around_small">
<lightning-icon
icon-name="utility:info"
size="small"
alternative-text="Information"
></lightning-icon>
<p class="slds-var-m-left_small slds-text-body_regular">
Changes are saved when you click Save.
</p>
</div>
</template>Here the div and p use SLDS classes directly. The lightning-icon handles its own SLDS styling.
Where SLDS CSS comes from
In a Lightning Experience page, the SLDS stylesheet is already loaded. You do not import it into your component, and you should not copy it into your component's CSS file. Your template can use any slds- class as long as the framework exists on the page.
This has practical consequences:
- Local development and tests. Outside a Salesforce page, such as in a plain browser preview or a Jest run, the SLDS stylesheet is not present. Classes still appear in the markup, but nothing styles them. Jest tests check markup, not appearance.
- Other hosts. Where the SLDS stylesheet is not loaded,
slds-classes do nothing. That includes Lightning Out and some Experience Cloud sites, depending on the site's setup. Check the target host before relying on them. - Versions. Because Salesforce owns the stylesheet, class behavior can change between releases. Stick to documented classes.
The `slds-var-` spacing prefix
Spacing utilities exist in two forms. The older form is slds-m-top_small. The newer form is slds-var-m-top_small. Both set a margin on the top edge. The slds-var- form reads its value from an SLDS spacing variable, which lets the spacing follow the design system's density settings. Prefer the slds-var- form in new code, as the examples in this tutorial do. The classes mostly overlap, so a component that uses one form works if you later move to the other.
How SLDS and shadow DOM interact
Lightning web components use a shadow DOM (a private DOM tree per component) in most contexts. Styles do not cross the boundary freely, so you need to know how SLDS reaches each piece of markup.
flowchart TDThe paragraph above is replaced by the diagram below, which shows the same relationship.
The rules that follow from this:
slds-classes on your own elements work. Yourdiv,pandspanelements in your template receive SLDS styles, as long as the stylesheet is loaded.slds-classes on a base component's host element work for the host only.<lightning-input class="slds-m-top_small">puts a margin on thelightning-inputelement. It does not change anything inside it.- Your CSS cannot select inside a base component. A rule like
lightning-button button { background: red; }does nothing, because the innerbuttonis in another component's private DOM. This is intentional. The supported way to change a base component's look is a styling hook. - Your CSS file is scoped to your component. A rule in your CSS file affects only your template's elements, not a parent's or child's.
The chapter on style scoping covers the shadow DOM in more detail. For now, remember the boundary: you style your own markup with classes and CSS, and you style base components through attributes and styling hooks.
Customizing a base component the supported way
Styling hooks are CSS custom properties. CSS custom properties inherit across shadow boundaries, so a value you set on the host reaches the inside of the component. This is the one styling channel that crosses the boundary.
lightning-button {
--slds-c-button-brand-color-background: #0b5cab;
}This rule sets a hook on the lightning-button element. The button reads the hook when it renders its brand variant, so the background changes without any override of internal selectors. Not every property of every component has a hook. If a hook does not exist for what you want, your options are to choose a different variant, build a custom component, or accept the standard look.
Common errors
- Writing
slds-classes on a base component and expecting inner changes. Classes on the host affect the host only. Use attributes such asvariantandsize, or a styling hook. - Copying SLDS CSS into the component. It duplicates a large stylesheet and may conflict with the page's version. Rely on the platform's copy.
- Mixing a hand-built blueprint with its base component equivalent. Wrapping markup that already has
slds-buttoninside alightning-buttondoubles the styling and the result is unpredictable. Choose one. - Using undocumented classes. Inspecting a rendered page may show classes you could copy. Internal classes can change without notice. Use those in the SLDS documentation.
- Missing the stylesheet. If a component looks unstyled in a preview or another host, check whether the host loads SLDS.
- Using a deprecated spacing form without noticing. The
slds-m-*andslds-var-m-*forms look alike. Pick one and be consistent within a component.
A short worked example
This template builds a notice panel. It uses a base component for the icon and button, and SLDS classes for the panel's layout.
<template>
<div class="slds-box slds-theme_shade slds-var-m-around_medium">
<div class="slds-grid slds-grid_align-spread slds-grid_vertical-align-center">
<div class="slds-grid slds-grid_vertical-align-center">
<lightning-icon
icon-name="utility:warning"
size="small"
alternative-text="Warning"
></lightning-icon>
<span class="slds-var-m-left_small slds-text-heading_small">
Unsaved changes
</span>
</div>
<lightning-button
label="Dismiss"
variant="neutral"
onclick={handleDismiss}
></lightning-button>
</div>
</div>
</template>import { LightningElement } from 'lwc';
export default class NoticePanel extends LightningElement {
handleDismiss() {
this.dispatchEvent(new CustomEvent('dismiss'));
}
}Reading the template from the outside in:
slds-boxadds padding and a border, andslds-theme_shadegives a light gray background.slds-var-m-around_mediumadds margin on all sides.slds-gridmakes a flex container.slds-grid_align-spreadpushes the left group and the button to opposite ends.slds-grid_vertical-align-centercenters the items vertically.- The inner
slds-gridkeeps the icon and text on one line. lightning-iconandlightning-buttonstyle themselves. The only SLDS-related inputs you give them are attributes (size,variant).
Notice that the alternative-text on the icon is not decoration. It is the text a screen reader announces, and the accessibility chapter returns to it.
Exercise
A teammate adds class="slds-button_brand" to a <lightning-button> hoping to change its color, and nothing visible changes. Why, and what are two supported ways to get a brand-colored button with a custom shade?
Show answer
The class lands on the lightning-button host element, but the visible button is a <button> inside the component's private DOM, so the class does not style it. Supported ways: set the variant="brand" attribute to get the brand look, and set the styling hook --slds-c-button-brand-color-background on lightning-button in the component's CSS file to change the shade.
Exercise
You need a layout of two columns with 16px-ish spacing between a heading and a paragraph, using only your own div, h2 and p elements. Should you use a base component, an SLDS blueprint, or utility classes, and why?
Show answer
Use SLDS layout and utility classes (for example slds-grid, slds-col and slds-var-m-bottom_small) on your own elements. No base component is needed because you are arranging plain content, and utility classes style markup you own, which works because the page already loads the SLDS stylesheet.