乐闻世界logo
搜索文章和话题

Can a sass @mixin accept an undefined number of arguments?

1个答案

1

Yes, Sass's @mixin can accept a variable number of parameters. This feature is implemented using argument lists (arglist), which are special variables that capture all remaining parameters passed to the mixin. In Sass, you can define this type of parameter by prefixing the parameter name with three dots ....

Here is a simple example demonstrating how to use a variable argument list to create a mixin for generating multiple text shadows:

scss
@mixin text-shadow($shadows...) { text-shadow: $shadows; } @include text-shadow(1px 1px 0px rgba(0,0,0,0.5), 2px 2px 0px rgba(0,0,0,0.15));

In the above example, $shadows... is an argument list that receives all parameters passed to the @mixin text-shadow. When @include calls this mixin, all parameters are collected into the $shadows variable, and these parameters are then used in the text-shadow property to generate a composite text shadow effect.

This approach makes @mixin more flexible and powerful, suitable for various scenarios, especially when dealing with a variable number of style parameters.

2024年7月20日 15:49 回复

你的答案