Sub topic 5
Laying out content with the SLDS grid
The Salesforce Lightning Design System (SLDS) grid is a set of CSS classes built on flexbox. It arranges the base components inside your Lightning web component. This chapter covers how rows and columns are built, how sizes and breakpoints work, and the mistakes that most often break a layout.
It assumes you know basic flexbox, the structure of an LWC template, and the spacing and utility classes from the previous chapter.
How the grid works
The grid has two roles:
- A container with
slds-gridis a flex container (display: flex). - Its direct children are flex items. Add
slds-colto a child when you want it to take part in the grid's sizing and gutter rules.
There is no fixed row element and no fixed column count. A grid is one flex container. If the children don't wrap, they share one line. slds-wrap allows wrapping, which is how you build multi-row layouts from a single grid.
slds-col sets flex: 1 1 auto, so columns without a size class share the free space. A size class turns a column into a fixed fraction of the container's width.
Sizing columns
Size classes follow the pattern slds-size_N-of-M. The common denominators are 2, 3, 4, 6 and 12:
slds-size_1-of-1is the full width.slds-size_1-of-2is half.slds-size_4-of-12is one third.slds-size_8-of-12is two thirds.
Sizes only produce rows when the grid has slds-wrap. Without it, the browser shrinks the columns to keep them on one line.
The next template stacks two inputs on small screens and puts them side by side on medium and larger screens:
<template>
<div class="slds-grid slds-gutters slds-wrap">
<div class="slds-col slds-size_1-of-1 slds-medium-size_1-of-2">
<lightning-input label="First name"></lightning-input>
</div>
<div class="slds-col slds-size_1-of-1 slds-medium-size_1-of-2">
<lightning-input label="Last name"></lightning-input>
</div>
</div>
</template>Each column carries two size classes. The base class applies at every width. The prefixed class overrides it once the viewport is wide enough.
Responsive breakpoints
SLDS is mobile first. An unprefixed size class applies at all widths. A prefixed class applies at its breakpoint and above.
| Prefix | Applies from viewport width | Example class |
|---|---|---|
| none | 0 px | `slds-size_1-of-1` |
| `slds-small-` | 480 px | `slds-small-size_1-of-2` |
| `slds-medium-` | 768 px | `slds-medium-size_1-of-3` |
| `slds-large-` | 1024 px | `slds-large-size_1-of-4` |
These breakpoints are CSS media queries, so they respond to the width of the browser viewport. They do not respond to the width of the region your component sits in. A component in a narrow sidebar on a wide monitor still gets the medium and large classes. If the layout must follow the width of its container, test it in that placement. You may need a different strategy, such as fewer breakpoints or a slds-wrap layout that degrades by itself.
Gutters and spacing
Columns touch each other unless you add spacing. There are two ways to do it:
slds-gutterson the grid adds horizontal padding to its directslds-colchildren. Variants such asslds-gutters_small,slds-gutters_x-smallandslds-gutters_largechange the amount.slds-col_paddedon a single column adds padding to that column only.
Gutters are horizontal only. When columns wrap onto a second row, there is no vertical space between the rows. Add it with a margin utility on the columns:
<template>
<div class="slds-grid slds-gutters slds-wrap">
<div class="slds-col slds-size_1-of-1 slds-medium-size_1-of-3 slds-var-m-bottom_small">
<lightning-input label="City"></lightning-input>
</div>
<div class="slds-col slds-size_1-of-1 slds-medium-size_1-of-3 slds-var-m-bottom_small">
<lightning-input label="State"></lightning-input>
</div>
<div class="slds-col slds-size_1-of-1 slds-medium-size_1-of-3 slds-var-m-bottom_small">
<lightning-input label="Postal code"></lightning-input>
</div>
</div>
</template>The slds-var-m-bottom_small class is the spacing utility that uses a CSS variable. Prefer it over the older slds-m-bottom_small, as in the card example from the earlier chapter.
Alignment and ordering
Alignment classes on the grid map to flexbox properties:
slds-grid_align-center,slds-grid_align-end,slds-grid_align-spaceandslds-grid_align-spreadcontrol horizontal distribution.slds-grid_vertical-align-start,slds-grid_vertical-align-centerandslds-grid_vertical-align-endcontrol vertical alignment.slds-grid_verticalchanges the direction to a column. In that mode the sizing classes refer to height rather than width.slds-grid_reversereverses the visual order.
On a single column, slds-col_bump-left and slds-col_bump-right push the item away from its neighbours with auto margins. slds-order_1, slds-order_2 and so on change the visual order. Use them sparingly. Screen readers and keyboard navigation follow the DOM order, not the visual order.
This header row puts a title on the left and a button on the right, centered vertically:
<template>
<div class="slds-grid slds-grid_align-spread slds-grid_vertical-align-center slds-var-p-around_small">
<h2 class="slds-text-heading_small">Open cases</h2>
<lightning-button label="New case" variant="brand"></lightning-button>
</div>
</template>The children are flex items, so they take part in the alignment rules without slds-col.
Nesting grids
A column can contain another grid. Each grid sets its own gutters and wrapping, and sizes refer to the width of the parent column.
<template>
<div class="slds-grid slds-gutters slds-wrap">
<div class="slds-col slds-size_1-of-1 slds-large-size_8-of-12">
<div class="slds-grid slds-gutters slds-wrap">
<div class="slds-col slds-size_1-of-1 slds-small-size_1-of-2">
<lightning-input label="Street"></lightning-input>
</div>
<div class="slds-col slds-size_1-of-1 slds-small-size_1-of-2">
<lightning-input label="City"></lightning-input>
</div>
</div>
</div>
<div class="slds-col slds-size_1-of-1 slds-large-size_4-of-12">
<lightning-textarea label="Notes"></lightning-textarea>
</div>
</div>
</template>Keep nesting to two levels. Deeper structures are hard to debug and the inherited gutters become hard to reason about.
An alternative: lightning-layout
The base components lightning-layout and lightning-layout-item wrap the same flexbox ideas in attributes. Sizes use a 12-column scale, and each breakpoint has its own attribute.
<template>
<lightning-layout multiple-rows>
<lightning-layout-item size="12" medium-device-size="6" padding="around-small">
<lightning-input label="First name"></lightning-input>
</lightning-layout-item>
<lightning-layout-item size="12" medium-device-size="6" padding="around-small">
<lightning-input label="Last name"></lightning-input>
</lightning-layout-item>
</lightning-layout>
</template>multiple-rows plays the role of slds-wrap. Without it, the items stay on one line. Choose lightning-layout when you want attribute-based sizing and padding. Choose the raw grid classes when you need finer control, such as slds-order_* or slds-col_bump-*. Both are supported.
Common errors
- Columns never stack or wrap. The grid is missing
slds-wrap, or you usedlightning-layoutwithoutmultiple-rows. - Gutters have no effect.
slds-guttersonly pads direct children that haveslds-col. If you put a wrapperdivbetween the grid and the column, the padding is lost. - Sizes don't change at the breakpoint. The prefixed class needs a base class to override. Without
slds-size_1-of-1, the column falls back to automatic width below the breakpoint. - Rows touch vertically. Gutters are horizontal only. Add a bottom margin utility.
- Sizes behave oddly in a vertical grid. With
slds-grid_vertical,slds-size_*refers to height, and the container needs a fixed height to resolve it. - Custom CSS fights the grid. Setting
widthorflexon a column in your own stylesheet overrides the size classes. Use the classes, or the CSS rule you write has to win by specificity.
Exercise
A form uses a grid with slds-grid slds-gutters. Each field sits in <div class="slds-col slds-medium-size_1-of-2">. On a phone-sized screen the two fields are squeezed onto one line, and when the browser is wide the second row of fields touches the first. Which classes would you add or change to fix both problems?
Show answer
Add slds-wrap to the grid so columns can move to a new line. Add slds-size_1-of-1 to each column so it is full width below the medium breakpoint, which gives slds-col slds-size_1-of-1 slds-medium-size_1-of-2. Add a vertical spacing utility such as slds-var-m-bottom_small to each column, because gutters only add horizontal space.