2017-08-12 26 views
0

我有下面的代码示例:类似类特定的造型LESS

div.box { 
 
    width: 50px; 
 
    height: 50px; 
 
} 
 

 
div.box.red { background: red; } 
 
div.box.blue { background: blue; } 
 
div.box.green { background: green; }
<div class="red box"></div> 
 
<div class="blue box"></div> 
 
<div class="green box"></div>

这是非常多余的,我正在寻找一种更有效的方式来做到这一点,我我确定你可以在LESS中做到这一点,但我不记得如何。

我认为这是箭头,但是当我使用它们时它不能按预期工作。

如果在所有LESS符号上都有一些文档,请将其链接到我,以便我可以重新熟悉自己。

编辑:寻找这样的事情:

div.box { 
 
    width: 50px; 
 
    height: 50px; 
 

 
    > .red { background: red; } 
 
    > .blue { background: blue; } 
 
    > .green { background: green; } 
 
}
<div class="red box"></div> 
 
<div class="blue box"></div> 
 
<div class="green box"></div>

感谢=)

回答

2

您可以使用LESS & selector做到这一点:

div.box { 
    width: 50px; 
    height: 50px; 

    &.red { 
    background: red; 
    } 

    &.blue { 
    background: blue; 
    } 

    &.green { 
    background: green; 
    } 
} 
+1

一些礼包阅读有关连字符可以在http://blog.slaks.net/2013-09-29/less-阅读css-secrets-of-the-andpersand/ – Marwelln

+0

非常感谢! – Robinlemon

1

你根本不喜欢这样:

div.box { 
 
    width: 50px; 
 
    height: 50px; 
 
} 
 
.red { background: red; } 
 
.blue { background: blue; } 
 
.green { background: green; }
<div class="red box">R</div> 
 
<div class="blue box">B</div> 
 
<div class="green box">G</div>

+0

这不使用我正在寻找的LESS原则,但感谢您的努力=) – Robinlemon

+0

是的,因为对于类似的事情,您实际上不需要LESS ... – Johannes

+0

'我有以下示例代码';我正在消除更大规模的冗余,下次请阅读标题。 – Robinlemon