# @csstools/postcss-random-function

> Use the random function in CSS

Latest version **4.0.2** (published 2026-09-13) · MIT-0 license · 0 weekly downloads

## Install

```sh
npm install @csstools/postcss-random-function
pnpm add @csstools/postcss-random-function
yarn add @csstools/postcss-random-function
bun add @csstools/postcss-random-function
```

## Health

**Score 60/100 (C)** — status: active.

Positive: esm support; no vulnerabilities; recently updated; high maintenance score.

Warnings: low downloads; no types.

## Facts

| | |
|---|---|
| Version | 4.0.2 |
| Published | 2026-09-13 |
| First published | 2024-11-11 |
| Weekly downloads | 0 |
| License | MIT-0 |
| TypeScript types | none |
| Module format | ESM |
| Node | >=20.19.0 |
| Dependencies | 3 |
| Unpacked size | 6.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1051 |
| Maintainers | jonathantneal, alaguna, romainmenke |
| Keywords | css, postcss-plugin, random |

## Links

- npm: https://www.npmjs.com/package/@csstools/postcss-random-function
- Repository: https://github.com/csstools/postcss-plugins
- Homepage: https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-random-function#readme
- Issues: https://github.com/csstools/postcss-plugins/issues
- Funding: https://github.com/sponsors/csstools
- npm.io page: https://npm.io/package/@csstools/postcss-random-function

## Dependencies (3)

- [@csstools/css-calc](https://npm.io/package/@csstools/css-calc.md) ^3.4.0
- [@csstools/css-tokenizer](https://npm.io/package/@csstools/css-tokenizer.md) ^4.0.0
- [@csstools/css-parser-algorithms](https://npm.io/package/@csstools/css-parser-algorithms.md) ^4.0.0

## Alternatives

- [style-dictionary](https://npm.io/package/style-dictionary.md) — 2.0M weekly downloads
- [postcss-merge-idents](https://npm.io/package/postcss-merge-idents.md) — 1.7M weekly downloads
- [@fontsource/noto-sans](https://npm.io/package/@fontsource/noto-sans.md) — 93.0K weekly downloads
- [uglifycss](https://npm.io/package/uglifycss.md) — 71.6K weekly downloads
- [mat4-interpolate](https://npm.io/package/mat4-interpolate.md) — 23.3K weekly downloads

## Recent versions

- 4.0.2 (latest) — 2026-09-13
- 4.0.1 — 2026-09-06
- 4.0.0 — 2026-07-22
- 3.0.3 — 2026-05-13
- 3.0.2 — 2026-04-12
- 3.0.1 — 2026-02-21
- 3.0.0 — 2026-01-14
- 2.0.1 — 2025-05-27
- 2.0.0 — 2025-04-19
- 1.0.3 — 2025-02-23
- 1.0.2 — 2024-12-27
- 1.0.1 — 2024-11-11
- 1.0.0 — 2024-11-11

## README

# PostCSS Random Function [<img src="https://postcss.github.io/postcss/logo.svg" alt="PostCSS Logo" width="90" height="90" align="right">][PostCSS]

`npm install @csstools/postcss-random-function --save-dev`

[PostCSS Random Function] lets you use the `random` function, following the [CSS Values 5] specification.

```css
div {
	color: oklch(0.7 0.2 random(120deg, 240deg));
}

div {
	color: oklch(0.7 0.2 random(120deg, 240deg, 7deg));
}

div {
	margin: random(10px, 100px) random(10px, 100px);
	padding: random(--padding, 10px, 100px) random(--padding, 10px, 100px);
}

/* becomes */

div {
	color: oklch(0.7 0.2 calc(166.80407deg));
}

div {
	color: oklch(0.7 0.2 calc(169deg));
}

div {
	margin: calc(45.10305px) calc(45.10305px);
	padding: calc(73.36206px) calc(73.36206px);
}
```

> [!NOTE]
> Generated values are deterministic pseudo random numbers.
> Generating values twice with the same input will give the same result.
> The input length of the CSS source file is used as a random seed.

## Usage

Add [PostCSS Random Function] to your project:

```bash
npm install postcss @csstools/postcss-random-function --save-dev
```

Use it as a [PostCSS] plugin:

```js
const postcss = require('postcss');
const postcssRandomFunction = require('@csstools/postcss-random-function');

postcss([
	postcssRandomFunction(/* pluginOptions */)
]).process(YOUR_CSS /*, processOptions */);
```



## ⚠️ About custom properties

Given the dynamic nature of custom properties it's impossible to know what the variable value is, which means the plugin can't compute a final value for the stylesheet. 

Because of that, any usage that contains a `var` is skipped.

## Options

### preserve

The `preserve` option determines whether the original notation
is preserved. By default, it is not preserved.

```js
postcssRandomFunction({ preserve: true })
```

```css
div {
	color: oklch(0.7 0.2 random(120deg, 240deg));
}

div {
	color: oklch(0.7 0.2 random(120deg, 240deg, 7deg));
}

div {
	margin: random(10px, 100px) random(10px, 100px);
	padding: random(--padding, 10px, 100px) random(--padding, 10px, 100px);
}

/* becomes */

div {
	color: oklch(0.7 0.2 calc(166.80407deg));
	color: oklch(0.7 0.2 random(120deg, 240deg));
}

div {
	color: oklch(0.7 0.2 calc(169deg));
	color: oklch(0.7 0.2 random(120deg, 240deg, 7deg));
}

div {
	margin: calc(45.10305px) calc(45.10305px);
	margin: random(10px, 100px) random(10px, 100px);
	padding: calc(73.36206px) calc(73.36206px);
	padding: random(--padding, 10px, 100px) random(--padding, 10px, 100px);
}
```

[cli-url]: https://github.com/csstools/postcss-plugins/actions/workflows/test.yml?query=workflow/test
[css-url]: https://cssdb.org/#random-function
[discord]: https://discord.gg/bUadyRwkJS
[npm-url]: https://www.npmjs.com/package/@csstools/postcss-random-function

[PostCSS]: https://github.com/postcss/postcss
[PostCSS Random Function]: https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-random-function
[CSS Values 5]: https://drafts.csswg.org/css-values-5/#random

---
_Source: https://npm.io/package/@csstools/postcss-random-function · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
