How to Write Your Own CSS Functions with @function
With the CSS @function rule, you can write functions in CSS that take arguments and return a value. You can use them to combine repeated calc() expressions into one function, such as keeping spacing in multiples of 8px, creating a semi-transparent version of a brand color, or changing font size based on the viewport width.
@function --double(--value) {
result: calc(var(--value) * 2);
}
p {
padding: --double(10px); /* 20px */
}
The test environment was Ubuntu 24.04, Node.js 22.23.5, Playwright 1.63.0, and Chromium 158. I opened pages with Playwright and checked the computed values with getComputedStyle(). I did not test rendering in browsers other than Chromium, such as Firefox or Safari. The browser support versions are from the compatibility data (@mdn/browser-compat-data 8.1.11).
- 1. Define with @function, return the value with result
- 2. Set argument types and default values
- 3. Values used only inside the function, and outer custom properties
- 4. Wrap values that contain commas in {} when passing them
- 5. Be careful about order when changing the returned value with @media
- 6. Example uses: spacing steps and font sizes that scale with the viewport
- 7. When a calculation fails, the earlier value does not come back
- 8. Preparing for browsers that do not support it
Define with @function, return the value with result
You define a function with @function. Its name starts with --, like custom properties, and the arguments are listed in parentheses. The return value is written as result in the function body. Arguments are treated as custom properties that exist only inside the function, so you read them with var(--value).
To call the function, write something like --double(10px) in a property value. I tried the basic example from MDN as it is.
p {
--base-spacing: 10px;
border-radius: var(--base-spacing);
padding: --double(var(--base-spacing));
}
In the test environment, border-radius was 10px and padding was 20px.
Set argument types and default values
If you write a type after an argument name, the argument accepts only values of that type. After returns, you write the type of the return value. The following is the function from the MDN explanation that makes a color semi-transparent.
@function --transparent(--color <color>, --alpha <number>) returns <color> {
result: oklch(from var(--color) l c h / var(--alpha));
}
background-color: --transparent(#00c8e8, 0.5) became oklch(0.766223 0.13511 214.688 / 0.5). This is the color #00c8e8 converted to OKLCH with an opacity of 0.5.
If you pass a value of the wrong type, the result is invalid. When I passed 50% to --alpha, which is <number>, the computed value was rgba(0, 0, 0, 0) (transparent), even though I had written background-color: red before it. If you also want to accept %, list multiple types with type(), such as --alpha type(<number> | <percentage>). After I changed it this way, passing 50% produced a color with an opacity of 0.5.
If you write : value after an argument, it becomes the default value.
@function --transparent(--color <color>, --alpha <number>: 0.8) returns <color> {
result: oklch(from var(--color) l c h / var(--alpha));
}
When I omitted the second argument, as in --transparent(#00c8e8), the opacity was 0.8. When I passed 50% to an argument that has a default value, the value was not invalid, and the default 0.8 was used. MDN also says that when an invalid value is passed and a default exists, the default is used.

Values used only inside the function, and outer custom properties
Besides result, the function body can contain custom properties. These are values that exist only inside the function.
@function --anim-1s(--animation, --count) {
--duration: 1s;
--easing: linear;
result: var(--animation) var(--duration) var(--count) var(--easing);
}
.ball {
animation: --anim-1s(bounce, 2);
}
In the test environment, the computed value of animation was 1s linear 2 bounce.
Inside the function, you can also read custom properties of the element that called it. When I used z-index: --double-z() (whose body is calc(var(--z) * 2)) on an element with --z: 3, z-index was 6. When the same name exists in more than one place, the value defined inside the function takes precedence over the argument, and the argument takes precedence over the element's value. I ran the --add-a-b-c() example from MDN. Even though the element had --a: 1; --b: 2; --c: 3, the result was 321 (the element's --a of 1, plus the argument --b of 20, plus the function's own --c of 300).
@function --add-a-b-c(--b, --c) {
--c: 300;
result: calc(var(--a) + var(--b) + var(--c));
}
div {
--a: 1;
--b: 2;
--c: 3;
z-index: --add-a-b-c(20, 30); /* 321 */
}
A function can also call another function. The called function can read the arguments and the values defined in the calling function. The --outer(1) example from MDN returned 3 in the test environment.
Wrap values that contain commas in {} when passing them
Arguments are separated by commas, so if you pass a value that contains commas as it is, it cannot be told apart from separate arguments. To pass it as a single argument, wrap it in {}.
@function --max-plus-x(--list <length>#, --x <length>) {
result: calc(max(var(--list)) + var(--x));
}
div {
width: --max-plus-x({1px, 7px, 2px}, 3px);
}
width became 10px (the largest of the three values, 7px, plus 3px).
Be careful about order when changing the returned value with @media
The function body can also contain @media. When there are multiple result declarations, the one written last is used, as in regular CSS.
@function --narrow-wide(--narrow, --wide) {
result: var(--wide);
@media (width < 700px) {
result: var(--narrow);
}
}
With padding: --narrow-wide(4px, 24px), the value was 24px when the viewport was 1000px wide and 4px when it was 600px wide.
When I put the @media block before result: var(--wide), the value stayed 24px even at 600px wide. CSS functions have no way to stop early and return a value, the way JavaScript's return does. MDN also says that a later result overrides an earlier one.
Using if() lets you write it in one line without worrying about order. result: if(media(width < 700px): var(--narrow); else: var(--wide)); also gave 24px at 1000px and 4px at 600px.
Example uses: spacing steps and font sizes that scale with the viewport
Here is a function that keeps spacing in multiples of 8px. The argument is <number>, and you pass the step number.
@function --space(--n <number>) returns <length> {
result: calc(var(--n) * 8px);
}
.card {
margin-top: --space(1); /* 8px */
margin-bottom: --space(3); /* 24px */
padding-left: --space(0.5); /* 4px */
}
You can also create a function that changes font size between a minimum and a maximum based on the viewport width. Here I made it return the minimum at 400px wide and the maximum at 1200px wide.
@function --fluid(--min <length>, --max <length>) returns <length> {
result: clamp(
var(--min),
calc(var(--min) + (var(--max) - var(--min)) * ((100vw - 400px) / 800px)),
var(--max)
);
}
h1 {
font-size: --fluid(16px, 32px);
}
In the test environment, font-size was as follows.
| Viewport width | Computed value of --fluid(16px, 32px) |
|---|---|
| 600px | 20px |
| 1000px | 28px |
| 1200px | 32px |
When a calculation fails, the earlier value does not come back
If a function's result is invalid, the declarations written before it do not come back. In the test environment, padding, margin, z-index, and background-color all fell back to their initial values (for padding, 0px). The following table shows the results when I wrote padding: 20px before each test.
| Value written | Computed padding |
|---|---|
--no-such-function(3), which is not defined |
0px |
A function declared returns <length> that returns result: red |
0px |
When I passed a length such as 1px to --space(), the earlier margin-top: 5px was not used, and the value was 0px.
If you misspell a function name or forget to load the file that defines it, the declaration does not return to the earlier value. Instead you can get results such as zero margin or a transparent color.
A function that calls itself also does not work. The CSS Custom Functions and Mixins specification treats a function that calls itself as circular. When I wrote --fact(3), a factorial function that calls itself until --n reaches 0, in z-index, the value became auto instead of the earlier z-index: 5.
On the other hand, if you put a space between the name and (, as in --double (10px), the declaration itself is ignored, and the earlier padding: 20px remained.
When the same function name is defined twice, the one written later was used. When @layer is involved, MDN says the definition in the layer with higher priority is used. In @layer, the layer declared last has the highest priority, so writing @layer low, high; gives high priority. In the test environment, I put a definition in high that returns 10px and then a definition in low, written later, that returns 30px. The result was 10px from high.
Preparing for browsers that do not support it
The compatibility data shows the following support.
| Browser | @function |
|---|---|
| Chrome and Edge (including Chrome for Android) | From 139 |
| Safari (including iOS) | From 27.3 |
| Firefox (including Firefox for Android) | Not supported |
According to MDN's guide on using custom functions, in browsers that do not support them, declarations that use custom functions are invalid and ignored. The guide's --supports() example uses this same mechanism to show a notice only in browsers that do not support custom functions. Likewise, if you write a normal value declaration before a declaration that uses a function, browsers without support ignore the function declaration, so the earlier value remains.
.card {
margin-top: 8px;
margin-top: --space(1);
}
However, as shown in the previous section, if the function's result is invalid in a browser that supports it, the value does not fall back to this 8px.
To switch a group of rules together, you can check with @supports at-rule(@function). In Chromium 158 in the test environment, the rules inside this block were applied.
@supports at-rule(@function) {
/* Rules that use @function */
}
You can check a function call with JavaScript's CSS.supports(), as in CSS.supports('padding', '--double(10px)'), but this does not tell you whether the function is defined. When I checked --no-such-function(3), which is not defined, the result was true.





