I'm looking for some kind of if-statement to control the background-color
of different div
elements.
I have tried the below, but it doesn't compile
@debug: true;
header {
background-color: (yellow) when (@debug = true);
#title {
background-color: (orange) when (@debug = true);
}
}
article {
background-color: (red) when (@debug = true);
}
LESS has guard expressions for mixins, not individual attributes.
So you'd create a mixin like this:
And turn it on or off by calling
.debug(true);
or.debug(false)
(or not calling it at all).I stumbled over the same question and I've found a solution.
First make sure you upgrade to LESS 1.6 at least. You can use
npm
for that case.Now you can use the following mixin:
Since LESS 1.6 you are able to pass PropertyNames to Mixins as well. So for example you could just use:
If @include-lineheight resolves to true LESS will print the
line-height: 35px
and it will skip the mixin if @include-lineheight is not true.There is a way to use guards for individual (or multiple) attributes.
...and with Less 1.7; compiles to:
I wrote a mixin for some syntactic sugar ;)
Maybe someone likes this way of writing if-then-else better than using guards
https://github.com/pixelass/more-or-less/blob/master/less/fn/_if.less
Usage:
using on example from above