Sub topic 5
Laying out content with the SLDS grid
This chapter assumes you know the structure of a Lightning web component (template, JavaScript class, CSS file). It also assumes you can add a utility class to an element, as covered in the previous chapter on spacing and utility classes.
What the SLDS grid is
The SLDS grid is a set of CSS classes built on flexbox. You apply the classes to ordinary div elements. A grid has two levels:
- A container with
slds-grid. It turns its direct children into a flex row. - Columns with
slds-col. These are the direct children of the container.
The grid is made of plain classes, so it works in any component template. It does not need a base component.
A responsive two-column form
The next template places two inputs side by side on medium and larger screens. On small screens it stacks them.
<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 class does one job:
| Class | Effect |
|---|---|
| `slds-grid` | Makes the element a flex container. |
| `slds-gutters` | Adds horizontal space between columns. |
| `slds-wrap` | Lets columns move to a new line when they do not fit. |
| `slds-col` | Marks the element as a column. |
| `slds-size_1-of-1` | Sets the column to full width. This is the default for all screen sizes. |
| `slds-medium-size_1-of-2` | Sets the column to half width on medium screens and larger. |
Size classes use the pattern slds-size_N-of-M, where M is usually 2, 3, 4, 5, 6 or 12. Add a breakpoint name (small, medium, large) to apply the size only from that screen width up.
Mobile first
SLDS sizing is mobile first. A class without a breakpoint applies everywhere. A class with a breakpoint overrides it from that width up. That is why the example sets slds-size_1-of-1 first and then slds-medium-size_1-of-2.
Alignment and spacing inside a grid
Add alignment classes to the container to position the columns:
slds-grid_align-centercenters the columns horizontally.slds-grid_align-spreadspreads them with equal space between.slds-grid_vertical-align-centercenters them vertically.
Without a size class, a column shares the leftover space equally. Add slds-col slds-grow-none to stop a column from growing, for example for an icon next to a text column.
Common errors
- Columns are not direct children.
slds-colonly works when its parent hasslds-grid. A wrapperdivin between breaks the layout. - No
slds-wrap. Without it, columns shrink to fit one row, and the stacking on small screens does not happen. - Missing default size. If you set only
slds-medium-size_1-of-2, the columns on a small screen use their content width, which is rarely what you want. - Gutters on the wrong element. Put
slds-gutterson the container, not on the columns.
Nesting grids
A column can itself be a grid container. Add slds-grid to a column and put new slds-col children inside it. This lets you build a page with a wide main region and a narrow sidebar, and a row of fields inside the main region.
Exercise
You want three equal columns on large screens, and one full-width column on small screens. Which classes do you put on each column?
Show answer
Use slds-col slds-size_1-of-1 slds-large-size_1-of-3. The container needs slds-grid slds-wrap, and you can add slds-gutters for spacing. The first size class sets full width by default. The second sets one third of the width from the large breakpoint up.
Exercise
Your two inputs stay side by side on a phone and their content is squeezed. Which class is probably missing from the container, and which is probably missing from the columns?
Show answer
The container is missing slds-wrap, so the columns cannot move to a new line. The columns are missing slds-size_1-of-1, so they have no full-width default for small screens.