Skip to main content
Login

Sign in to access your account

Flex - National Design System

A flex container lays out its children in one row or one column, with the alignment and gap you set

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:

  • the children sit in columns that change with the screen width: Grid
  • the parts stack down the page, each with space below it: Block

Markup

Options
Direction
Justify
Align
Gap
More
Preview
<div class="nds-flex"> <button type="button" class="nds-btn nds-primary"> <span class="nds-label">Submit</span> </button> <button type="button" class="nds-btn nds-secondary-outline"> <span class="nds-label">Save Draft</span> </button> <button type="button" class="nds-btn nds-secondary-outline"> <span class="nds-label">Preview</span> </button> <button type="button" class="nds-btn nds-secondary-outline"> <span class="nds-label">Cancel</span> </button> </div>

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, --align and --gap change the layout from the style attribute, with no new class.

  • Direction on Any Flex Element

    nds-row and nds-col work without nds-flex, on a component that is already a flex container, such as card actions.

  • Scoped Reverse

    nds-reverse reverses only a flex container. A component that has its own nds-reverse, such as a card, the stepper or the side info column, keeps its own meaning.

  • Wrap Control

    nds-wrap lets the children flow onto new lines. A min-width on 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-middle and nds-reverse change the items, and --justify and --align move 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 --gap with a spacing token, such as var(--spacing-sm).
  • Add --align: center when a row mixes children of different heights, such as a heading and a button.
  • Add nds-row or nds-col alone to a component that is already a flex container, such as nds-card-actions nds-row. Do not add nds-flex to it as well.
  • Do not use nds-reverse to 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 its style to fill the parent.
  • To make a flex container as wide as its content, add width: fit-content to its style.

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)
Last Modified Date: 03/11/2026 - 01:45 PM
Was this page useful?
60% of users said Yes from 2843 Feedbacks