Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LESS detached ruleset vs non-parametric mixin

Are there substantial differences between detached ruleset, e.g.

@detached-ruleset: {
  @margin: 1px;
  margin: @margin;
};

and non-parametric mixin? E.g.

.mixin() {
  @margin: 1px;
  margin: @margin;
}

Do they behave the same with nested operators?

The most obvious difference is syntactic (semicolon is mandatory for a ruleset), and the ruleset keeps its variables private, but that's all I could find. The manual doesn't go into details too much on that.

like image 312
Estus Flask Avatar asked May 28 '15 12:05

Estus Flask


1 Answers

The detached ruleset is a variable. For variables in Less the last declaration wins and variables are lazy loaded.

For reusable code you can easily extend the .mixin() by defining a second mixin() with the same name:

.mixin() {
  @margin: 1px;
  margin: @margin;
}

.mixin() {
  color: red;
}

When using a detached ruleset in the above you should repeat all propperties cause the second declaration overrides the first:

@detached-ruleset: {
  @margin: 1px;
  margin: @margin;
};

@detached-ruleset: {
  @margin: 1px;
  margin: @margin;
  color: red;
};

See also: https://stackoverflow.com/a/30384948/1596547

like image 74
Bass Jobsen Avatar answered Sep 22 '22 22:09

Bass Jobsen