在Sass中为CSS属性使用变量 [英] Using variables for CSS properties in Sass

查看:1603
本文介绍了在Sass中为CSS属性使用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个@mixin,其中包含一些数学运算来计算元素的百分比宽度,但是由于它非常有用,因此我也希望对其他属性(例如边距和填充)也使用相同的函数.

I am writing a @mixin with some math in it that calculates the percentage width of an element, but since it is very useful I would like to use the same function for other properties too, like margins and paddings.

是否可以将属性名称作为参数传递给mixin?

Is there a way to pass the property name as an argument to a mixin?

@mixin w_fluid($property_name, $w_element,$w_parent:16) {
    $property_name: percentage(($w_element/$w_parent));
}

推荐答案

您需要使用 对变量进行插值 (例如#{$var}),以便Sass将其视为CSS属性.没有它,您将仅执行变量分配.

You need to use interpolation (eg. #{$var}) on your variable in order for Sass to treat it as a CSS property. Without it, you're just performing variable assignment.

@mixin w_fluid($property_name, $w_element, $w_parent:16) {
    #{$property_name}: percentage(($w_element / $w_parent));
}

这篇关于在Sass中为CSS属性使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆