Masonry Grid

Items of any height packed into columns by width, the source order kept for reading and Tab — laid out again whenever anything changes size, the moves animated; native where the browser can.

LayoutExperimental

Usage

Usage

  • A pine forest above the sea
  • A waterfall in a rocky gorge
  • A river through a green forest
  • Snowy mountains under clouds
  • Wild roses above a misty sea
  • A wooden table in a quiet café
  • A wooded summit under white clouds
  • A green bicycle against a wooden shed
import { NgOptimizedImage } from '@angular/common';import { Component, ViewEncapsulation } from '@angular/core';import {  ScFrame,  ScMasonryGrid,  ScMasonryGridItem,} from '@semantic-components/ui';
/** Photos of different heights: as many columns of at least 200px as fit, each photo into the shortest — in a list, the order the source’s, row by row. */@Component({

Anatomy

Two parts on your elements: the grid (a ul when the items are a list) and each item. The gaps are your classes.

import { ScMasonryGrid, ScMasonryGridItem } from '@semantic-components/ui';
<ul scMasonryGrid [columnWidth]="200" class="gap-4">  <li scMasonryGridItem>…</li>  <li scMasonryGridItem [span]="2">…</li></ul>

Layout

  • Columns: as many of at least columnWidth as fit the grid's width (at most maxColumns) — no breakpoints. The gaps: your classes (gap-4, gap-x-6 gap-y-2), read from the page.
  • Each item, in the source's order, goes into the shortest column — the first of the columns within 1em of it, as CSS grid-lanes does, so the rows read evenly — or, sequential, into the next in turn. span makes it several columns wide.
  • Before it is measured — on the server, at the first paint — it is a plain grid of the same columns and gaps: rows, never a pile. Then laid out whenever the grid or an item changes size (an image loading, text wrapping) or items come and go; the items move into place (not under reduced motion).
  • Right to left, the first column is on the right. Where the browser has display: grid-lanes, it lays out the shortest-column grid itself (data-native on the grid).

Accessibility

  • When the items are a list, use ul and li: the grid adds no roles.
  • The order on screen follows the source, row by row — the order of reading and of Tab: keep the source in the order it should be read.

Examples

Cards and spans

  • Groceries

    Milk, eggs, bread, and the good coffee.

  • Ideas

    A timeline that draws its own line. A dock that grows near the pointer. A marquee written once.

  • Call

    Ada, about Friday.

  • Reading

    Finish the chapter on layout; then the one on type, which everyone says is the best part of the book, and probably is.

  • Garden

    Water the roses.

  • Trip

    Book the train for the coast. Pack light: two shirts, the grey jumper, a book.

  • Code

    Rename Next away after the merge.

  • Music

    The Satie, again.

import { Component, ViewEncapsulation } from '@angular/core';import {  ScCard,  ScCardDescription,  ScCardHeader,  ScCardTitle,  ScMasonryGrid,  ScMasonryGridItem,} from '@semantic-components/ui';

Order

Shortest column

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9

Sequential

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
import { Component, ViewEncapsulation } from '@angular/core';import {  ScFrame,  ScMasonryGrid,  ScMasonryGridItem,} from '@semantic-components/ui';
/** Into the shortest column (balanced) — or, sequential, each into the next column in turn (predictable): the same tiles, both ways. */@Component({  selector: 'app-order-masonry-grid-demo',

Load more

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
import { Component, ViewEncapsulation, signal } from '@angular/core';import {  ScButton,  ScFrame,  ScMasonryGrid,  ScMasonryGridItem,} from '@semantic-components/ui';
/** Items added, or removed: the grid lays out again, and the others move into place (not under reduced motion). */@Component({

Columns and gaps

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
import { Component, ViewEncapsulation } from '@angular/core';import {  ScFrame,  ScMasonryGrid,  ScMasonryGridItem,} from '@semantic-components/ui';
/** maxColumns caps the count however wide; the gaps are your classes — wider across than down here (gap-x-6 gap-y-2). */@Component({  selector: 'app-columns-masonry-grid-demo',

RTL

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
import { Dir } from '@angular/cdk/bidi';import { Component, ViewEncapsulation } from '@angular/core';import {  ScFrame,  ScMasonryGrid,  ScMasonryGridItem,} from '@semantic-components/ui';
/** In a right-to-left page: the first column on the right, the rows read from the right. */@Component({

API reference

Each part takes class, merged over its own (cn: a later class wins).

PartSelectorDescription
ScMasonryGrid[scMasonryGrid] The grid: columns by width, the items placed. exportAs: columns(), native().
ScMasonryGridItem[scMasonryGridItem] An item, any height; span columns wide.
InputTypeDefaultDescription
columnWidthnumber240 The least width of a column, in pixels: as many as fit.
maxColumnsnumber | nullnull At most this many columns.
sequentialbooleanfalse Each item into the next column in turn, not the shortest.
InputTypeDefaultDescription
spannumber1 How many columns wide.