@cssints/gradient-2d

mesh-gradient() and freeform-gradient() for cssints, as styles of cssints/plugin scope(). Where the browser has the CSS function, the style uses it. Where it does not, a runtime draws the gradient on a canvas and sets the drawing as the background. No browser has either function yet, so today the runtime draws every gradient; the first paint is freeform's stack of radial gradients or mesh's average colour.

Status (2026-10-17). The CSSWG adopted mesh and freeform gradients into css-images-5 on 2026-10-14, as an explainer (css-images-5/explainers/2d-gradients.md). That is not a spec draft: the syntax can still change. Freeform may be renamed diffusion-gradient(), and neither function's rendering rule is public, so both drawings here are this package's algorithm, not a browser's or Illustrator's.

import { cn, color } from "cssints" with { type: "cssints" };
import { freeformGradient, meshGradient } from "@cssints/gradient-2d" with { type: "cssints" };

export const hero = cn(meshGradient("red, blue; green, yellow"), color("white"));
export const panel = freeformGradient("red top left, gold 4 60% 40%, yellow bottom right");
@layer _.k {
  ._gradient2d-core {
    background-image: var(--gradient2d-fallback);
    background-repeat: no-repeat;
    background-size: 100% 100%;
  }
  /* only where the browser parses the function */
  @supports (background-image: mesh-gradient(red, blue; green, yellow)) {
    ._gradient2d-core {
      background-image: var(--gradient2d-native);
    }
  }
  ._gradient2d-ff {
    background-image: var(--gradient2d-ff-fallback);
    background-repeat: no-repeat;
    background-size: 100% 100%;
  }
  @supports (background-image: freeform-gradient(red top left, blue bottom right)) {
    ._gradient2d-ff {
      background-image: var(--gradient2d-ff-native);
    }
  }
}
@layer _.a {
  ._1gxhgq5 {
    --gradient2d-native: mesh-gradient(red, blue; green, yellow);
    --gradient2d-fallback: linear-gradient(#918d78, #918d78);
    --gradient2d-data: '{"space":"oklab","rows":[[[1,0,0,1],[0,0,1,1]],[[0,0.502,0,1],[1,1,0,1]]]}';
  }
  ._1ps31mi {
    --gradient2d-ff-native: freeform-gradient(red top left, gold 4 60% 40%, yellow bottom right);
    --gradient2d-ff-fallback:
      radial-gradient(circle at 0% 0%, #ff0000, transparent),
      radial-gradient(circle at 60% 40%, #ffd700, transparent),
      radial-gradient(circle at 100% 100%, #ffff00, transparent);
    --gradient2d-ff-data: '{"space":"oklab","points":[[0,0,1,[1,0,0,1]],[0.6,0.4,4,[1,0.8431,0,1]],[1,1,1,[1,1,0,1]]]}';
  }
}

The native value is the spec's text, as written. The data is the stops or points as sRGB numbers, for the runtime. Mesh's fallback is the average colour of its stops, in oklab; freeform's is a radial layer per point.

Syntax

meshGradient(mesh)

freeformGradient(points)

Browsers

The runtime

Limits

cd packages/gradient-2d && bun run test           # the maths in Node, the engine (kernels, data, errors, the runtime by targets)
cd packages/gradient-2d && bun run typecheck      # and the type tests
cd packages/gradient-2d && bun run test:browser   # Chrome (installed): both first paints, the pixels against the maths, resize, an added element