@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

Rules

Limits

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.