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

Adding vendor prefixes with LESS mixin

I'm getting a Syntax Error for this mix-in: .vendors(@statement){ @statement; -moz-@statement; -webkit-@statement; } Any way to do this, or do mixin variables have to be on the right side of a :?
Artur Sapek
  • 2,425
  • 6
  • 27
  • 29
3
votes
1 answer

Using a mixin nested inside another selector/class

I'm trying to make a Mixin output two different things based on context. Like so: .seticon(@r,@g,@b) { b { background-color: rgb(@r,@g,@b); } &.act b { .box-shadow(0 0 5px 1px rgba(@r,@g,@b,0.45)); } &.act.hover b…
2
votes
1 answer

default values for mixins parameters

I am using a mixin for font like this: #font { .trebuchet(@weight: normal, @size: 12px, @lineHeight: 20px, @style:normal) { font-family: "Trebuchet MS", arial, verdana, sans-serif; font-size: @size; font-weight: @weight; …
periklis
  • 10,102
  • 6
  • 60
  • 68
2
votes
2 answers

LESS mixin calling another mixin

I am using lessphp (latest from git) Is it possible to call a mixin from another mixin in the same "namespace"? Here is a short sample of code which produces no output (but should give a border to h1) #test { .mix() { border: 1px solid…
Matt
  • 5,478
  • 9
  • 56
  • 95
2
votes
0 answers

Convert less mixins to scss

Can anyone, please, convert less mixins to scss? This is mixins for ineresting practice for apple icons style borders. .border-squircle(@radius-x; @radius-y) { .loop(@radius-x; @radius-y; 359; 100% 0); } .loop(@radius-x; @radius-y; @counter;…
Dmitriy
  • 95
  • 6
2
votes
1 answer

Combine Less mixin output

I am looking for some sort of extend to media queries but I know extend is not the right thing here. I have a mixin that is supposed to create classes and media queries for each. Unfortunately my current mixin will create those one by one as you…
Dominik
  • 6,078
  • 8
  • 37
  • 61
2
votes
2 answers

How to Pass Parameters with Commas in Less

I have a mixin that takes the name of the shape and its coordinates. I am wondering how would I pass in my coordinates if the coordinates contains commas? .clip-path(@shape @coords) { -webkit-clip-path: @shape(@coords); -moz-clip-path:…
Jon
  • 8,205
  • 25
  • 87
  • 146
2
votes
1 answer

Dynamic classnames with parameters in Less mixin

I'm trying to create a Less mixin to generate media queries. The goal is to store my breakpoints in a variables.less file, and loop through them to create @media blocks. The mixin would then be used as: .mq-medium({ // rules }); and generate CSS…
Brett DeWoody
  • 59,771
  • 29
  • 135
  • 184
2
votes
1 answer

Why cannot we use ruleset within the same ruleset?

I am trying to use the same .formater() ruleset within itself. I am getting syntax error. Why cannot we use a ruleset within itself? .formater(@className;@rules){ .@{className}{ @rules(); } } .formater(mainContainer;{ …
vssadineni
  • 454
  • 4
  • 11
2
votes
1 answer

Converting Mixin using Guarded Namespace in Less to Sass

How do I convert the following mixin written in LESS to SASS? .box_gradient (@from, @to) when (iscolor(@from)) and (iscolor(@to)) { background-image: -webkit-gradient(linear, left top, left bottom, from(@from), to(@to)); /* Saf4+, Chrome */ …
Arkhitech
  • 475
  • 3
  • 11
2
votes
1 answer

Mixin guards in Less based on variables

I'm trying to create a button mixin in less that is highly customizable. For example, I want a client to be able to go in their .less file and write: .my-button { .btn(@bg: #FFF, @font: "", @color: #000, @size: 'large'...); ... } Now in my…
Zach
  • 4,555
  • 9
  • 31
  • 52
2
votes
1 answer

Less mixins, variable colours specificity

I have a design where I need to be able to change colours on a page depending on the input in the cms. To do this I'm adding one class to a containing div and I'll change the colours according to that surrounding class. There's going to be a set…
2
votes
1 answer

Inheritance of animation keyframes of element

I've got an element .shake and when it's :hover an animation starts. I need to inherit the shake element properties and its animation :hover effect. I've got .inherit-shake { .shake; } It doesn't work because .inherit-shake just inherits the…
Brian
  • 349
  • 1
  • 5
  • 16
2
votes
2 answers

Restrict mixin values and throw exception when invalid value is provided

I have the following Less mixin: .box-sizing(@value) { -webkit-box-sizing: @value; -moz-box-sizing: @value; box-sizing: @value; } and I would like to allow only the values 'border-box' and 'content-box' as parameter, otherwise the Less…
user1613797
  • 1,197
  • 3
  • 18
  • 33
2
votes
1 answer

CSS/LESS when > something AND < something

I'm trying to set a property when a value is less than AND greater than, to set a window for the property to apply. Example: .mixin (@colorVal) when (lightness(@colorVal) > 75%) {color:red;} .mixin (@colorVal) when (lightness(@colorVal) > 25%)…
Chase
  • 2,051
  • 2
  • 21
  • 26
1 2
3
10 11