Favorites

@function CSS At-Rule

@function
CSS At-RuleThis at-rule is supported in some modern browsers, but not all.
Updated 21st November 2026

The @function at-rule defines a custom CSS function with parameters and a result, which can be called by name in property values, such as --rem(18).

Looks like you're using an ad blocker, and that's fine! Ads help keep CSS Portal's generators and references free, so if the site's useful to you, visit our Support page to see other ways to help.

Description

The @function at-rule defines a custom CSS function: a named, reusable calculation that takes arguments and returns a value. Custom functions work much like custom properties, but they accept parameters, so a piece of logic that would otherwise be copied into many declarations, such as converting pixels to rem, building a fluid size or picking a color variant, can be written once and called by name wherever a value is needed.

A function's name must start with two dashes, like a custom property, for example --fluid-size(). Its parameters are listed in parentheses; each one is a custom property name that can optionally have a type, such as <length>, and a default value after a colon. Inside the rule, the parameters are read with var(), local custom properties can be declared for intermediate steps, and the result descriptor sets the value the function returns. An optional returns clause declares the type of the result, so the browser can check it.

A custom function is called just like a built-in one, for example padding: --spacing(2);. The call is resolved at computed-value time for each element, so the function can use the element's own custom properties and inherited values. The body can also contain conditional rules such as @media, @supports and @container, and conditional values with if(), so a function can return different results in different contexts.

Custom functions are supported in Chromium-based browsers and Safari, but not yet in Firefox. Where they are not supported, the declaration that calls the function is invalid, so provide a plain fallback declaration before it.

Interactive Demo

Custom functions
Demo Code
HTML Code
<div class="demo-element">Custom functions</div>
CSS Code
@function --demo-shade(--colour <color>, --amount <percentage>: 25%) returns <color> {
	result: color-mix(in oklch, var(--colour), black var(--amount));
}
@function --demo-space(--step <number>: 1) returns <length> {
	result: calc(var(--step) * 8px);
}
@function --demo-rem(--px <number>) returns <length> {
	result: calc(var(--px) / 16 * 1rem);
}
.demo-element {
	width: 220px;
	padding: 10px;
	background: #4361ee;
	color: #fff;
	font-size: 15px;
	font-weight: bold;
	border-radius: 6px;
	background: --demo-shade(#4361ee);
}

Syntax

@function <function-name>( <function-parameter>#? ) [ returns <css-type> ]? {
  <declaration-rule-list>
}
where
<function-name> = <dashed-function>
<function-parameter> = <custom-property-name> <css-type>? [ : <default-value> ]?

Values

  • <function-name>The name of the function, starting with two dashes and followed directly by an opening parenthesis, for example --double(.
  • <function-parameter>Optional, comma-separated. Each parameter is a custom property name such as --value, optionally followed by a type (for example <length> or type(<number> | <percentage>)) and a default value after a colon.
  • returns <css-type>Optional. The type of the value the function returns, for example returns <length>. The result is checked against it.
  • resultA descriptor inside the body that sets the function's return value. It can use the parameters with var() and any other CSS functions.

Example

/* Convert a pixel value to rem */
@function --rem(--px ) returns {
result: calc(var(--px) / 16 * 1rem);
}

/* Spacing scale with a default step */
@function --spacing(--step : 1) {
result: calc(var(--step) * 0.5rem);
}

/* Return a different value on small screens */
@function --gutter() {
result: 2rem;
@media (width < 600px) {
result: 1rem;
}
}

.card {
padding: 16px; /* fallback */
padding: --spacing(3);
font-size: --rem(18);
margin-inline: --gutter();
}

Browser Support

The following information will show you the current browser support for the CSS at-rule @function. Hover over a browser icon to see the version that first introduced support for this CSS at-rule.

This at-rule is supported in some modern browsers, but not all.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported

Looks like you're using an ad blocker, and that's fine! Ads help keep CSS Portal's generators and references free, so if the site's useful to you, visit our Support page to see other ways to help.