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
2
votes
3 answers

How do I convert a variable containing a percentage to a number in LESS, but leave plain numbers alone?

In a LESS mixin, I have an alpha value that I'd like to convert to a plain number (0.xx) even if it is given as a percentage (xx%). I've found the percentage() function to convert a number to a percentage, but percentage(xx%) returns "xx00%" and…
SuperFLEB
  • 204
  • 2
  • 11
2
votes
2 answers

Less CSS - cut down repetition

I have the following code in Less: I'm pretty sure this can be abstracted out further with some kind of mixin, but I've been scratching my head for a while now. I would like to be able to pass in a variable such as @xs or @xs-gutter and have the…
dotnetnoob
  • 10,783
  • 20
  • 57
  • 103
2
votes
1 answer

Less loop with hash is it possible?

Before start shooting me, I have to say that I am totally new on less, so please don't shoot !! :) What I'd like to do is to write some classes for aligning my text based on my screen size, and instead of writing my code by hand I'd like to use a…
KodeFor.Me
  • 13,069
  • 27
  • 98
  • 166
2
votes
2 answers

Bootstrap 3 translate mixin

How to add values on the translate mixin? I want to have these values on the Y coordinate: -webkit-transform: translateY(-50%); -ms-transform: translateY(-50%); -o-transform: translateY(-50%); transform: translateY(-50%); The bootstrap…
Augusto Triste
  • 461
  • 7
  • 8
2
votes
1 answer

Overriding mixins in LESS

when defining a mixin multiple times in LESS, and later calling that mixin as follows .background-color() { background: red; } .background-color() { background: yellow; } body { .background-color; } the result will be a combined…
Anas Nakawa
  • 1,977
  • 1
  • 23
  • 42
2
votes
1 answer

How to re-use a mixin whose selector is formed using concatenation

In Less, I can write: .outer { .inner { color: red; } } .test { .outer .inner; } But when I write: .outer { &-inner { color: red; } } .test { .outer-inner; } When I remove the .test,…
2
votes
2 answers

How to pass multiple class names to a mixin in LESS

I'm making a view that displays transactions on an account, and I want to colour-code the transaction type/state. I also want to show a legend explaining the colour codes. I want an end result that's structured like this: HTML
millimoose
  • 39,073
  • 9
  • 82
  • 134
2
votes
1 answer

Less mixin and variables

I have the following mixin: .iconFont(@color: @green, @font-size: 18px){ color: @color; font-size: @font-size; } If I want only to change the second variable value, I need to write the first variable default value? h1{ .iconFont(@green,…
marcelo2605
  • 2,734
  • 4
  • 29
  • 55
2
votes
1 answer

Loop through mixin parameter in Less

I am trying to create a Less mixin for vendor properties that allows somebody to specify what CSS property they want to use, the value of the property, and what vendor(s) they want it for (Opera, Mozilla, Firefox, Webkit, IE, none). I originally…
Keenan Payne
  • 796
  • 2
  • 15
  • 32
2
votes
1 answer

Less custom function/mixin

For example I have next less code form.someForm { .form-group { > div { @media @wideMobile { width: calc(~"100%-144px"); } } > label { …
user1325696
  • 616
  • 1
  • 8
  • 16
2
votes
1 answer

Less CSS variable encapsulation and reusable mixins

Can anyone offer any solutions for combining encapsulation and mixin reuse for Less/CSS? I'm trying to keep my variables encapsulated by namespace, but I haven't figured out how to reuse mixins for it. Example: #signup-module { @button-width: …
xff0000
  • 53
  • 5
2
votes
1 answer

LESS CSS how to modify parent property in mixin

I'm looking for a less syntax to do something like that: .button-default{ background-color: rgb(100,200,250); :hover{ .button-hover-effect-mixin(); } } .button-warning{ background-color: rgb(250,100,0); :hover{ …
Andreas Neumann
  • 364
  • 1
  • 3
  • 12
2
votes
1 answer

Conditional mixin based on parameter existence

Any suggestion how to create a conditional mixin based on parameter existence? For example I need to to verify that all the parameters are passed in order to perform something or not, for example: .margin…
Dee
  • 3,185
  • 10
  • 35
  • 39
2
votes
2 answers

Send properties as argument for mixin

I'd like to break out all of my Media-queries and pass the CSS properties as arguments instead. .bp1(@css){ @media (max-width: 959px){ @css } } .bp1(width: 186px;); Unfortunately, this wont work and makes the Less fail :(
Anton Gildebrand
  • 3,641
  • 12
  • 50
  • 86
2
votes
1 answer

How to access specific class attribute in less mixin?

I have .aClass .aClass { width: 20px; height: 20px; } In .anotherClass I would like to calculate a width value based on the value of the width attribute in .aClass. .anotherClass { width: .aClass.width } The above example does not work. I…
Björn
  • 12,587
  • 12
  • 51
  • 70