有没有办法在SASS中获得当前元素的设置宽度?
问题描述:
在Tp中心绝对定位的元素上,我必须将当前宽度划分为一半,并将其设置为负的左边距值.但我不想做:
Tp center absolutely positioned elements I have to devide the current width in half and set that as the negative left margin value. But I don't want to have to do:
#elem {
width: 75px;
margin-left: -(75/2)px;
}
从现在开始,如果我更新宽度,则必须在2个地方进行更新.而是可以在SASS中以某种方式引用当前选择器的设置宽度吗?
Since now if I update the width I have to update it in 2 places. Instead is it possible to reference the set width for the current selector in some way in SASS?
伪代码:
#elem {
width: 75px;
margin-left: -(this.width/2)px;
}
答
我将建议使用与@GEspinha相同的方法,但是如果要在许多不同的元素上进行此操作,则可能值得使用mixin:
I was going to suggest the same approach as @GEspinha, but if you are going to be doing this on many different elements it might be worthwhile to use a mixin:
@mixin absolute-width($width) {
margin-left: -($width/2);
width: $width;
}
#elem {
@include absolute-width(75px);
}
#elem2 {
@include absolute-width(50px);
}