Twelve-column CSS Grid with gutters, column counts, and page margins from the design tokens at six breakpoints, plus span and placement classes.
Overview
The Chassis grid system arranges content with CSS Grid. .grid creates a twelve-column grid whose gap is the gutter token of the current breakpoint; span and placement classes position its items, with a variant for each of the six breakpoints.
<div class="container text-center">
<div class="grid">
<div class="col-span-4">.col-span-4</div>
<div class="col-span-4">.col-span-4</div>
<div class="col-span-4">.col-span-4</div>
</div>
</div>Three items of four tracks each fill the twelve columns. The parent .container centers the grid and adds the page margin.
The flexbox grid (.row, .col-*) was deprecated in v0.6.0 and removed in v0.7.0. See Migrating for the replacement of each class.
How it works
The grid has three parts — container, grid, and items — and reads its measures from custom properties on :root:
- Containers center and pad content.
.containerapplies amax-widthper breakpoint and apadding-inlineof--cx-container-padding, the page margin. A grid does not need a container:.gridhas no negative margins to cancel. See Containers. .griddefines the tracks. It setsdisplay: gridwith--cx-grid-columnsequal tracks ofminmax(0, 1fr), so content never widens a track, and agapof--cx-grid-gutter. It declares no rows: each line of items creates a row as tall as its content.- Items span tracks. A direct child of
.gridoccupies one track until a class says otherwise:.col-span-{n}spansntracks,.col-start-{n}and.col-end-{n}place the item on a grid line. Lines follow the writing direction, so a layout mirrors underdir="rtl"with no other class. See Columns. - The gutter follows the breakpoint.
:rootdeclares--cx-grid-gutterfrom the gutter tokens, again at each breakpoint where the value changes, so one grid has a narrow gap on a phone and a wide one on a desktop with no class.--cx-grid-gapor a gap utility overrides it per grid. See Gutters. - Breakpoints are min-width media queries. A prefixed class like
.md:col-span-6applies from themdbreakpoint upward. See Breakpoints. - Container variants follow the container. A class with the
@prefix, like.@md:col-span-6, applies from themdwidth of the nearest query container, not of the viewport. See Container queries. - Class names and declarations match Tailwind's.
col-span-6,col-start-3,col-end-13,row-span-2,grid-cols-4, andgrid-flow-densedeclare what the Tailwind utilities of the same names declare, so markup moves between the regular build and the Tailwind entry unchanged.
Grid options
Each breakpoint has a container max-width, a page margin, a gutter, and a column count, all read from the design tokens:
xs<576px | sm≥576px | md≥768px | lg≥1024px | xl≥1280px | 2xl≥1536px | |
|---|---|---|---|---|---|---|
Container max-width | None (auto) | 540px | 720px | 960px | 1200px | 1440px |
| Page margin | 1rem | 1rem | 1.5rem | 1.5rem | 1.5rem | 1.5rem |
| Gutter | 1rem | 1rem | 1.5rem | 1.5rem | 1.5rem | 1.5rem |
| Columns | 12 | 12 | 12 | 12 | 12 | 12 |
| Class prefix | None | sm: | md: | lg: | xl: | 2xl: |
Spanning
.col-span-{n} makes an item span n tracks, from 1 to 12. .col-span-full spans from the first grid line to the last, whatever the column count.
<div class="grid text-center">
<div class="col-span-8">.col-span-8</div>
<div class="col-span-4">.col-span-4</div>
<div class="col-span-full">.col-span-full</div>
</div>Auto columns
An item with no span class takes one track, so twelve of them fill a row:
<div class="grid text-center">
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
</div>Spanning and one-track items share a grid:
<div class="grid text-center">
<div class="col-span-6">.col-span-6</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
</div>Wrapping
An item that does not fit in the tracks left on a row moves to the next row. The gap applies between rows as it does between columns.
<div class="grid text-center">
<div class="col-span-9">.col-span-9</div>
<div class="col-span-4">.col-span-4: 9 + 4 is more than 12, so the item starts a new row</div>
<div class="col-span-6">.col-span-6</div>
</div>Placement
.col-start-{n} places an item on a grid line, from 1 to 12, and .col-start-auto returns it to automatic placement. .col-end-{n} sets the line on which it ends, from 1 to 13: .col-span-3 .col-end-13 puts three tracks against the end edge. .row-span-{n}, .row-start-{n}, and .row-end-{n} do the same on the row axis, and .order-* changes the visual order.
<div class="grid text-center">
<div class="col-span-3 col-start-2">.col-span-3 .col-start-2</div>
<div class="col-span-4 col-start-7">.col-span-4 .col-start-7</div>
</div>Columns covers placement, rows, ordering, and alignment in full.
Responsive classes
Every span and placement class has a variant per breakpoint. An unprefixed class applies at every width; a prefixed class applies from its breakpoint upward, until a class of a wider breakpoint overrides it.
Narrow viewports
The column count is a design token, twelve at every breakpoint by default. On a 320px viewport, twelve tracks between the page margins and eleven gutters are about 9px each, so a span of one to three tracks holds no content there. Start from .col-span-full or .col-span-6 and narrow the spans from a breakpoint up, as the examples below do, or lower the count of one grid with .grid-cols-{n}. A design with fewer columns on small screens sets them in the tokens or in $grid-column-counts, and --cx-grid-columns follows the breakpoint.
All breakpoints
Unprefixed classes keep one layout from the narrowest viewport to the widest.
<div class="grid text-center">
<div class="col-span-8">.col-span-8</div>
<div class="col-span-4">.col-span-4</div>
</div>Stacked to horizontal
.col-span-full with a prefixed span stacks the items below a breakpoint and sets them side by side from it. The unprefixed class is needed: an item with only .sm:col-span-8 is one track wide below sm, not full width.
<div class="grid text-center">
<div class="col-span-full sm:col-span-8">.col-span-full .sm:col-span-8</div>
<div class="col-span-full sm:col-span-4">.col-span-full .sm:col-span-4</div>
</div>Mix and match
Classes of several breakpoints on one item set its span at each of them.
<div class="grid text-center">
<div class="col-span-6 md:col-span-4">.col-span-6 .md:col-span-4</div>
<div class="col-span-6 md:col-span-4">.col-span-6 .md:col-span-4</div>
<div class="col-span-6 md:col-span-4">.col-span-6 .md:col-span-4</div>
</div>Column counts
.grid-cols-{n} on the grid sets the number of tracks, from 1 to 12, so equal items need no class of their own. Breakpoint variants change the count per breakpoint. See Grid utilities.
<div class="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-4 text-center">
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
</div>The span, start, and end classes have no print: variant. A printed page takes the classes of the breakpoint its paper width falls in, as a viewport of that width does. To lay a page out differently on paper, change the display of an element with the print display utilities — .print:d-none, .print:d-block, .print:d-grid — see Display in print.
Container queries
A breakpoint variant follows the viewport, and a grid in a sidebar, a card, or a grid item is narrower than the viewport. Every span, placement, and column count class has a second variant under the @ prefix — .@md:col-span-4, .@lg:grid-cols-3 — that applies when the nearest query container is at least as wide as the breakpoint. Resize the example below to preview the items going from stacked to two, then three, in a row.
<div class="contains-inline">
<div class="grid text-center">
<div class="col-span-full @sm:col-span-6 @md:col-span-4">.@sm:col-span-6 .@md:col-span-4</div>
<div class="col-span-full @sm:col-span-6 @md:col-span-4">.@sm:col-span-6 .@md:col-span-4</div>
<div class="col-span-full @sm:col-span-full @md:col-span-4">.@sm:col-span-full .@md:col-span-4</div>
</div>
</div>The prefixes are @sm:, @md:, @lg:, @xl:, and @2xl:, at the widths of the breakpoints. There is no @xs:: an unprefixed class applies at every width, and it is the layout of a grid with no query container above it.
| Classes | Container variants |
|---|---|
.col-span-{1…12}, .col-span-full | .@md:col-span-6, .@md:col-span-full |
.col-start-{1…12}, .col-start-auto | .@md:col-start-3, .@md:col-start-auto |
.col-end-{1…13}, .col-end-auto, .col-auto | .@md:col-end-13, .@md:col-auto |
.row-span-{1…6}, .row-start-{1…6}, .row-start-auto | .@md:row-span-2, .@md:row-start-auto |
.row-end-{1…7}, .row-end-auto, .row-auto | .@md:row-end-3, .@md:row-auto |
.grid-cols-{1…12}, .grid-cols-none, .grid-cols-subgrid, .grid-rows-* | .@md:grid-cols-3, .@md:grid-rows-2 |
.grid-flow-*, .auto-cols-*, .auto-rows-* | .@md:grid-flow-col, .@md:auto-rows-fr |
.gap-*, .row-gap-*, .column-gap-* | .@md:gap-lg |
The layout utilities have the same variants — display, flex, order, alignment, the fraction widths, text alignment, and float — so an element inside the container changes with it: .@md:d-flex, .@md:flex-row, .@md:w-6/12. See the container option of the utilities API for the full set and for adding a utility to it.
Query containers
An element is a query container when it has container-type: inline-size: add .contains-inline, or include set-container() in Sass. An element cannot query itself — a container variant reads the nearest ancestor that is a query container — so .contains-inline goes on an element around the grid. The classes of the grid (.@md:grid-cols-3, .contained) and of its items then read the same container.
Some components are query containers already: .navbar, .nav-overflow, .table-responsive, and .stepper-overflow. A grid inside one of them reads it.
With no query container among its ancestors, an element ignores its @ classes at every width.
Contained grids
The gutter and the column count of .grid come from :root, by the width of the viewport. .contained on a .grid or a .grid-fill takes both from the breakpoint of the query container instead, so a grid in a narrow column has the gutter of a narrow page. Resize the example below to preview the column count changing at the sm and md widths, and the gutter widening at md.
<div class="contains-inline">
<div class="grid contained grid-cols-1 @sm:grid-cols-2 @md:grid-cols-4 text-center">
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
</div>
</div>.contained sets --cx-grid-gutter and --cx-grid-columns on the grid itself, from the same maps as :root. --cx-grid-gap, a gap utility, and a column count class still override them. With no query container above the grid, .contained sets nothing, and the grid keeps the values of the viewport, as a plain .grid does.
With breakpoint variants
Breakpoint and container variants work on one element. Where both apply, the container variant wins: .md:col-span-6 .@md:col-span-4 spans six tracks on a viewport of the md width whose container is narrower, and four once the container reaches md too.
Alignment
Grid items stretch to the height of their row. .align-items-* on the grid and .align-self-* on an item change that; .place-items-* aligns on both axes. See Columns.
<div class="grid align-items-center text-center">
<div class="col-span-4">.col-span-4</div>
<div class="col-span-4">.col-span-4</div>
<div class="col-span-4">.col-span-4</div>
</div>Gaps
The gap of a grid is --cx-grid-gutter unless --cx-grid-gap or a gap utility sets another. .gap-{size}, .row-gap-{size}, and .column-gap-{size} take the sizes of the spacing scale. See Gutters.
<div class="grid gap-xs text-center">
<div class="col-span-6">.col-span-6</div>
<div class="col-span-6">.col-span-6</div>
<div class="col-span-6">.col-span-6</div>
<div class="col-span-6">.col-span-6</div>
</div>Auto-fit
.grid-fill lays its children out in equal columns of at least 12rem, as many as fit, with the gap of .grid. Children that no longer fit wrap to a new row, which makes a card grid responsive with no breakpoint class.
<div class="grid-fill text-center">
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
</div>The columns are an auto-fit repeat: a track with no item in it collapses, so fewer children than fit share the whole row instead of leaving empty columns at its end.
<div class="grid-fill text-center">
<div>Item</div>
<div>Item</div>
</div>--cx-grid-min sets another minimum width, on one grid or on an ancestor of several. $grid-min changes the default.
<div class="grid-fill text-center" style="--cx-grid-min: 6rem;">
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
<div>Item</div>
</div>Nesting
A .grid inside a grid item is a grid of its own, with twelve tracks across the width of the item. Nested spans count against the nested grid.
<div class="grid text-center">
<div class="col-span-full sm:col-span-3">Level 1: .sm:col-span-3</div>
<div class="col-span-full sm:col-span-9">
<div class="grid">
<div class="col-span-8 sm:col-span-6">Level 2: .col-span-8 .sm:col-span-6</div>
<div class="col-span-4 sm:col-span-6">Level 2: .col-span-4 .sm:col-span-6</div>
</div>
</div>
</div>--cx-grid-columns, --cx-grid-gap, and --cx-grid-min are inherited: set on an element, each applies to every .grid inside until another element sets it again. The classes apply to one grid: .grid-cols-{n}, .grid-rows-{n}, and a gap utility leave the grids nested in it as they are. A contained grid sets its gutter and column count on itself, so the grids nested in it inherit those of its container. Any other nested grid has the gutter of the viewport in a fraction of its width — see Narrow grids.
Subgrid
.grid-cols-subgrid on a nested .grid replaces its tracks with the tracks its item spans in the parent grid, so nested items align to the outer columns.
<div class="grid text-center">
<div class="col-span-8 grid grid-cols-subgrid">
<div class="col-span-4">Subgrid .col-span-4</div>
<div class="col-span-4">Subgrid .col-span-4</div>
</div>
<div class="col-span-4">.col-span-4</div>
</div>The subgrid has the tracks of the parent, and a gap of its own as every .grid does: the gutter or an inherited --cx-grid-gap, the same as the gap of the parent. A gap utility on the parent does not reach the subgrid, whose items then stop aligning to the outer columns: repeat the utility on the subgrid. Its rows stay its own: .grid-rows-subgrid adopts the rows of the parent too, on an item that spans several. .grid-cols-subgrid is a utility with the variants of the column counts, so a nested grid can become a subgrid from a breakpoint (.md:grid-cols-subgrid).
Common layouts
The span, start, and flow classes cover the page layouts that come up most. Each example is the whole markup of the layout.
Sidebar and content
The sidebar stacks above the content on narrow viewports and takes three of twelve tracks from a breakpoint. Add position: sticky and a top offset to the sidebar to keep it in view while the content scrolls.
<div class="grid text-center">
<div class="col-span-full md:col-span-3">Sidebar</div>
<div class="col-span-full md:col-span-9">Content</div>
</div>Three-column page
A full-width header and footer sit above and below a row of three items. Below the breakpoint every item is a row of its own, in DOM order.
<div class="grid text-center">
<div class="col-span-full">Header</div>
<div class="col-span-full md:col-span-2">Navigation</div>
<div class="col-span-full md:col-span-7">Main</div>
<div class="col-span-full md:col-span-3">Aside</div>
<div class="col-span-full">Footer</div>
</div>Card grid
Cards of equal width that wrap by themselves need no breakpoint class: see Auto-fit.
Gallery
Items of different sizes pack without holes, with dense auto flow and equal implicit rows.
<div class="grid grid-cols-4 grid-flow-dense auto-rows-fr text-center">
<div class="col-span-2 row-span-2">Large</div>
<div>Item</div>
<div>Item</div>
<div class="col-span-2">Wide</div>
<div>Item</div>
<div class="col-span-2">Wide</div>
<div>Item</div>
</div>Content-width columns
A grid sizes its tracks from its own width, not from the content. For items as wide as their content beside items that take the rest of the row, use a flex row: .d-flex with a gap utility, and .flex-fill on the items that grow. gap: var(--cx-grid-gutter) gives a flex row the gutter of the grid. See Flex utilities.
<div class="d-flex gap-md text-center">
<div class="flex-fill p-sm">.flex-fill</div>
<div class="p-sm">Content width</div>
<div class="flex-fill p-sm">.flex-fill</div>
</div>Customizing
Custom properties configure every grid inside an element at runtime. Each is inherited, so one set on a .grid applies to the grids nested in it too — see Nesting. For the column count of one grid, use .grid-cols-{n} or a grid-template-columns declaration.
| Property | Default | Description |
|---|---|---|
--cx-grid-columns | :root, from $grid-column-counts | Number of tracks of .grid; set by .contained per container width |
--cx-grid-gap | --cx-grid-gutter | Gap of .grid and .grid-fill; one value, or row-gap column-gap |
--cx-grid-min | 12rem, from $grid-min | Minimum column width of .grid-fill |
--cx-grid-gutter | :root, from $grid-gutters | Gutter of the current breakpoint; set by .contained per container width |
--cx-container-padding | :root, from $container-paddings | Page margin at the current breakpoint: the padding-inline of .container |
--cx-grid-gap and --cx-grid-min are not declared anywhere by default; .grid and .grid-fill fall back to the value in the table until an element sets them. The rows of a grid have no custom property: .grid-rows-{n} or a grid-template-rows declaration sets them.
<div class="grid text-center" style="--cx-grid-columns: 4; --cx-grid-gap: 2rem;">
<div class="col-span-2">.col-span-2</div>
<div class="col-span-2">.col-span-2</div>
</div>The span, start, and end classes are generated up to $grid-columns, the end lines one past it. A grid with more columns than that needs an inline grid-column for the spans without a class. A span wider than the grid, such as .col-span-8 in four columns, adds implicit tracks: they are empty, their gaps take room from the tracks of the grid, and in a narrow grid they overflow it. Keep the spans within the count of the grid they are in.
Migrating
The flexbox grid was removed in v0.7.0. Each of its classes has a replacement:
| Flexbox grid | CSS Grid | Notes |
|---|---|---|
.row | .grid | No .container needed around it |
.col-{n} | .col-span-{n} | |
.col-12, or no class below a breakpoint | .col-span-full | An item with no class is one track wide, not full width |
.offset-{n} | .col-start-{n + 1} | Exact for the first item of a row: an offset counts from the previous item, a start line from the edge of the grid |
.{breakpoint}:offset-0 | .{breakpoint}:col-start-auto | Resets a start line set at a narrower breakpoint |
.offset-0 | None | An item with no start class is placed automatically |
.row-cols-{n} | .grid-cols-{n} | |
.row-cols-auto | .grid-fill, or .d-flex .flex-wrap with .w-auto items | .grid-fill for equal columns, the flex row for content-width items |
.g-{size} | .gap-{size} | Not needed for the default gutter |
.g-0 | .gap-0 | The padding of the columns, which .g-0 also removed, is gone with the flexbox grid |
.gx-{size}, .gy-{size} | .column-gap-{size}, .row-gap-{size} | |
.col | .grid-fill on the parent, or .d-flex with .flex-fill | |
.col-auto | .d-flex on the parent | See Content-width columns |
.{breakpoint}:col, .{breakpoint}:col-auto | .{breakpoint}:flex-fill, .{breakpoint}:w-auto | In a flex row |
.col-{n} beside .col in one row | .col-span-{n} and .col-span-{12 - n} | A track has no "the rest": count the remaining span, or use a flex row with .w-{n}/12 and .flex-fill |
.justify-content-* on a .row | .col-start-* on the items | The tracks fill the grid, so there is no free space to distribute |
.align-items-*, .align-self-*, .order-* | Unchanged | |
.col-{n} outside a .row | .w-{n}/12 | A width, as on skeletons. See Sizing |
.col, .col-auto outside a .row | .flex-fill, .w-auto | |
.g-col-{n}, .g-start-{n} | .col-span-{n}, .col-start-{n} | |
.grid-cols-fill | .col-span-full |
The gap of .grid differs from the gutter of .row: it is the gutter token of the breakpoint, not 1.5rem at every width, and it applies between rows too.
The Sass of the flexbox grid went with its classes: the mixins make-row(), make-col-ready(), make-col(), make-col-auto(), make-col-offset(), row-cols(), and make-grid-columns(), the variables $grid-gutter-x, $grid-gutter-y, $grid-row-columns, $gutters, and $container-padding-x, the option $enable-grid-classes, and the $gutter parameter of make-container(). A @use ... with (...) that configures one of them no longer compiles. $grid-gutter-x goes without a deprecation notice in v0.6.0, which named the other variables only: set $grid-gutters for the gap of .grid.
CSS
The Grid system can be customized at both runtime (via custom properties) and compile time (via Sass variables).
.grid and .grid-fill are emitted when $enable-grid-system is true, the default. The option governs the whole grid system: these classes, the span, start, and end classes, .contained, and the grid utilities (.grid-cols-*, .grid-rows-*, .grid-flow-*, .auto-cols-*, .auto-rows-*). The gap utilities, the fraction widths, the containers, and the :root custom properties stay, as flex layouts and components read them. Until v0.7.0 the option was $enable-cssgrid and left the utilities in.
// No `grid-template-rows`: the rows are implicit, as tall as their content.
.grid {
display: grid;
grid-template-columns: repeat(var(--grid-columns), minmax(0, 1fr));
gap: var(--grid-gap, var(--grid-gutter));
}
// As many columns of at least `--grid-min` as fit, the same gap as `.grid`.
// Empty tracks collapse, so fewer items than fit share the whole row. In a
// grid narrower than the minimum, `min()` caps it at the grid's width, so
// the one column that fits does not overflow.
.grid-fill {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(var(--grid-min, #{$grid-min}), 100%), 1fr));
gap: var(--grid-gap, var(--grid-gutter));
}.contained and the @ variants of the span, start, and end classes are emitted when $enable-container-queries is true, the default. The same option governs the container variants of the utilities.
// The gutter and the column count by the width of the nearest query
// container, in place of the viewport's values on `:root`.
.grid.contained,
.grid-fill.contained {
@include grid-container-vars();
}With the default tokens, .contained compiles to:
@container (width >= 0) {
.grid.contained,
.grid-fill.contained {
--cx-grid-gutter: 1rem;
--cx-grid-columns: 12;
}
}
@container (width >= 768px) {
.grid.contained,
.grid-fill.contained {
--cx-grid-gutter: 1.5rem;
}
}@container (width >= 0) matches whenever an ancestor is a query container and never without one, so the values of the first breakpoint replace those of :root only inside a container.
Grid bundle
dist/css/chassis-grid.css, compiled from scss/chassis-grid.scss, is the layout system without the components: .grid, .grid-fill, and their classes with every variant, the containers, and the display, flex, alignment, order, gap, grid, margin, and padding utilities, with .contains-inline for the container variants and the fraction widths (.w-6/12). It declares the :root custom properties these read — the gutter, the page margin, the column count, the spacing scale, and the breakpoints — so it works with no other Chassis stylesheet.
Custom properties
:root declares the gutter, the page margin, and the column count. The value of xs is declared unconditionally, and each wider breakpoint only where its value differs from the one before. With the default tokens:
:root {
--cx-grid-gutter: 1rem;
--cx-container-padding: 1rem;
--cx-grid-columns: 12;
}
@media (width >= 768px) {
:root {
--cx-grid-gutter: 1.5rem;
--cx-container-padding: 1.5rem;
}
}Sass variables
$grid-columns and $grid-rows set how many span, start, and end classes are generated, and $grid-min the minimum column width of .grid-fill, a value of its own until @chassis-ui/website-tokens has a token for it.
$grid-columns: 12;
$grid-rows: 6;
$grid-min: 12rem; // minimum column width of `.grid-fill`, the fallback of `--grid-min`; `@chassis-ui/website-tokens` has no token for it yetSass maps
Three maps, keyed by breakpoint, feed the :root custom properties. Override a map, or the tokens it reads, before importing Chassis.
$grid-gutters: (
xs: $gutter-xsmall,
sm: $gutter-small,
md: $gutter-medium,
lg: $gutter-large,
xl: $gutter-xlarge,
2xl: $gutter-2xlarge
);$grid-column-counts: (
xs: $columns-xsmall,
sm: $columns-small,
md: $columns-medium,
lg: $columns-large,
xl: $columns-xlarge,
2xl: $columns-2xlarge
);$container-paddings: (
xs: $margin-xsmall,
sm: $margin-small,
md: $margin-medium,
lg: $margin-large,
xl: $margin-xlarge,
2xl: $margin-2xlarge
);$breakpoints and $container-max-widths define the breakpoints and the container widths.
$breakpoints: (
xs: 0,
sm: $breakpoint-small,
md: $breakpoint-medium,
lg: $breakpoint-large,
xl: $breakpoint-xlarge,
2xl: $breakpoint-2xlarge
);$container-max-widths: (
sm: $container-small,
md: $container-medium,
lg: $container-large,
xl: $container-xlarge,
2xl: $container-2xlarge
);Design tokens
The Grid system consumes design tokens from Chassis Tokens with the $cx- prefix. — See the design tokens page.
$breakpoint-2xlarge: $cx-grid-breakpoint-2xlarge;
$breakpoint-xlarge: $cx-grid-breakpoint-xlarge;
$breakpoint-large: $cx-grid-breakpoint-large;
$breakpoint-medium: $cx-grid-breakpoint-medium;
$breakpoint-small: $cx-grid-breakpoint-small;$container-2xlarge: $cx-grid-container-2xlarge;
$container-xlarge: $cx-grid-container-xlarge;
$container-large: $cx-grid-container-large;
$container-medium: $cx-grid-container-medium;
$container-small: $cx-grid-container-small;$gutter-2xlarge: $cx-grid-gutter-2xlarge;
$gutter-xlarge: $cx-grid-gutter-xlarge;
$gutter-large: $cx-grid-gutter-large;
$gutter-medium: $cx-grid-gutter-medium;
$gutter-small: $cx-grid-gutter-small;
$gutter-xsmall: $cx-grid-gutter-xsmall;$margin-2xlarge: $cx-grid-margin-2xlarge;
$margin-xlarge: $cx-grid-margin-xlarge;
$margin-large: $cx-grid-margin-large;
$margin-medium: $cx-grid-margin-medium;
$margin-small: $cx-grid-margin-small;
$margin-xsmall: $cx-grid-margin-xsmall;$columns-2xlarge: $cx-grid-columns-2xlarge;
$columns-xlarge: $cx-grid-columns-xlarge;
$columns-large: $cx-grid-columns-large;
$columns-medium: $cx-grid-columns-medium;
$columns-small: $cx-grid-columns-small;
$columns-xsmall: $cx-grid-columns-xsmall;Sass mixins
grid-root-vars() emits the three :root custom properties from the maps. Include it in a :root rule when compiling the grid without scss/_root.scss. Each map needs a value for the first breakpoint: the compile stops on a map without one, and warns about a key that is no breakpoint.
/// Emit the per-breakpoint custom properties of the layout system on `:root`:
/// `--grid-gutter` (the gap of `.grid`), `--container-padding` (the padding of
/// `.container`) and `--grid-columns` (the columns of `.grid`). The value of
/// the first breakpoint is declared unconditionally; a later breakpoint is
/// declared, in its `media-breakpoint-up()`, only where the map value changes.
///
/// @param {Map} $gutters [$grid-gutters] - Gutter per breakpoint
/// @param {Map} $paddings [$container-paddings] - Container padding per breakpoint
/// @param {Map} $columns [$grid-column-counts] - Column count per breakpoint
/// @param {Map} $breakpoints [$breakpoints] - Breakpoint map
@mixin grid-root-vars($gutters: $grid-gutters, $paddings: $container-paddings, $columns: $grid-column-counts, $breakpoints: $breakpoints) {
@include -grid-var-maps-check(("$gutters": $gutters, "$paddings": $paddings, "$columns": $columns), $breakpoints, "grid-root-vars()");
$vars: (
"grid-gutter": $gutters,
"container-padding": $paddings,
"grid-columns": $columns
);
@each $breakpoint, $changed in -grid-var-changes($vars, $breakpoints) {
@include media-breakpoint-up($breakpoint, $breakpoints) {
@each $name, $value in $changed {
--#{$name}: #{$value};
}
}
}
}make-cssgrid() generates the span, start, and end classes of every breakpoint.
/// Generate the placement classes of `.grid` for every breakpoint, with the
/// declarations of Tailwind's utilities of the same names: `col-span-{1…$columns}`,
/// `col-span-full` and `col-auto` (`grid-column`), `col-start-{1…$columns}` and
/// `col-start-auto` (`grid-column-start`), `col-end-{1…$columns + 1}` and
/// `col-end-auto` (`grid-column-end`), and the same on the row axis:
/// `row-span-{1…$rows}`, `row-auto`, `row-start-{1…$rows}`, `row-start-auto`,
/// `row-end-{1…$rows + 1}` and `row-end-auto`. The `auto` classes reset a span
/// or a line set at a narrower breakpoint.
///
/// @param {Number} $columns [$grid-columns] - Number of grid columns
/// @param {Number} $rows [$grid-rows] - Number of grid rows the row classes cover
/// @param {Map} $breakpoints [$breakpoints] - Breakpoint map
@mixin make-cssgrid($columns: $grid-columns, $rows: $grid-rows, $breakpoints: $breakpoints) {
@each $breakpoint in map.keys($breakpoints) {
@include media-breakpoint-up($breakpoint, $breakpoints) {
@include -cssgrid-placement(breakpoint-prefix($breakpoint, $breakpoints), $columns, $rows);
}
}
}make-cssgrid-container() generates their container variants, from the second breakpoint up.
/// Generate the container-query variants of the placement classes of
/// `make-cssgrid()`: `@md:col-span-4` applies when the nearest query container
/// is at least as wide as the `md` breakpoint. The first breakpoint has no
/// variant, the unprefixed classes apply at every width.
///
/// @param {Number} $columns [$grid-columns] - Number of grid columns
/// @param {Number} $rows [$grid-rows] - Number of grid rows the row classes cover
/// @param {Map} $breakpoints [$breakpoints] - Breakpoint map
@mixin make-cssgrid-container($columns: $grid-columns, $rows: $grid-rows, $breakpoints: $breakpoints) {
// Sass keeps an empty `@container` rule, where it drops an empty `@media`.
@if $columns > 0 or $rows > 0 {
@each $breakpoint in map.keys($breakpoints) {
$prefix: container-breakpoint-prefix($breakpoint, $breakpoints);
@if $prefix != "" {
@include container-breakpoint-up($breakpoint, null, $breakpoints) {
@include -cssgrid-placement($prefix, $columns, $rows);
}
}
}
}
}grid-container-vars() emits --cx-grid-gutter and --cx-grid-columns on a selector by the width of its query container, as .contained does. Include it in a grid on a custom selector.
/// Emit `--grid-gutter` and `--grid-columns` on the calling selector by the
/// width of its nearest query container, where `grid-root-vars()` sets them on
/// `:root` by the width of the viewport. The value of the first breakpoint is
/// declared in `@container (width >= 0)`, which matches whenever an ancestor
/// is a query container and never without one, so an element with no query
/// container above it keeps the viewport's values of `:root`; a later
/// breakpoint is declared, in its `container-breakpoint-up()`, only where the
/// map value changes.
///
/// @param {Map} $gutters [$grid-gutters] - Gutter per breakpoint
/// @param {Map} $columns [$grid-column-counts] - Column count per breakpoint
/// @param {Map} $breakpoints [$breakpoints] - Breakpoint map
@mixin grid-container-vars($gutters: $grid-gutters, $columns: $grid-column-counts, $breakpoints: $breakpoints) {
@include -grid-var-maps-check(("$gutters": $gutters, "$columns": $columns), $breakpoints, "grid-container-vars()");
$vars: (
"grid-gutter": $gutters,
"grid-columns": $columns
);
@each $breakpoint, $changed in -grid-var-changes($vars, $breakpoints) {
@if breakpoint-min($breakpoint, $breakpoints) == null {
// `container-breakpoint-up()` writes no query for the first breakpoint.
@container (width >= 0) {
@each $name, $value in $changed {
--#{$name}: #{$value};
}
}
} @else {
@include container-breakpoint-up($breakpoint, null, $breakpoints) {
@each $name, $value in $changed {
--#{$name}: #{$value};
}
}
}
}
}make-container() is covered in Containers.
Semantic layouts
A grid on a custom selector needs no mixin: three declarations that read the :root custom properties, and grid-column on the items inside the breakpoint mixins.
.example-container {
@include make-container();
max-width: 800px;
}
.example-layout {
display: grid;
grid-template-columns: repeat(var(--cx-grid-columns), minmax(0, 1fr));
gap: var(--cx-grid-gutter);
}
.example-content-main,
.example-content-secondary {
grid-column: 1 / -1;
}
.example-content-main {
@include media-breakpoint-up(sm) {
grid-column: span 6;
}
@include media-breakpoint-up(lg) {
grid-column: span 8;
}
}
.example-content-secondary {
@include media-breakpoint-up(sm) {
grid-column: span 6;
}
@include media-breakpoint-up(lg) {
grid-column: span 4;
}
}<div class="example-container">
<div class="example-layout">
<div class="example-content-main">Main content</div>
<div class="example-content-secondary">Secondary content</div>
</div>
</div>