Skip to content

Usage

---
import Card from "@components/Card.astro";
---

<div class="l-grid" style="--gr-min-width: calc((50rem / 2) - (var(--s0) * 1));">
  <Card />
  <Card />
  <Card />
  <Card />
  <Card />
  <Card />
</div>

Refresh yourself on how to use: https://gridbyexample.com/resources/

Use this layout pattern to create a grid of equally sized elements. Particulary useful for collections such as posts.

The parent container width and gap size will determine the number of items that can fit in a grid.

You can use the following formula to calculate the minimum width of a grid item:

/**
* ( [container width] / [grid item count]) - ([gap] * [grid item count - 1] )
*/
/* 3-column grid example */
.component {
--min-width: calc((var(--bp-48) / 3) - (var(--s0) * 2));
}

Use the --gr-repeat custom property to specify the number of items the grid can fit in a single row. If unset, a fallback value of auto-fit will used. In most cases, auto-fit is the desired value to ensure responsiveness.

Basing grid item min-width on a fluid container size

Section titled “Basing grid item min-width on a fluid container size”

Instead of basing the grid item width on a fixed value, we can use clamp to allow it to scale with the viewport.

.component {
--container-width: clamp(48rem, 83.33333333333334vw, 75rem);
--min-width: calc((var(--container-width) / 3) - (var(--s0) * 2));
}