Menu

Menu

On this page

On this page

Grid

A simple grid based layout component.

npm install @vrembem/grid
Copied!
SCSS
@use "@vrembem/grid";
Copied!

Options

Grid is built as a Sass module. Sass options are used to control CSS output, while design token CSS variables control the styling of the component. Below are the available configuration options.

Sass

Grid comes with the following Sass configuration options that can be set using either the config module or Sass with clause methods.

SCSS
Unlock scroll
@use "@vrembem/grid" with (
  $config: (
    // Map for outputting grid--cols-[num] modifier and col-[num] utility variants
    // @type map
    "grid-cols": (
      "breakpoints": true,
      "default": 1,
      "total": 6
    ),

    // Map for outputting grid--rows-[num] modifier and row-[num] utility variants
    // @type map
    "grid-rows": (
      "breakpoints": true,
      "default": 1,
      "total": 6
    ),

    // Map for outputting the grid--gap-[key] modifier variants
    // @type map
    "grid-gap": (
      "breakpoints": true,
      "default": calc(tokens.get("spacing") * 8),
      "0": 0,
      "xs": calc(tokens.get("spacing") * 2),
      "sm": calc(tokens.get("spacing") * 4),
      "md": calc(tokens.get("spacing") * 8),
      "lg": calc(tokens.get("spacing") * 12),
      "xl": calc(tokens.get("spacing") * 16)
    ),

    // Map for outputting the grid--flow-[key] modifier variants
    // @type map
    "grid-flow": (
      "default": dense,
      "row": row,
      "col": column,
      "row-dense": row dense,
      "col-dense": column dense
    )
  )
);
Copied!

Design tokens

Grid uses design token CSS variables in a reference and fallback pattern. Variables are referenced in the component's styles but are not directly defined; instead, they are provided with sensible fallbacks.

VariableFallback
--vb-grid-gap
Copied!
2em
Copied!
--vb-grid-gap-x
Copied!
var(--vb-grid-gap)
Copied!
--vb-grid-gap-y
Copied!
var(--vb-grid-gap)
Copied!
--vb-grid-flow
Copied!
dense
Copied!
--vb-grid-cols
Copied!
1
Copied!
--vb-grid-rows
Copied!
1
Copied!

Basic usage

The most basic implementation of the grid layout consists of the grid container and it's children (grid items). Apply the grid class to the desired grid container with an optional grid--cols-[num] modifier to create a grid layout.

1
2
3
HTML
<div class="grid grid--cols-3">
  <div>...</div>
  <div>...</div>
  <div>...</div>
</div>
Copied!

grid--inline

Also available is the grid--inline modifier. This sets the display property to inline-grid instead of grid.

1
2
3
HTML
<div class="grid grid--inline grid--cols-3">
  <div>...</div>
  <div>...</div>
  <div>...</div>
</div>
Copied!

grid--cols-[num]

By default the grid class only defines a single column. To add additional columns to the grid layout, use the grid--cols-[num] modifiers. The number of variants available is controlled by the grid-cols config option (default: 6).

1
2
3
4
5
6
HTML
<div class="grid grid--cols-3">
  ...
</div>
Copied!

Available variants

  • grid--cols-1
  • grid--cols-2
  • grid--cols-3
  • grid--cols-4
  • grid--cols-5
  • grid--cols-6

Breakpoint variants

These modifiers come with media breakpoint variants. This allows changing styles based on a specific breakpoint key in the breakpoints config options map.

Current view-port width: ...

1
2
3
HTML
<div class="grid lg:grid--cols-3">
  ...
</div>
Copied!

Available variants

  • xs:grid--cols-[num]
  • sm:grid--cols-[num]
  • md:grid--cols-[num]
  • lg:grid--cols-[num]
  • xl:grid--cols-[num]

grid--rows-[num]

By default the grid class only defines a single row. To add additional rows to the grid layout, use the grid--rows-[num] modifiers. The number of variants available is controlled by the grid-rows config option (default: 6).

1
2
HTML
<div class="grid grid--cols-2 grid--rows-2">
  <div class="row-full">...</div>
  <div>...</div>
</div>
Copied!

Available variants

  • grid--rows-1
  • grid--rows-2
  • grid--rows-3
  • grid--rows-4
  • grid--rows-5
  • grid--rows-6

Breakpoint variants

These modifiers come with media breakpoint variants. This allows changing styles based on a specific breakpoint key in the breakpoints config options map.

Current view-port width: ...

1
2
HTML
<div class="grid grid--cols-2 lg:grid--rows-2">
  <div class="row-full">...</div>
  <div>...</div>
</div>
Copied!

Available variants

  • xs:grid--rows-[num]
  • sm:grid--rows-[num]
  • md:grid--rows-[num]
  • lg:grid--rows-[num]
  • xl:grid--rows-[num]

grid--gap-[key]

Modifiers used to set the gap property of a grid component. These modifiers are generated using key/value pairs of the grid-gap config options map. The default key sets the provided value on the flex component itself and is not output as a modifier.

1
2
3
4
HTML
<div class="grid grid--cols-2 grid--gap-xs">
  ...
</div>
Copied!

Available variants

  • grid--gap-0
  • grid--gap-xs
  • grid--gap-sm
  • grid--gap-md
  • grid--gap-lg
  • grid--gap-xl

grid--gap-x-[key]

Modifiers to set the column-gap value on a grid component. These modifiers are generated using key/value pairs of the grid-gap config options map.

1
2
3
4
HTML
<div class="grid grid--cols-2 grid--gap-x-xs">
  ...
</div>
Copied!

Available variants

  • grid--gap-x-0
  • grid--gap-x-xs
  • grid--gap-x-sm
  • grid--gap-x-md
  • grid--gap-x-lg
  • grid--gap-x-xl

grid--gap-y-[key]

Modifiers to set the row-gap value on a grid component. These modifiers are generated using key/value pairs of the grid-gap config options map.

1
2
3
4
HTML
<div class="grid grid--cols-2 grid--gap-y-xs">
  ...
</div>
Copied!

Available variants

  • grid--gap-y-0
  • grid--gap-y-xs
  • grid--gap-y-sm
  • grid--gap-y-md
  • grid--gap-y-lg
  • grid--gap-y-xl

Breakpoint variants

These modifiers come with media breakpoint variants. This allows changing styles based on a specific breakpoint key in the breakpoints config options map.

Available variants

  • xs:grid--gap-[key]
  • sm:grid--gap-[key]
  • md:grid--gap-[key]
  • lg:grid--gap-[key]
  • xl:grid--gap-[key]
  • xs:grid--gap-x-[key]
  • sm:grid--gap-x-[key]
  • md:grid--gap-x-[key]
  • lg:grid--gap-x-[key]
  • xl:grid--gap-x-[key]
  • xs:grid--gap-y-[key]
  • sm:grid--gap-y-[key]
  • md:grid--gap-y-[key]
  • lg:grid--gap-y-[key]
  • xl:grid--gap-y-[key]

grid--flow-[key]

Modifiers used to set the grid-auto-flow property of a grid component. By default, the grid component has auto-flow set to dense. These modifiers are generated using key/value pairs of the grid-flow config options map. The default key sets the provided value on the grid component itself and is not output as a modifier.

1
2
3
4
5
HTML
<div class="grid grid--cols-3 grid--rows-3 grid--flow-row">
  ...
</div>
Copied!

Available variants

  • grid--flow-row
  • grid--flow-col
  • grid--flow-row-dense
  • grid--flow-col-dense

Utilities

Grid utilities are intended to be used directly on the children of a grid component or grid container (elements with display: grid or display: inline-grid). Since these only change a single CSS property, utility classes are used instead of BEM element classes.

grid-col-[num]

A utility used to control the grid-column property which handles a grid item's size and location within a grid column. The number of variants available is controlled by the grid-cols config option (Default: 6).

1
2
3
4
5
6
HTML
<div class="grid grid--cols-6">
  <div class="grid-col-3">...</div>
  <div>...</div>
  <div>...</div>
  <div>...</div>
</div>
Copied!

Available variants

  • grid-col-auto
  • grid-col-1
  • grid-col-2
  • grid-col-3
  • grid-col-4
  • grid-col-5
  • grid-col-6
  • grid-col-full
  • grid-col-start-1
  • grid-col-start-2
  • grid-col-start-3
  • grid-col-start-4
  • grid-col-start-5
  • grid-col-start-6
  • grid-col-start-7
  • grid-col-end-1
  • grid-col-end-2
  • grid-col-end-3
  • grid-col-end-4
  • grid-col-end-5
  • grid-col-end-6
  • grid-col-end-7

Breakpoint variants

This utility also come with media breakpoint variants. This allows changing styles based on a specific breakpoint key in the breakpoints config options map.

Current view-port width: ...

1
2
3
4
5
HTML
<div class="grid grid--cols-6">
  <div class="grid-col-6 md:grid-col-3 lg:grid-col-2">...</div>
  <div class="grid-col-6 md:grid-col-3 lg:grid-col-2">...</div>
  <div class="grid-col-3 md:grid-col-6 lg:grid-col-2">...</div>
  <div class="grid-col-3 md:grid-col-3 lg:grid-col-3">...</div>
  <div class="grid-col-6 md:grid-col-3 lg:grid-col-3">...</div>
</div>
Copied!

Available variants

  • xs:grid-col-[num]
  • sm:grid-col-[num]
  • md:grid-col-[num]
  • lg:grid-col-[num]
  • xl:grid-col-[num]

grid-row-[num]

A utility used to control the grid-row property which handles a grid item's size and location within a grid row. The number of variants available is controlled by the grid-rows config option (Default: 6).

1
2
3
4
5
6
HTML
<div class="grid grid--cols-6 grid--rows-6">
  <div class="grid-row-2">...</div>
  <div>...</div>
  <div>...</div>
  <div>...</div>
</div>
Copied!

Available variants

  • grid-row-auto
  • grid-row-1
  • grid-row-2
  • grid-row-3
  • grid-row-4
  • grid-row-5
  • grid-row-6
  • grid-row-full
  • grid-row-start-1
  • grid-row-start-2
  • grid-row-start-3
  • grid-row-start-4
  • grid-row-start-5
  • grid-row-start-6
  • grid-row-start-7
  • grid-row-end-1
  • grid-row-end-2
  • grid-row-end-3
  • grid-row-end-4
  • grid-row-end-5
  • grid-row-end-6
  • grid-row-end-7

Breakpoint variants

This utility also come with media breakpoint variants. This allows changing styles based on a specific breakpoint key in the breakpoints config options map.

Current view-port width: ...

1
2
3
4
5
HTML
<div class="grid grid--cols-3 grid--rows-3 grid--flow-col-dense">
  <div class="md:grid-row-2 lg:grid-row-3">1</div>
  <div class="md:grid-row-2 lg:grid-row-3">2</div>
  <div class="md:grid-row-1">3</div>
  <div class="md:grid-row-1">4</div>
  <div class="md:grid-row-3 lg:grid-row-1">5</div>
</div>
Copied!

Available variants

  • xs:grid-row-[num]
  • sm:grid-row-[num]
  • md:grid-row-[num]
  • lg:grid-row-[num]
  • xl:grid-row-[num]