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

Using undefined number of arguments in mixins

I currently have -webkit specific attributes in my Less CSS sheet, I am trying to update them with mixins to add -moz attributes, like this: .transition(@1) { -webkit-transition: @1; -moz-transition: @1; } div { .transition(all…
1
vote
1 answer

Less custom function for :first-letter uppercase

I'm looking for a way to create a custom mixin less function that would act as follows: .my-class { ... .FirstLetterUppercase(); } And that would transpile to: .my-class { ... } .my-class:first-letter { text-transform: uppercase; } Is that…
Alexandre Daubricourt
  • 3,323
  • 1
  • 34
  • 33
1
vote
0 answers

How to import global less mixin so it can be used in any components?

I was using webpack extract-text-plugin with the following code, and got the error: ModuleBuildError: Module build failed: .bg is undefined. Is there a way to make such way of importing work? my index.jsx file is as below: import…
Blake
  • 7,367
  • 19
  • 54
  • 80
1
vote
2 answers

LESS CSS Functions: Utilize Different Values

With LESS, I'm trying to come up with a snappy way to calculate margins, paddings, and the like based upon defining an amount. Currently, I'm specifying this: .emSpacing(@string) { @emSpacing: unit(@string / 16) + 0em; } and call…
Moring
  • 13
  • 3
1
vote
1 answer

Bootstrap Grid Mixin Overriding Previous Declarations

I'm using Bootstrap 3.3.7s Less mixins for their grid system, found here. My basic concept is to increase gutter size as my screen size increases. Here is an example of my html:
Joe W.
  • 167
  • 8
1
vote
1 answer

Making this LESS Mixin more efficient and less redundant?

so for a website project I had to create a LESS Mixin which I had never done before. I have the feeling that it is not as efficient as it could be and may be a little redundant. I looked through it but couldnt think of a better way to do it. Maybe…
SVARTBERG
  • 435
  • 1
  • 7
  • 16
1
vote
1 answer

are variable mixin names in LESS possible?

What I want to do is to create a mixin that takes arguments and uses one or more of the arguments as names for other mixins to be included. as I'm not sure about the proper terms, I'll try to explain through example: @gradients{ light:#fafafa;…
jpeltoniemi
  • 5,320
  • 7
  • 24
  • 34
1
vote
1 answer

Less Mixin iteration infinite (circular loop)

How do I iterate a mixin-loop without stop? Let's say I have an array with four colors. When it iterate to the last color, it begins with the first color and so on and fourth. My code: @array: blue, red, green, yellow; .color-mix(@i) when (@i > 0)…
maverick
  • 800
  • 2
  • 13
  • 30
1
vote
1 answer

Are mixin parenthesis optional when nested?

Consider the following: .flashing { .flashKeyFrames(fade-in;{ 0%, 100% {opacity: 1;} 50% {opacity: 0;} }); .flashing(fade-in linear infinite alternate; 1s) } .flashKeyFrames(@name; @arguments) { …
1
vote
1 answer

Accessing Less variables names inside a list

OK, let's say that I have a Less file like so: // Turquoise @turquoise: #1ABC9C; @turquoise-2: #16A085; // Greens @green: #2ECC71; @green-2: #27AE60; // Blues @blue: #3498DB; @blue-2: #2980B9; // Navy Blues @navy: #34495E; @navy-2: #2C3E50; //…
BlueEyesWhiteDragon
  • 427
  • 1
  • 6
  • 20
1
vote
1 answer

Calculations on Less variable variables

My grid has different gutters for different breakpoints and I would have liked to generate my .row classes dynamically by passing to it predefined variables such as: @grid-gutter-xs: 10px; @grid-gutter-sm: 20px; ... Then I have some mixins within…
lavirius
  • 467
  • 1
  • 5
  • 15
1
vote
1 answer

Less mixin, extract colors from array and on hover, add darken to that array

Is it possible to darken an array with colors? Like this: @array: @color1 @color2 @color3 @color4 .color-mix(@i) when (@i > 0) { .myClass { li:nth-child(@{i}) { .background-color(extract(@array, @i)); &:hover { …
maverick
  • 800
  • 2
  • 13
  • 30
1
vote
1 answer

How do I change the unit of multiple numbers in a Less mixin?

I am trying to create a mixin whereby I can change the unit from px to rem. For now, I have the following code: @emSize : 16px; @pxr : 1 / unit(@emSize, rem); .padding(@padding) {padding: @padding * @pxr;} .test {.padding(10px);} This works…
Moshe
  • 551
  • 3
  • 7
  • 17
1
vote
0 answers

Add unit after each space separated value in LESS mixin

I have a mixin in LESS that has two variables as perimeters and that has to output a PX and a REM value. This is the mixin: .rem (@attr, @val) { @{attr}: @val * 1px; @{attr}: (@val * 1rem) / @baseFontSize; } With the @baseFontSize: 10; If i write…
1
vote
1 answer

Reverse states on hover with Less

I have the following LESS code: .favourite-action-link { &:after { color:@grey; content: '\e836'; } &.is-favourite:after { color:@red; content: '\e811'; } &:hover { &:after { …
Chris O'Kelly
  • 1,863
  • 2
  • 18
  • 35