I have some LESS for making margins based on the side being passed (top, right, bottom, left or all):
.margin(@px,@side) when (@side = top) {
(){ margin-top: ~"@{px}px"; }
}
.margin(@px,@side) when (@side = right) {
(){ margin-right: ~"@{px}px"; }
}
.margin(@px,@side) when (@side = bottom) {
(){ margin-bottom: ~"@{px}px"; }
}
.margin(@px,@side) when (@side = left) {
(){ margin-left: ~"@{px}px"; }
}
.margin(@px,@side) when (@side = all) {
(){ margin-top: ~"@{px}px";
margin-right: ~"@{px}px";
margin-bottom: ~"@{px}px";
margin-left: ~"@{px}px"; }
}
My question is that if I have an ID like this:
#testfeature {
.margin(10px,l);
.margin(10px,r);
}
Then LESS compiles it like this:
#testfeature {
margin-left:10px;
}
#testfeature {
margin-right:10px;
}
How do I get it to compile like this:
#testfeature {
margin-left:10px;
margin-right:10px;
}
Get rid of the unnecessary
() { ... }
's that are wrapping all of your mixin styles. They're causing the selectors to be interpreted oddly and separating them into different selections, rather than joining everything under one selection:You could probably also drop the
~"@{px}px"
in favor of simply@px
, also the last mixin should probably be:To get them to "group" you need to create a nested and grouped mixin. Below is a grouped mixin function, "streamlined" for margin setting.
position
thenvalue
(except as noted below; it can accept any order of position and values ofauto
orinherit
are allowed).0px
margin on that position.px
, but keeps explicitly set units as passed.LESS Mixin
Examples
.setMargins(right);
producesmargin-right: 0px;
.setMargins(right, 15);
producesmargin-right: 15px;
.setMargins(left, 10em);
producesmargin-left: 10em;
.setMargins(top, 12, right, 10);
produces:margin-top: 12px; margin-right: 10px;
.setMargins(25);
produces:margin: 25px;
.setMargins(auto);
produces:margin: auto;
So you use it in a selector:
LESS
CSS Output