Grid

On this page

The grid layer, src/3-grid, is a 12-column flex grid: a row extends %bf-d-flex-row, and each column %bf-col-{1-12}, with a breakpoint variant for each width that changes. The full reference is in docs/3-grid.

Each demo on this page is styled by sass/3-grid.sass, compiled to css/3-grid.css. Under each demo are its Sass, the CSS that Sassy outputs for it, and its HTML. In the demos, the shaded boxes are each column's content: the white space around them is the gutter.

Two columns that stack on small screens

Side by side from 768px; make the window narrower and they stack.

Left
Right
@use "sassy/src" as sassy

.row
    @extend %bf-d-flex-row

.half
    @extend %bf-col-12
    @extend %bf-col-6-md
@layer sassy.base {
  @layer grid {
    .row {
      --sassy-col-offset: 0;
      display: flex;
      flex-wrap: wrap;
      margin: 0;
      padding: 0;
    }
    .half {
      box-sizing: border-box;
      flex: 0 0 auto;
      margin-block: 0;
      margin-inline: var(--sassy-col-offset, 0) 0;
      padding: var(--sassy-grid-gutter, 0.5rem);
      width: 100%;
    }
  }
}
@layer sassy.md {
  @layer grid {
    @media (width >= 768px) {
      .half {
        box-sizing: border-box;
        flex: 0 0 auto;
        margin-block: 0;
        margin-inline: var(--sassy-col-offset, 0) 0;
        padding: var(--sassy-grid-gutter, 0.5rem);
        width: 50%;
      }
    }
  }
}
<div class="row">
    <div class="half">Left</div>
    <div class="half">Right</div>
</div>

A centred column

Two thirds wide, then half wide from 992px, centred by its offset at both widths.

Centred
.narrow
    @extend %bf-col-8
    @extend %bf-col-offset-2
    @extend %bf-col-6-lg
    @extend %bf-col-offset-3-lg
@layer sassy.base {
  @layer grid {
    .narrow {
      box-sizing: border-box;
      flex: 0 0 auto;
      margin-block: 0;
      margin-inline: var(--sassy-col-offset, 0) 0;
      padding: var(--sassy-grid-gutter, 0.5rem);
      width: 66.666%;
    }
    .narrow {
      --sassy-col-offset: 16.666%;
    }
  }
}
@layer sassy.lg {
  @layer grid {
    @media (width >= 992px) {
      .narrow {
        box-sizing: border-box;
        flex: 0 0 auto;
        margin-block: 0;
        margin-inline: var(--sassy-col-offset, 0) 0;
        padding: var(--sassy-grid-gutter, 0.5rem);
        width: 50%;
      }
    }
    @media (width >= 992px) {
      .narrow {
        --sassy-col-offset: 25%;
      }
    }
  }
}
<div class="row">
    <div class="narrow">Centred</div>
</div>

Cards in a row

The children have no placeholder: the row makes them columns, one to a row, then three from 768px, with a wider gutter.

One
Two
Three
.cards
    @extend %bf-d-flex-row
    @extend %bf-row-cols-1
    @extend %bf-row-cols-3-md
    @extend %bf-gutter-4
@layer sassy.base {
  @layer grid {
    .cards {
      --sassy-col-offset: 0;
      display: flex;
      flex-wrap: wrap;
      margin: 0;
      padding: 0;
    }
    .cards {
      --sassy-grid-gutter: 0.75rem;
    }
    .cards > * {
      box-sizing: border-box;
      flex: 0 0 auto;
      margin-block: 0;
      margin-inline: var(--sassy-col-offset, 0) 0;
      padding: var(--sassy-grid-gutter, 0.5rem);
      width: 100%;
    }
  }
}
@layer sassy.md {
  @layer grid {
    @media (width >= 768px) {
      .cards > * {
        box-sizing: border-box;
        flex: 0 0 auto;
        margin-block: 0;
        margin-inline: var(--sassy-col-offset, 0) 0;
        padding: var(--sassy-grid-gutter, 0.5rem);
        width: 33.333%;
      }
    }
  }
}
<div class="cards">
    <article>One</article>
    <article>Two</article>
    <article>Three</article>
</div>

A CSS grid

Stacked, then eight and four columns from 992px. The gap is only between the cells.

Main
Side
.layout
    @extend %bf-grid

.layout-main
    @extend %bf-grid-col-12
    @extend %bf-grid-col-8-lg

.layout-side
    @extend %bf-grid-col-12
    @extend %bf-grid-col-4-lg
@layer sassy.base {
  @layer grid {
    .layout {
      display: grid;
      gap: var(--sassy-grid-gap, 1rem);
      grid-template-columns: repeat(12, minmax(0, 1fr));
    }
    .layout-main, .layout-side {
      grid-column: auto/span 12;
    }
  }
}
@layer sassy.lg {
  @layer grid {
    @media (width >= 992px) {
      .layout-side {
        grid-column: auto/span 4;
      }
      .layout-main {
        grid-column: auto/span 8;
      }
    }
  }
}
<div class="layout">
    <main class="layout-main">Main</main>
    <aside class="layout-side">Side</aside>
</div>

Removing a column's gutter

A utility such as %bf-p-0 always overrides the grid, whatever order the @extend lines are in. The first column has no gutter, so its box reaches the edges.

No gutter
Gutter
Gutter
.third
    @extend %bf-col-4

.edge
    @extend %bf-col-4
    @extend %bf-p-0
@layer sassy.base {
  @layer grid {
    .third, .edge {
      box-sizing: border-box;
      flex: 0 0 auto;
      margin-block: 0;
      margin-inline: var(--sassy-col-offset, 0) 0;
      padding: var(--sassy-grid-gutter, 0.5rem);
      width: 33.333%;
    }
  }
  @layer utilities {
    .edge {
      padding: 0rem;
    }
  }
}
<div class="row">
    <div class="edge">No gutter</div>
    <div class="third">Gutter</div>
    <div class="third">Gutter</div>
</div>