@cssints/balance

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

text-wrap: balance and pretty for cssints, with a script that balances lines in browsers without text-wrap: balance

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

@cssints/balance

text-wrap: balance and text-wrap: pretty for cssints, as members of cssints/plugin scope(). css.textWrap("balance") already gives the native feature; what this package adds is the fallback: for browserslist targets that lack text-wrap: balance (BCD css.properties.text-wrap.balance: Chrome 114, Firefox 121, Safari 17.5), a 0.55 kB script balances the lines the way nytimes/text-balancer and react-wrap-balancer do.

import { cn, fontSize } from "cssints" with { type: "cssints" };
import { balance, pretty } from "@cssints/balance" with { type: "cssints" };

export const title = cn(balance(), fontSize("2rem"));
export const lead = pretty();
@layer _.k {
  ._balance-core {
    text-wrap: balance;
  }
  ._balance-prettyCore {
    text-wrap: pretty;
  }
  /* only when a target lacks text-wrap: balance */
  @supports not (text-wrap: balance) {
    ._balance-core {
      --balance-js: 1;
    }
  }
}

Browsers

  • Native: text-wrap: balance in Chrome 114, Firefox 121, Safari 17.5; text-wrap: pretty in Chrome 117 and Safari 26, not in Firefox.
  • The runtime covers every browser with ResizeObserver (Chrome 64, Firefox 69, Safari 13.1). bun run test:browsers checks it in Chrome, Firefox and WebKit, natively and with the native feature switched off: the script gives the same lines as the browser.

Rules

  • balance() is a kernel written as styles, css.textWrap("balance"), and a runtime entry for css.properties.text-wrap and css.properties.text-wrap.balance, which covers the kernel's support warnings. When a target lacks it, the sheet gets the fallback rule above: --balance-js: 1 inside @supports not (text-wrap: balance), so it is on only in a browser without the feature, and the module that uses balance() imports the script through virtual:cssints/runtime/balance.script (the runtime entry names it, script). When every target has it, neither the rule nor the script ships: the runtime module is empty. No support warning either way, and nothing to import by hand.
  • The runtime (@cssints/balance/runtime, no imports, 552 bytes minified and gzipped) acts on an element of class _balance-core whose computed --balance-js is set: it binary-searches the narrowest max-width (between half the width and all of it) that keeps the element's height, so the line count stays and the lines even out. It runs again when the element's parent resizes (ResizeObserver), when its text changes, when a marked element is added (MutationObserver), and when the fonts are ready.
  • pretty() is text-wrap: pretty and nothing more. There is no small polyfill: pretty weighs the whole paragraph to avoid orphans and ragged edges, and a browser without it wraps as usual, which is what pretty improves on. So it requires nothing, warns about nothing and ships nothing (a css.textWrap("pretty") warns for Firefox, this does not): its kernel is raw CSS, &{text-wrap:pretty}, since nothing would cover the warnings of one written as styles.
  • Conflicts. Both set text-wrap: a cn() that holds one of them and text-wrap* or white-space is a build error. balance()'s are derived from its kernel, cssints: balance: kernel core sets text-wrap (this style also sets text-wrap); pretty()'s raw kernel derives none, so an entry lists them: cssints: balance: it sets text-wrap (this style also sets text-wrap-style).

Limits

  • No conditions. A member is a kernel's class, global CSS that cannot be conditional: hover(pretty()) or media(...)(balance()) is a build error at the call (:hover wraps _balance-prettyCore, the marker of kernel balance.prettyCore, with no declaration of its own). Under a condition, use css.textWrap(...) (native only).
  • The fallback sets max-width on the element: the box narrows, where text-wrap: balance keeps it. Centred text needs margin-inline: auto on the element to stay centred. An inline max-width of your own is overwritten.
  • Horizontal writing modes only; the parent must not size itself from the element (a shrink-to-fit parent can loop with the ResizeObserver).
cd packages/balance && bun run test            # fixtures through the engine (kernel, fallback CSS by target, conflict)
cd packages/balance && bun run typecheck       # and the type test
cd packages/balance && bun run test:browsers   # Chrome, Firefox and WebKit through playwright-core

Credits

The idea is nytimes/text-balancer (Apache-2.0, The New York Times Company) and react-wrap-balancer (MIT, Shu Ding): find the narrowest width that keeps the line count by a binary search, and again when the parent resizes. The script is written here; no code is copied.

版本与发布记录

@cssints/balance 共发布 42 个版本,首次发布于 2026/10/11;过去 12 个月发布了 42 个版本。最新版本附带 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/balance 的常见问题

@cssints/balance 是什么?

text-wrap: balance and pretty for cssints, with a script that balances lines in browsers without text-wrap: balance

如何安装 @cssints/balance?

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

@cssints/balance 支持 TypeScript 吗?

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

@cssints/balance 支持 ESM 还是 CommonJS?

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

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

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

@cssints/balance 可以商用吗?

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

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

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

@cssints/balance 还在维护吗?

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