有没有办法在SASS中获得当前元素的设置宽度? [英] Is there a way to get the set width of the current element in SASS?

查看:37
本文介绍了有没有办法在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);
}

这篇关于有没有办法在SASS中获得当前元素的设置宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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