@cssints/masonry

中文文档
0.35.1发布于 2026/11/07MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 7418

Masonry for cssints: display grid-lanes with a CSS columns fallback, as a chain member

原始使用文档
文档目录5 节
  1. Methods
  2. Browsers
  3. Rules
  4. Limits
  5. Credits

@cssints/masonry

A masonry layout for cssints: masonry(), a chain member of cssints/plugin scope(). Items of different heights are packed into as many columns as fit, each item under the shortest column, with no row gaps. It is CSS Grid Level 3's grid lanes (display: grid-lanes) where the browser has them, and CSS multi-column layout where it does not. No JavaScript, no media queries.

import { cn, p } from "cssints" with { type: "cssints" };
import { masonry } from "@cssints/masonry" with { type: "cssints" };

export const photos = masonry();
export const wall = cn(masonry().columns("12rem").gap(4), p(2));
@layer _.k {
  ._masonry-lanes {
    columns: var(--masonry-min);
    gap: var(--masonry-gap);
    --masonry-min: 16rem;
    --masonry-gap: 1rem;
    display: grid-lanes;
    grid-template-columns: repeat(auto-fill, minmax(min(var(--masonry-min), 100%), 1fr));
  }
  /* only when a browserslist target lacks grid lanes */
  @supports not (display: grid-lanes) {
    ._masonry-lanes > * {
      display: inline-block;
      inline-size: 100%;
      vertical-align: top;
      break-inside: avoid;
      margin-block-end: var(--masonry-gap);
    }
  }
}
@layer _.a {
  ._x {
    --masonry-min: 12rem;
  }
  ._y {
    --masonry-gap: 1rem;
  }
}

Methods

Each once, in any order:

MethodDefaultWhat it is
columns16remthe least width of a column: as many columns as fit, never wider than the container
gap1remthe space between columns and between an item and the next in its column (one value for both)

Both take a length (no percentage: the fallback's columns and margins cannot use one the same way). A lone number in gap is a step of the spacing scale (0.25rem), as in padding. Tokens go where a length does. The types check each method, and the build checks it again. A method in media() or another condition sets the custom property under it.

Browsers

  • Native: grid lanes. display: grid-lanes is the syntax the CSS Working Group settled on for masonry (CSS Grid Level 3, 2025), in place of grid-template-rows: masonry and display: masonry. Safari 26.4 ships it; Chrome and Edge (since 140) and Firefox (since 77, still on the old grid-template-*: masonry syntax) have it only behind a flag (October 2026). @mdn/browser-compat-data knows it as css.properties.display.grid-lanes.
  • Fallback: columns. The kernel's columns is the fallback: a browser that has grid lanes ignores it (a grid lanes container is not a block container), one that lacks them drops display: grid-lanes and lays the items out in columns. For a target that lacks grid lanes (the default targets do: Chrome and Firefox), the build adds the @supports not rule above for the items: a fallback of CSS only, so it reaches every target and covers the support warning of display: grid-lanes (its polyfills name the same BCD key). For targets that all have grid lanes (safari 26.4), the rules are not in the sheet.
  • Checked in Chrome 154 and Firefox 155 (columns) and Playwright's WebKit 26.6 (grid lanes, and the columns with the native value hidden): bun run test:browsers.

Rules

  • Kernel. One kernel in layer _.k, once, written as styles (css.display("grid-lanes"), gridTemplateColumns, gap, columns): the defaults of the custom properties on the element (a nested masonry does not inherit its parent's values), display, the columns and the gap. A call is its class (_masonry-lanes), and each method one class in _.a (or _.b under a condition), which wins over the defaults.
  • Conflicts, derived from the kernel. A cn() that holds masonry() and display, grid, grid-template, grid-template-columns, gap, row-gap, column-gap, columns, column-width or column-count is a build error at the call: cssints: masonry: kernel lanes sets display, grid-template-columns, gap, columns (this style also sets display). Padding, sizes and colours do not conflict. Conflicts with an opaque runtime string in cn() are not seen, as for any plugin.

Limits

  • The order differs. Grid lanes put each item in the shortest column, so the items read across, roughly row by row. The columns fallback fills the first column top to bottom, then the next: item 2 sits under item 1, not beside it. When the order matters (a feed, a timeline sorted by date), the fallback shows it down the columns.
  • The fallback sets the items' box. Each child is an inline block as wide as its column, with a bottom margin of gap (a block's margin before a column break moves the next column down in WebKit). An item shorter than its line height gets that height; a display of your own on an item replaces the inline block, and in Safari before 26.4 its column may then start a gap lower. The columns are balanced, so the last items may leave one column longer.
  • Not here: flow-tolerance (cssints has it, flowTolerance(), Safari 26.4 only: set it beside masonry()), rows as lanes (grid-template-rows, a horizontal masonry), items that span lanes, and a JavaScript polyfill (masonic, solid-mason: too heavy for a fallback that CSS gives).
cd packages/masonry && bun run test   # fixtures through the engine (output for both kinds of targets, conflicts, build errors, a build), and the type test via bun run typecheck
bun run test:browsers                 # Chrome, Firefox and WebKit through playwright-core: items pack with no row gaps

Credits

The layout is CSS Grid Layout Module Level 3 (the W3C CSS Working Group), display: grid-lanes as WebKit ships it. Nothing is copied.

版本与发布记录

@cssints/masonry 共发布 43 个版本,首次发布于 2026/10/11;过去 12 个月发布了 43 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.35.1latest2026/11/073 天前
0.35.02026/11/073 天前
0.34.02026/11/073 天前
0.33.02026/11/046 天前
0.32.02026/11/046 天前
0.31.02026/11/047 天前

关于 @cssints/masonry 的常见问题

@cssints/masonry 是什么?

Masonry for cssints: display grid-lanes with a CSS columns fallback, as a chain member

如何安装 @cssints/masonry?

在项目目录运行 npm install @cssints/masonry,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:cssints(^0.35.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@cssints/masonry 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@cssints/masonry 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@cssints/masonry 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@cssints/masonry 可以商用吗?

@cssints/masonry 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@cssints/masonry 的下载量有多少?

根据 npm 官方下载统计,@cssints/masonry 在 2026/10/11 – 2026/11/09 这 30 天内被下载 7,418 次,日均约 247 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@cssints/masonry 还在维护吗?

最新版本 0.35.1 发布于 2026/11/07(3 天前);至今共发布 43 个版本,其中过去 12 个月发布了 43 个;首次发布于 2026/10/11。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。