@function CSS At-Rule
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).
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
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
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.
Desktop





Tablets & Mobile





