# @csstools/postcss-custom-functions

> Custom functions allow authors the same power as custom properties, but parameterized

Latest version **1.0.1** (published 2026-10-02) · MIT-0 license · 0 weekly downloads

## Install

```sh
npm install @csstools/postcss-custom-functions
pnpm add @csstools/postcss-custom-functions
yarn add @csstools/postcss-custom-functions
bun add @csstools/postcss-custom-functions
```

## Health

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

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

Warnings: low downloads; no types.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2026-10-02 |
| First published | 2026-10-01 |
| Weekly downloads | 0 |
| License | MIT-0 |
| TypeScript types | none |
| Module format | ESM |
| Node | >=20.19.0 |
| Dependencies | 4 |
| Unpacked size | 28.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | jonathantneal, alaguna, romainmenke |
| Keywords | css, custom-function, function, postcss, postcss-plugin |

## Links

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

## Dependencies (4)

- [@csstools/css-tokenizer](https://npm.io/package/@csstools/css-tokenizer.md) ^4.0.2
- [@csstools/css-parser-algorithms](https://npm.io/package/@csstools/css-parser-algorithms.md) ^4.0.2
- [@csstools/custom-function-parser](https://npm.io/package/@csstools/custom-function-parser.md) ^1.0.0
- [@csstools/cascade-layer-name-parser](https://npm.io/package/@csstools/cascade-layer-name-parser.md) ^3.0.2

## Recent versions

- 1.0.1 (latest) — 2026-10-02
- 1.0.0 — 2026-10-01

## README

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

`npm install @csstools/postcss-custom-functions --save-dev`

[PostCSS Custom Functions] lets you use `@function` custom functions following [CSS Custom Functions and Mixins 1].

Custom functions are still an early draft.  
This plugin is only a partial implementation to avoid conflicts with the final specification.

Unsupported:
- typed parameters without default values
- return types other than the default `type(*)`
- `@function` definitions inside `@container` or `@scope`
- `@function` definitions inside a `@layer` that is itself inside a conditional group rule
- `@function` definitions inside a style rule
- definitions whose body contains anything other than declarations and conditional group rules
- `result` descriptors that resolve to a CSS-wide keyword through a conditional rule or a `var()` fallback
- validation that a typed default value matches its parameter type

Definitions inside `@layer`, `@media` and `@supports` are supported. When a name
has several active definitions, the browser cascade selects the strongest one.

Unsupported definitions are left as-is. A definition is only removed when every
call site is transformed by this plugin, so call sites that are left as-is (for
example inside `@keyframes` or a nested conditional declaration) keep their
definitions.

Untyped parameters with default values are supported and may use any value,
including relative units and `var()` references.

Typed parameters with default values are checked by the browser through a
generated `@property` registration. A default value that is not
computationally independent (for example `1em` or `var(--x)`) can not be used
as an `initial-value` and makes the function unsupported.

```css
@function --negative(--value) {
	result: calc(-1 * var(--value));
}

@function --double(--value <length>: 1px) {
	result: calc(2 * var(--value));
}

html {
	--gap: 1em;
	padding: --negative(var(--gap));
	margin: --double(4px);
}

/* becomes */

html {
	--gap: 1em;
	--_csstools-cf-1gice1e-1-arg-0: var(--gap);
	--_csstools-cf-1gice1e-0-result: calc(-1 * var(--_csstools-cf-1gice1e-1-arg-0));
	padding: var(--_csstools-cf-1gice1e-0-result);
	--_csstools-cf-1gice1e-3-arg-0: 4px;
	--_csstools-cf-1gice1e-2-result: calc(2 * var(--_csstools-cf-1gice1e-3-arg-0));
	margin: var(--_csstools-cf-1gice1e-2-result);
}
@property --_csstools-cf-1gice1e-3-arg-0 {
	syntax: "<length>";
	inherits: false;
	initial-value: 1px;
}
```

## Usage

Add [PostCSS Custom Functions] to your project:

```bash
npm install postcss @csstools/postcss-custom-functions --save-dev
```

Use it as a [PostCSS] plugin:

```js
const postcss = require('postcss');
const postcssCustomFunctions = require('@csstools/postcss-custom-functions');

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



## Options

### preserve

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

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

```css
@function --negative(--value) {
	result: calc(-1 * var(--value));
}

@function --double(--value <length>: 1px) {
	result: calc(2 * var(--value));
}

html {
	--gap: 1em;
	padding: --negative(var(--gap));
	margin: --double(4px);
}

/* becomes */

@function --negative(--value) {
	result: calc(-1 * var(--value));
}

@function --double(--value <length>: 1px) {
	result: calc(2 * var(--value));
}

html {
	--gap: 1em;
	--_csstools-cf-1gice1e-1-arg-0: var(--gap);
	--_csstools-cf-1gice1e-0-result: calc(-1 * var(--_csstools-cf-1gice1e-1-arg-0));
	padding: var(--_csstools-cf-1gice1e-0-result);
	padding: --negative(var(--gap));
	--_csstools-cf-1gice1e-3-arg-0: 4px;
	--_csstools-cf-1gice1e-2-result: calc(2 * var(--_csstools-cf-1gice1e-3-arg-0));
	margin: var(--_csstools-cf-1gice1e-2-result);
	margin: --double(4px);
}

@property --_csstools-cf-1gice1e-3-arg-0 {
	syntax: "<length>";
	inherits: false;
	initial-value: 1px;
}
```

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

[PostCSS]: https://github.com/postcss/postcss
[PostCSS Custom Functions]: https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-custom-functions
[CSS Custom Functions and Mixins 1]: https://drafts.csswg.org/css-mixins-1/#function-rule

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