changing css class variables

冷暖自知 提交于 2019-12-02 07:43:16
Bass Jobsen

You should first write your Less file, for instance as follows:

.marginTop(@topMargin) {
margin-top: @topMargin;
}

p.marginTop-10 {
.marginTop(10px); 
}

p.marginTop-15 {
.marginTop(15px); 
}

The preceding Less code will compile into the following CSS code:

p.marginTop-10 {
  margin-top: 10px;
}
p.marginTop-15 {
  margin-top: 15px;
}

After that you can use in your HTML:

<p class="marginTop-10">css help</p>
<p class="marginTop-15">css help</p>

Notice that you can also compile a list of classes dynamically, see also: LESS loops used to generate column classes in twitter - How do they work?

Doing that you could write the the following Less code:

@margins: 10 20 50;

.marginTop(@i: 1) when (@i <= length(@margins)) {
.marginTop(@i + 1);
@margin-top: extract(@margins,@i);
.marginTop-@{margin-top} {
    margin-top:unit(@margin-top,px);
    }
}

.marginTop();

outputs:

.marginTop-50 {
  margin-top: 50px;
}
.marginTop-20 {
  margin-top: 20px;
}
.marginTop-10 {
  margin-top: 10px;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!