Overview
A grid is one class, nds-grid, on the element that holds the items. CSS custom properties in its style set the layout: a column count, a minimum column width, or a track template. Each can take another value on tablets and phones. Every child fills its cell. Only a divided grid runs a script.
Pick another component when:
Markup
Variants
Each example is a whole grid. Change its values to fit your content: the knobs are in the API tables below. Use either a column count or a track value on one grid, not both: a track value overrides the columns.
| Group | Option | Markup | On element | Use |
|---|---|---|---|---|
| Example | Equal columns (default) | — | — | Every child gets an equal column, all in one row. For a short, fixed set of items |
| Example | Columns per screen | canon #grid-columns |
— | A set number of equal columns, with fewer on tablets and phones. --mid-col and --min-col fall back to the next larger value |
| Example | Minimum width (hint: As many columns as fit; the rest wrap) | canon #grid-min-width |
— | As many columns as fit at a minimum width. It needs no values for smaller screens. Do not add a column count, which then overflows |
| Example | Main and side | canon #grid-main-side |
— | Columns of different widths, set with a grid-template-columns value. --mid-track: 1fr stacks them below 960px |
| Example | Fixed side column | canon #grid-fixed-side |
— | A side column at a set width and a main column that takes the rest |
| Example | Gaps | canon #grid-gaps |
— | Row and column gaps set apart. --gap sets both |
| Example | Centered items | canon #grid-center |
— | Children at their own width, in the middle of their columns. The text inside is centered too |
| Example | Divided (hint: A line between rows) | canon #grid-divided |
— | A line between rows, in the middle of the row gap, never under the last row. For items with no border of their own. The default gap doubles. It draws no line between columns |
| Example | Container width (hint: The grid follows its parent’s width, not the screen’s) | canon #grid-container |
— | A grid inside an nds-cq element follows that element’s width, not the screen’s. The same grid shows more columns in a wide column than in a narrow one. The container steps are 768px and 480px |
Built-in Features
-
Auto-fit Columns
With no settings, the children share the row in equal columns.
-
Minimum Width Wrapping
Set
--min-width, and the items wrap once they cannot fit at that width. No media queries needed. -
Responsive Column Tokens
Set a column count for each screen size with
--max-col,--mid-coland--min-col. A value you leave out falls back to the next larger one. -
Custom Track Layouts
Set columns of different widths for each screen size with
--max-track,--mid-trackand--min-track. -
Adaptive Gap Scaling
The default gap shrinks from 20px to 16px below 960px. A gap you set stays the same, and never goes under 8px there.
-
Alignment Tokens
--justifyand--alignset how the children sit in their cells.nds-centercenters them. -
Container-Aware (opt-in)
Add
nds-cqto any element around a grid, and the grid follows that element's width, such as a sidebar. It is opt-in, because that element no longer takes its width from its content.
Best Practices
- For a set of like items, such as cards, use a List: a
ulthat is a grid and sets the items’ look. Keep a plain grid for page regions, fields and link cards. - Use Equal columns for a short set of items that must stay in one row. Use a minimum width when the number of columns should follow the space.
- With a fixed column count, set values for smaller screens too, such as
--mid-col: 2and--min-col: 1. Without them, a phone gets the desktop column count. - Use tracks for columns of different widths, such as main content and a side column. Add
--mid-track: 1frto stack them below 960px. - Use either column values or track values on one grid, not both. A track value overrides the column count at the same screen size.
- Do not use a grid for one column of content. Normal page flow already does that.
- Do not build the page structure from grids inside grids. Use Section for page regions.
- Add
nds-cqonly when a grid sits in a narrow column and must follow that column’s width. Not on an element that takes its width from its content, such as an inline-block or a flex item with no width: it shrinks to nothing. - Set
--gapwith a spacing token, such asvar(--spacing-md). - Use
nds-dividedfor items with no border or background of their own. Cards already have an edge.
API
Classes
| Class | Element | Effect |
|---|---|---|
nds-grid |
The element that holds the items | Makes it a full-width grid |
nds-center |
.nds-grid |
Centers each child in its column and centers the text inside. Sets --justify: center |
nds-divided |
.nds-grid |
Draws a line in the middle of each row gap, in the divider color. The last row draws none. The default gap doubles |
nds-cq |
Any element around a grid | Makes the grid inside follow this element’s width. See the next table |
nds-last-row |
Each item in the last row of a divided grid | Set by the script, which moves it when the row changes. The grid’s row lines read it: the last row draws none. Do not set it yourself |
CSS Custom Properties
Set these in the style of .nds-grid.
| Property | Default | Controls |
|---|---|---|
--max-col |
auto-fit |
Number of equal columns at 960px and up |
--mid-col |
--max-col |
Number of columns from 600px to 959px |
--min-col |
--mid-col |
Number of columns below 600px |
--max-track |
none | A grid-template-columns value at 960px and up. It overrides the column count |
--mid-track |
--max-track |
Track value from 600px to 959px |
--min-track |
--mid-track |
Track value below 600px |
--min-width |
0 |
Smallest column width. With no column count, as many columns as fit, and the rest wrap |
--gap |
var(--spacing-2xl) |
Space between rows and columns. The default is 16px below 960px. On a divided grid, the default doubles |
--row-gap, --col-gap |
--gap |
Space between rows, or between columns, alone |
--justify |
stretch |
justify-items: how a child sits across its column |
--align |
start |
align-items. Children fill the row height, so it changes only a child with its own height |
Screen and Container Widths
| Values | Screen width | Inside .nds-cq |
|---|---|---|
--max-col, --max-track |
960px and up | wider than 768px |
--mid-col, --mid-track |
600px to 959px | 768px or less |
--min-col, --min-track |
below 600px | 480px or less |
The container widths apply only inside an nds-cq element. The screen widths still apply there too.
JavaScript
| Method | Effect |
|---|---|
NDS.Grid.init() |
Watches every divided grid on the page. It runs at page load |
NDS.Grid.refresh(el) |
Marks the last row of el, or of every divided grid inside it, now, and watches a grid added after load |
NDS.Grid.destroy(el) |
Stops watching el, or the divided grids inside it, and removes their nds-last-row marks |
The script runs only for a grid with nds-divided, and for a Drawer list in columns under a divided Drawer. It measures the rows, so a grid whose column count changes with its width marks itself again when it resizes. For a view that may hold other components, call NDS.init.mount(el) after it mounts and NDS.init.destroy(el) before it goes, rather than the grid calls. The full API is in the banner of _js/nds-grid.js.