Web platform features explorer

@function

The @function CSS at-rule defines a custom function that takes CSS values or custom properties as arguments, and returns a CSS value. It can be based on conditional logic such as by using the @media at-rule.

Status

Limited availability

MDN documentation

Browser support

Usage (according to Chrome Platform Status)

~0.001% of page loads. More data at chromestatus.com.

Developer votes

Do you need this feature?
Let us know by leaving a 👍 reaction on the feedback issue. Don't forget to also leave a comment explaining your specific use case.

Developer use cases

  • I'd like an easy way to make a color partially transparent, like `background-color: translucent(var(--m3-scheme-on-background) / 0.08)`. I could literally define a `--translucent` function with `@function`. I'd like an easy way to make a color partially transparent, like `background-color: translucent(var(--m3-scheme-on-background) / 0.08)`. I could literally define a `--translucent` function with `@function`. [see original comment]
  • With CSS functions, I could simply declare a single parametrized function once (instead of 30+ times the exact same function for each property), and reuse it whereever I need it. With CSS functions, I could simply declare a single parametrized function once (instead of 30+ times the exact same function for each property), and reuse it whereever I need it. [see original comment]
  • I want to use @function to encapsulate complex styling logic in a reusable, DRY way. This can significantly reduce CSS bundle sizes, reduce maintenance requirements, and promote uniformity. It is compatible with any framework by construction, and usable directly in any form of markup (TSX, RSX, etc). Even simple react apps that rely heavily on inline styles (which normally lack support for features like @media), can easily bake in complex, comprehensive styles without onerous build concerns. This would be the single biggest impact CSS feature in my decade as a developer. I want to use @function to encapsulate complex styling logic in a reusable, DRY way. This can significantly reduce CSS bundle sizes, reduce maintenance requirements, and promote uniformity. It is compatible with any framework by construction, and usable directly in any form of markup (TSX, RSX, etc). Even simple react apps that rely heavily on inline styles (which normally lack support for features like @media), can easily bake in complex, comprehensive styles without onerous build concerns. This would be the single biggest impact CSS feature in my decade as a developer. [see original comment]
  • I'd like to create a function which takes a min and max number and a min and max viewport size (or default values) and returns a clamp() function with a smooth transition between the min and max using vw units. I'd like to create a function which takes a min and max number and a min and max viewport size (or default values) and returns a clamp() function with a smooth transition between the min and max using vw units. [see original comment]