Overview
A flex container is one class, nds-flex, on the element that holds the children. It lays them out in one row. Classes change the direction, the order and the wrapping. CSS custom properties in its style set the alignment and the gap. Flex needs no JavaScript.
Pick another component when:
Markup
Variants
Each Justify, Align and Gap option sets one custom property in the style of .nds-flex. Any value that the CSS property accepts works: see CSS Custom Properties in the API. Justify is off in a column: the column is only as tall as its children, so they have no room to move.
| Group | Option | Markup | On element | Use |
|---|---|---|---|---|
| Direction | Row (default) | — | — | The children sit side by side at their own widths, such as a set of buttons |
| Direction | Column | .nds-col |
.nds-flex |
The children stack, such as cards or form steps. A child with no width of its own, such as a card, fills the column |
| Direction | Column on phones (hint: A row above 600px, a column below) | .nds-col-sm |
.nds-flex |
A row on a large screen. Below 600px the children stack, such as two buttons. With nds-reverse, the stack keeps the markup order |
| Justify | Start (default) | — | — | The children sit at the start edge |
| Justify | Center | --justify: center |
.nds-flex:not(.nds-col) |
The children sit in the middle of the row. In a column, use Align: Center |
| Justify | End | --justify: flex-end |
.nds-flex:not(.nds-col) |
The children sit at the end edge, such as the actions under a form |
| Justify | Space between | --justify: space-between |
.nds-flex:not(.nds-col) |
The first child sits at the start and the last at the end. The rest of the space is shared between them. For a title and an action on one line |
| Align | Stretch (default) (hint: Each child fills the row height, or the column width) | — | — | Each child fills the row height, or the column width. A button keeps its own size |
| Align | Center | --align: center |
.nds-flex |
Children of different heights line up on their middles. In a column, each child keeps its own width and sits in the middle |
| Align | Start | --align: flex-start |
.nds-flex |
Each child keeps its own size and sits at the top, or at the start edge in a column |
| Align | End | --align: flex-end |
.nds-flex |
Each child keeps its own size and sits at the bottom, or at the end edge in a column |
| Gap | XS (hint: 4px between the children) | --gap: var(--spacing-xs) |
.nds-flex |
4px. Small children close together, such as icon buttons in a table cell |
| Gap | MD (default) (hint: 8px between the children) | — | — | 8px. Tags, chips and buttons |
| Gap | XL (hint: 16px between the children) | --gap: var(--spacing-xl) |
.nds-flex |
16px. Blocks in a column, such as cards or sections. A form’s field stack and a List of cards have it already |
| Gap | 3XL (hint: 24px between the children) | --gap: var(--spacing-3xl) |
.nds-flex |
24px. Larger children, such as cards in a column |
| Wrap | Wrap | .nds-wrap |
.nds-flex:not(.nds-col) |
The children flow onto a new line when the row is full. Row only: a column wraps only when it has a fixed height |
| Reverse | Reverse (hint: Shows the children in the opposite order, from the other edge) | .nds-reverse |
.nds-flex |
Shows the children in the opposite order. They also move to the other edge: with Justify Start they sit at the end. The keyboard order stays the markup order |
Built-in Features
-
CSS-Only
One class makes the layout. There is no script to load or start.
-
Default Layout
The children sit at the start edge, 8px apart. They stretch to the tallest child in a row, and to the full width in a column, unless a child has its own size, as a button does.
-
Custom Property API
--justify,--alignand--gapchange the layout from thestyleattribute, with no new class. -
Direction on Any Flex Element
nds-rowandnds-colwork withoutnds-flex, on a component that is already a flex container, such as card actions. -
Scoped Reverse
nds-reversereverses only a flex container. A component that has its ownnds-reverse, such as a card, the stepper or the side info column, keeps its own meaning. -
Wrap Control
nds-wraplets the children flow onto new lines. Amin-widthon each child makes them stack when the container gets narrow, whatever the screen width.
Best Practices
- For a set of cards whose rows fill, use a List with
nds-flex. On a list,nds-center,nds-middleandnds-reversechange the items, and--justifyand--alignmove the rows. - Use flex to lay out a few elements in one line, such as buttons, tags or icons. Use Grid for columns of content, and Section for page regions.
- For a row that becomes a column on phones, add
nds-col-sm. For columns whose count changes with the screen size, use a Grid with--max-col: 2; --min-col: 1;. - Set
--gapwith a spacing token, such asvar(--spacing-sm). - Add
--align: centerwhen a row mixes children of different heights, such as a heading and a button. - Add
nds-rowornds-colalone to a component that is already a flex container, such asnds-card-actions nds-row. Do not addnds-flexto it as well. - Do not use
nds-reverseto fix the order of content people read. A keyboard and a screen reader still follow the markup order. Use it only where the order on screen differs on purpose. - In normal page flow, a flex container is as wide as its parent. In a parent that centers it, it shrinks to its content: add
width: 100%to itsstyleto fill the parent. - To make a flex container as wide as its content, add
width: fit-contentto itsstyle.
API
Classes
| Class | Element | Effect |
|---|---|---|
nds-flex |
The element that holds the children | Makes it a flex container, in a row |
nds-row |
Any flex container, with or without nds-flex |
Lays the children out in a row |
nds-col |
Any flex container, with or without nds-flex |
Lays the children out in a column |
nds-col-sm |
Any flex container, with or without nds-flex |
Lays the children out in a column below 600px, and leaves the row above it |
nds-reverse |
.nds-flex, .nds-row or .nds-col |
Shows the children in the opposite order. It also swaps the start and end edges, so --justify: flex-start puts them at the end |
nds-center |
.nds-flex |
Centers the children along the row, the same as --justify: center. With nds-col it also centers them across. It centers the text inside too |
nds-middle |
.nds-flex or .nds-row |
Lines the children up on their middles, top to bottom, the same as --align: center. nds-center centers them along the row instead |
nds-wrap |
Any flex container | Lets the children flow onto new lines |
nds-nowrap |
Any flex container | Keeps the children on one line. Children that do not fit overflow |
CSS Custom Properties
Set these in the style of .nds-flex.
| Property | Default | Controls |
|---|---|---|
--justify |
flex-start |
justify-content: where the children sit along the row or the column, such as center, flex-end or space-between. flex-start and flex-end follow the text direction |
--align |
stretch |
align-items: how the children sit across the row or the column, such as center, flex-start or baseline |
--gap |
var(--spacing-md) |
Space between the children. A form’s field stack and a List of cards default to var(--spacing-xl) |