CSS custom functions and mixins
MDN Web Docs · نسخهٔ df622a2a1c1e · CC BY-SA 2.5 و مجوز جداگانهٔ ناشر برای نمونهکد
The CSS custom functions and mixins module allows developers to create reusable blocks of CSS code that can accept arguments, contain complex logic (defined using features such as CSS {{cssxref("if()")}} functions and {{cssxref("@media")}} at-rules), and return values based on that logic.
CSS custom functions are defined in {{cssxref("@function")}} at-rules and called using {{cssxref("<dashed-function>")}} syntax, which looks very similar to CSS custom property syntax except that it includes parentheses at the end containing arguments (for example, --my-function(30px, 3)). CSS custom functions can be called within any property value, and will return a value based on the arguments passed into the function and the logic inside it.
CSS mixins are defined in @mixin at-rules and applied using @apply at-rules nested inside rulesets. CSS mixins define a set of properties that can be reused inside multiple rulesets and customized with arguments and logic.
CSS custom functions and mixins can be assigned optional data types for their arguments and return values, to restrict the values passed into them and returned from them.
توجه: Currently, only CSS custom functions have browser support. CSS mixins are not currently supported in any browser.
Reference
At-rules and descriptors
- {{cssxref("@function")}}
- {{cssxref("@function#result", "result")}}
The CSS custom functions and mixins module also introduces the @mixin, @apply, @contents, and @env at-rules. Currently, no browsers support these features.
Data types and values
- {{cssxref("dashed-function")}}
Functions
- {{cssxref("type")}}
Interfaces
- {{domxref("CSSFunctionRule")}}
- {{domxref("CSSFunctionDeclarations")}}
- {{domxref("CSSFunctionDescriptors")}}
Guides
- Using CSS custom functions
- : This guide teaches you how to use CSS custom functions and presents some typical use cases.
Related concepts
- {{cssxref("abs()")}}
- {{cssxref("acos()")}}
- {{cssxref("asin()")}}
- {{cssxref("atan()")}}
- {{cssxref("atan2()")}}
- {{cssxref("calc()")}}
- {{cssxref("clamp()")}}
- {{cssxref("cos()")}}
- {{cssxref("exp()")}}
- {{cssxref("log()")}}
- {{cssxref("max()")}}
- {{cssxref("min()")}}
- {{cssxref("mod()")}}
- {{cssxref("pow()")}}
- {{cssxref("rem()")}}
- {{cssxref("round()")}}
- {{cssxref("sign()")}}
- {{cssxref("sin()")}}
- {{cssxref("sqrt()")}}
- {{cssxref("tan()")}}
Specifications
{{Specifications}}
See also
- {{cssxref("if()")}}
- {{cssxref("@media")}}