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
1
vote
1 answer

Conditional parameter validation in a LESS mixin function

How can I translate the following conditional to LESS? .mixin(@width) { width: (!@width ? auto : @width); } Results should be like: [no value is passed] LESS: .mixin(); CSS: width: auto; [value is passed] LESS: .mixin(200px); CSS: width:…
pilau
  • 6,635
  • 4
  • 56
  • 69
1
vote
1 answer

Overwrite only the essential parameters in Mixin

Simple question. I just started looking at less for CSS building, I stumbled upon this dilemma: .fontcase (@family:'helvetica',@size:1em,@fontweight:300,@color:#ffffff) { font-family: @family Arial sans-serif; font-size:@size; …
Mr A
  • 1,345
  • 4
  • 22
  • 51
0
votes
1 answer

LESS mixin with multiple variables

I'm trying to make a mixin with multiple variables in LESS, but for some reason it's not working. I have this LESS: .rgbabg(@r: 0, @g: 0, @b: 0, @a: .5) { @val: rgba(@r, @g, @b, @a); background: @val; } I call it like this: .rgbabg(255, 0, 0,…
Gregory Bolkenstijn
  • 10,003
  • 7
  • 36
  • 38
0
votes
0 answers

Escaped calc inside Recursive Mixin throws error

Currently I use less version 2.5.3 and to have the default css calc behaviour I have to escape it like this: width: calc(100vw - 30px); becomes this: width: calc(~"100vw - 30px"); This is working, but not when I use Recursive Mixins. I use this…
skarpeta
  • 105
  • 6
0
votes
2 answers

How do I change only one argument in Less mixins?

I want to use a Polymorphism mixin in Less, I have set default values for my parameters, regarding that both the Polymorphism mixins have different number of parameters, when using the mixin for different elements, I wonder if I can only assign one…
Zeinab
  • 66
  • 9
0
votes
1 answer

How to write a loop function or mixin with variables in less.js for DRY

Is there any efficient way to write this code with less.js: I've got already 3 variables colors : @vert, @violet and @orange li { &:nth-child(1) { background: @vert; &:hover, &.open { >.dropdown-menu li { …
echayotte
  • 1
  • 3
0
votes
1 answer

LESS mixin to automatically add CSS custom property fallback for IE

We're introducing a dark theme for our site that can be turned on and off on the fly, so we're using CSS custom properties. The problem is that we still have to support IE 11. IE just isn't getting the dark theme, it'll stay on the light theme,…
vaindil
  • 7,536
  • 21
  • 68
  • 127
0
votes
0 answers

.less mixin guard won't accept @media in expression

I'm trying to create a mixin guard, as seen on http://lesscss.org/features/ , where they do mixin(@a) when (@media = mobile) { ... } However, when I do mixin() when (@media = mobile) { ... } or mixin(@a) when (@media = mobile) { ... }, less is…
mwarrior
  • 499
  • 5
  • 17
0
votes
1 answer

Generate CSS classes from a list of values in LESS

I have a LESS loop that generates different CSS (incremental) classes extracting color values from a list. My current LESS code is the following: .generate-detached(#f00, #0f0, #00f); .generate-detached(@colors...) { .generate-detached-loop(1,…
Luca Detomi
  • 5,564
  • 7
  • 52
  • 77
0
votes
1 answer

LESS Mixins aren't available in other imported LESS files

I have this code: @import "vars.less"; // Desktop/Laptop Section @import "mixins-d-1.less"; @import "positioning.less"; @import "containers-d-1.less"; Inside containers-d-1.less I reference a mixin from mixins-d-1.less. However, I get an error…
Reverend Bubbles
  • 1,363
  • 5
  • 15
  • 29
0
votes
1 answer

Mixin parameter as a part of the class name

Hi is it possible to use mixin parameter as a part of the class name used in this mixin or do it in some other way? .thumbs-mixin(@length) { &.thumbs-@length { .slick-track { width: 180px * @length - 20px; …
gidzior
  • 1,807
  • 3
  • 25
  • 38
0
votes
1 answer

How I properly use bootstrap mixin for box-shadow and transition?

I am trying to use boostrap mixin for box-shadow and transition as below in my less file: .service-box { .box-shadow(3px 3px 1px rgba(195, 195, 195, 0.4)); .transition(0.4s); } Lets look at its compiled css: .service-box { -webkit-box-shadow:…
user3733831
  • 2,886
  • 9
  • 36
  • 68
0
votes
0 answers

css - Less extend with pseudo-class and pseudo-elements

I have list element with link inside, used font-awesome for Less. How to keep code in first pseudo-element and repeat in second, third. Is there any shorthand notation? My goal is not to repeat the same properties for different elements. If…
0
votes
1 answer

Is it possible to create a CSS/SASS mixin that detects current URL?

I just found out CSS's @media is a mixin that takes different parameters like width/height/orientation etc. to determine what device is used and then the mixin applies some basic CSS styles. My question is whether it is possible to create a very…
BB-8
  • 565
  • 2
  • 9
  • 18
0
votes
2 answers

Mark a Less mixin as optional

Less lets you mark an import as optional like this: @import (optional) "foo.less"; I have a Less file that I'm importing optionally which contains a mixin that I use in the parent file. How can I mark the usage of a mixin as optional, so rendering…
Nathan Arthur
  • 8,287
  • 7
  • 55
  • 80