Questions tagged [less-mixins]

In Less, any selector with a group of CSS properties can be referred to as a mixin. Using mixins allow users to inherit/embed properties of one selector into another by just calling it within the selector which needs to inherit the properties. Use this tag for any questions related to Less mixins and their features. For other types of mixins, please use the generic mixins tag.

Any selector with a group of CSS properties can be referred to as a mixin. Using mixins allow users to inherit/embed properties of one selector into another by just calling it within the selector which needs to inherit the properties.

.default-props{ /* a simple mixin */
  font-size: 16px;
  letter-spacing: 1em;
  color: black;
  padding: 4px;
}

div, p{
  .default-props; /* mixin call will assign all default properties to div and p tags */
}

When the mixins are defined, the parentheses are optional. But when parentheses are used, the base mixin itself is not output in the compiled CSS.

Mixins can also accept input parameters and behave like functions. The parameters that are passed can be used to assign values to properties (or) in guards to help in decision making etc.

.padding-gen(@gutter-size; @direction){
  & when (@direction = all){ /* guards for decision making */
    padding: @gutter-size; /* value assignment */
  }
  & when not (@direction = all){
    padding-@{direction}: @gutter-size;
  }
}

/* mixin usage */

div#demo{
  .padding-gen(4px; all);
}
p#demo2{
  .padding-gen(2px; left);
}

Reference Links:


Collection of Common & Useful Mixins:

151 questions
26
votes
6 answers

Less CSS: Mixins with Variable Number of Arguments

LESS allows parametric mixins, such as: .transition(@property, @duration){ transition: @property @duration; -moz-transition: @property @duration; /* Firefox 4 */ -webkit-transition: @property @duration; /* Safari and Chrome…
Tuanderful
  • 1,337
  • 1
  • 10
  • 13
18
votes
4 answers

Multiple properties are getting treated as separate arguments in mixins

I'm trying to write a mixin, but I can't seem to get the arguments working the way I want: multiple properties are getting treated each as a separate argument. Current Code .transition(@property: all, @time: 1s, @timing: ease-in-out) { …
Oscar Broman
  • 1,109
  • 8
  • 19
18
votes
4 answers

Can I define a LESS mixin to generate a transition-property with a variable number of parameters?

I'm introducing LESS to a large web app project to simplify my CSS. I've got a few CSS rules which apply transitions to a varying number of properties, for example: .movable { transition-property: top, left; transition-duration: 0.2s; …
Mark Whitaker
  • 8,465
  • 8
  • 44
  • 68
9
votes
5 answers

LESSCSS - use calculation and return value

H i, Hoping you can help. Is there a way for LESS to return just a value - feel like I'm missing something very obvious Say I have: @unit:em; @basevalue:1; Can I use something to give me a shorthand return for - .someClass { padding:…
Rob Sedgwick
  • 5,216
  • 4
  • 20
  • 36
8
votes
3 answers

Conditionally setting one variable's value based on another

I've got a Less variable called @side. What I want is to set the variable @sideOpposite depending on the value of the @side variable. It can take only two values: "left" or "right". In other words I need a Less equivalent of the JS code: var side =…
zorza
  • 2,814
  • 3
  • 27
  • 41
5
votes
1 answer

How to throw an error in the LESS compiler

Question Is there any way to (programmatically) throw an error in the LESS compiler? Why? I have been fiddling around with mixin guards today, because I wanted to generate my CSS margin based upon element size and element count. I thought it would…
Smamatti
  • 3,901
  • 3
  • 32
  • 43
5
votes
1 answer

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…
Estus Flask
  • 206,104
  • 70
  • 425
  • 565
5
votes
1 answer

Generation CSS group via less

Is it able to create such a mixin which generate CSS group? I will explain what I mean below: .fancymixin(@max, @prefix) { //content what I don't know how to code } .fancymixin(10, x); It generates something like: .x10, .x9, .x8, .x7, .x6,…
kspacja
  • 4,648
  • 11
  • 38
  • 41
5
votes
1 answer

Enums/Documentation with LESS Mixins

I am creating a set of LESS mixins with Visual Studio/Web Essentials. Is it possible to write XML-type documentation for LESS mixins? Or perhaps have an enum to limit the parameters that are input? For instance, with this…
William
  • 3,335
  • 9
  • 42
  • 74
5
votes
1 answer

How can I call a mixin by reference in LESS?

The logical way would be: .mymixin() { sample_key: samplevalue; } @avar: mymixin; .@{avar}(); but I get a parse error. Is there a way to do it?
jbaylina
  • 4,408
  • 1
  • 30
  • 39
5
votes
2 answers

How to simplify this LESS CSS Box-shadow mixin? (multiple shadows with "directions")

How to reduce this code (probably with a loop ?), to have a "function" which takes direction and number? @dir = the wanted "direction" @number = how many times I need a shadow (here 10 times) @color = color of the shadow Example (working, but not…
flks
  • 610
  • 10
  • 28
4
votes
1 answer

LESS CSS - Setting variable within mixin

I've recently starting using LESS CSS - it's awesome (I recommend you check it out if you haven't yet). I'm working on a project, where I would like to do the following (It's not proper syntax, it's only to try and explain my…
4
votes
1 answer

Dynamic LESS mixin

I'm trying to write a dynamic mixin with LESS to generate the CSS automatically based on an ID. Is it possible with LESS? // Variables @body-margin: 50px; @body-margin-tablet: 30px; @body-margin-mobile: 20px; @body-padding:…
user3631047
  • 3,256
  • 5
  • 23
  • 34
4
votes
2 answers

Less lighten, darken, and spin only work sometimes

The issue I'm a newbie to LESS, and we just learned some basics about parametric mixins in my Intermediate CSS & Preprocessors course. I'm working on my project website, and loving what LESS can do, but it isn't always working the I want it to. For…
4
votes
2 answers

Use mixin argument to create class name in LESS

I'm trying to create a simple mixin in LESS for different colors I'll use for a website. What i want is use mixin argument as a part of class name as well. @green: #5FBEAA; // my color variable .text-color(@color) { .text-{@color} { …
Deepak Yadav
  • 6,804
  • 5
  • 31
  • 49
1
2 3
10 11