Sub topic 4
Spacing, typography and utility classes
This chapter assumes you know the structure of a Lightning web component (template, JavaScript class, CSS file) and have met base components such as lightning-card and lightning-input. It covers the SLDS utility classes you use to space and style content without writing your own CSS.
What utility classes are
SLDS ships small, single-purpose CSS classes. Each class does one job, such as adding padding, setting a margin, or changing text size. You add them to the class attribute of ordinary HTML elements, such as a div, that wrap or sit next to base components.
Salesforce loads the SLDS stylesheet on pages where your component runs, so you do not import anything. You only choose class names.
The naming pattern
Most spacing classes follow this pattern:
slds-{property}-{side}_{size}- Property:
mfor margin,pfor padding. - Side:
around,top,right,bottom,left,horizontalorvertical. Omit the side to apply all four. - Size:
xxx-small,xx-small,x-small,small,medium,large,x-large,xx-large.
| Class | Effect |
|---|---|
| `slds-m-top_small` | Margin above the element |
| `slds-p-around_medium` | Padding on all four sides |
| `slds-m-bottom_large` | Margin below the element |
| `slds-p-horizontal_x-small` | Padding on left and right |
Use slds-var- versions, such as slds-var-p-around_medium and slds-var-m-top_small. These classes are built on CSS variables, and Salesforce recommends them over the older names.
Spacing a component
The search component from the card example uses one padding class on a wrapper:
<template>
<lightning-card title="Contact search" icon-name="standard:contact">
<lightning-button
slot="actions"
label="Clear"
onclick={handleClear}
></lightning-button>
<div class="slds-var-p-around_medium">
<lightning-input
type="search"
label="Name"
value={searchKey}
onchange={handleChange}
></lightning-input>
</div>
</lightning-card>
</template>The lightning-card body has no padding of its own, so the input would touch the card edge. The wrapper div adds medium padding on every side.
import { LightningElement } from 'lwc';
export default class ContactSearch extends LightningElement {
searchKey = '';
handleChange(event) {
this.searchKey = event.target.value;
}
handleClear() {
this.searchKey = '';
}
}Typography and other utilities
Text utilities work the same way. You add a class and the element changes.
| Class | Effect |
|---|---|
| `slds-text-heading_medium` | Heading style at medium size |
| `slds-text-title_caps` | Small uppercase title |
| `slds-text-color_weak` | Muted text color |
| `slds-text-align_center` | Centered text |
| `slds-truncate` | Cuts overflowing text with an ellipsis |
| `slds-hide` | Hides the element |
Classes combine freely:
<template>
<p class="slds-text-heading_medium slds-text-align_center slds-var-m-bottom_small">
No contacts found
</p>
</template>Common errors
- Putting the class on a base component. A class on
lightning-inputstyles the outer host element only. Spacing classes often work there, but text and color classes do not reach the inner elements. Wrap the component in adivand style the wrapper. - Mixing
slds-m-*andslds-var-m-*inconsistently. Pick theslds-var-form for new code so spacing stays consistent. - Misspelled class names. SLDS classes fail silently. If nothing changes, check for
_versus-, because the side and size are joined by an underscore. - Adding custom margin CSS instead of a utility. Hand-written pixel values drift from the design system. Use the utility first, and write CSS only when no class fits.
Exercise
A lightning-input inside a card touches the card's left and right edges. Which class on a wrapper div adds padding only on the left and right at medium size?
Show answer
slds-var-p-horizontal_medium. The property is p (padding), the side is horizontal, and the size is medium.
Exercise
You add slds-text-color_weak directly to a lightning-input and the label color does not change. Why, and what is the fix?
Show answer
The class applies to the outer host element, not to the label rendered inside the component's shadow DOM. Put the text in a wrapper element that has the class, or use a styling hook if the base component offers one.