Ideally, I would like to do this :
@w: 4px;
@media (max-width:900px) {
@r: 3px;
}
@media (min-width:900px) {
@r: 5px;
}
.myclass {
border-radius: @w + @r;
}
This doesn't compile because @r
isn't defined in the scope where I define .myclass
. The obvious solutions are either to define .myclass
inside the @media
blocs or to copy the @media
queries inside the definition of .myclass
.
But as soon as you use @r
in many classes, both solutions are messy and involve many duplications.
Is there a clean dry solution ?
I found a solution, based on
@import
, which lets me keep dry.I make two files :
classes.less
mediawidth.less
Generated CSS :
This way I don't have to repeat the many classes definition but only the import.
I accepted Martin's answer, which is much cleaner in the most common case when there are only a few numbers of variables to pass. My solution might be dryer and cleaner as soon as you have more variables and when your classes are defined in many files.
Just use a mixin, that calculates the property values according to the mediaquery. It is unnecessary to do this via import.
LESS:
CSS: