Sub topic 3
What SLDS is and how it relates to base components
This chapter assumes you can read a Lightning web component (HTML template, JavaScript class, CSS file) and know basic HTML and CSS. It covers what the Salesforce Lightning Design System (SLDS) is and how it connects to the lightning-* base components. Later chapters cover utility classes, grid layout and styling hooks in detail.
What SLDS is
SLDS is Salesforce's design system. It is a collection of CSS classes, design tokens, icons and usage guidelines that give Salesforce screens a consistent look. A design token is a named value, such as a color or a spacing size, that the system reuses everywhere.
SLDS provides three things you will use in components:
- CSS classes that start with
slds-, such asslds-gridorslds-var-p-around_medium. - Blueprints, which are documented HTML structures for patterns like cards, modals and tables. Each blueprint lists the exact markup and classes to use.
- Styling hooks, which are CSS custom properties (variables) that change how a component looks. You will meet these in a later chapter.
How SLDS relates to base components
Base components are the lightning-* elements. Each one is built by Salesforce using SLDS blueprints. When you write <lightning-button>, the component renders the SLDS button markup and classes inside itself. You do not add any slds- class to get the SLDS look.
Salesforce loads the SLDS stylesheet in Lightning Experience and in Experience Cloud sites that use the Lightning framework. Your components can use SLDS classes on their own elements without importing anything.
| Approach | Who writes the markup | Who applies SLDS | Example |
|---|---|---|---|
| Base component | Salesforce | The component, internally | `<lightning-input>` |
| SLDS-styled element | You | You, with `slds-` classes | `<div class="slds-box">` |
| Fully custom | You | You, with your own CSS | `<div class="my-panel">` |
A single template often mixes the first two rows. In the example below, lightning-card is a base component, and the div uses an SLDS class for padding.
<template>
<lightning-card title="Contact search" icon-name="standard:contact">
<div class="slds-var-p-around_medium">
<lightning-input type="search" label="Name"></lightning-input>
</div>
</lightning-card>
</template>What you can and cannot style
The SLDS classes you put on your own elements apply to those elements. They do not reach inside a base component. Each base component hides its internal markup behind a shadow DOM boundary, which is a browser feature that keeps one component's styles and elements separate from another's.
This has a practical effect. Adding class="slds-p-around_large" to a lightning-input styles the outer host element only. The inner label and input element are unaffected. To change the inside of a base component, use the attributes it documents or an SLDS styling hook.
Finding the right class
When you need an SLDS class, use the SLDS documentation site. Look up the utilities section for spacing, text and visibility, and the component blueprints for layout patterns. Prefer the slds-var- spacing classes, for example slds-var-p-around_medium, in LWC. They use design tokens, so they follow the theme that applies to the org.
Common mistakes
- Adding
slds-classes to a base component and expecting its internals to change. - Copying a blueprint's markup when a base component already provides it. Use
lightning-buttonrather than rebuildingslds-button. - Writing hard-coded colors and sizes when an SLDS class or token exists.
Exercise
A teammate adds class="slds-text-heading_large" to a lightning-input to enlarge its label, but the label does not change. Why, and what should they do?
Show answer
The class applies only to the outer host element. The label lives inside the base component's shadow DOM, so the class does not reach it. They should use the attributes the component documents, or an SLDS styling hook if one exists for that part.