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
0
votes
1 answer

How to use bootstrap mixin with LESS?

I'm preparing the layout for page and have to use Bootstrap 3. I have a button with gradient, so I wanted to use Bootstrap mixin #gradient.horizontal: background: #gradient.horizontal(@color-start,@color-end,0%,100%); But I got parse error when…
Kania
  • 2,342
  • 4
  • 28
  • 36
0
votes
1 answer

SyntaxError when writing IE9 html[lang] selectors in LESS

I have a LESS mixin with rule-set to make IE9 hack selectors: .ie9(@rules) { html[lang='\ en'] & { @rules(); } } Hack source: http://browserhacks.com/#hack-e6b158b7e27bcb157326348efb5c0bb2 But after compiling I see: SyntaxError: expected ']' got…
Dmitry Yaremenko
  • 2,542
  • 20
  • 31
0
votes
2 answers

Inconsistent Output between LESS Compilers

I have written some LESS code that resizes text based on browser width. Multiple different elements and their parameters can be sent to the reusable mixin. All online LESS compilers output the desired result. But I am getting different output from…
Brandon
  • 3,572
  • 2
  • 12
  • 27
0
votes
1 answer

using less mixin inside of media query

is it possible to have different mixin depends on width? how to make it work? e.g. .fs (@weight: 300, @size: 2vw, @color: black) { @size2: @size/10.2; font-family: 'Lato', sans-serif; font-weight: @weight; font-size: unit(@size2,vw); color:…
luke9999
  • 151
  • 1
  • 3
  • 9
0
votes
0 answers

Bootstrap 3 translate3d mixin issue

I break my head about the next issue. I use Bootstrap 3 vendor-prefixes.less for my website to generate the CSS prefixes. I want to generate the following code: p { -moz-transform: translate3d(0, 50px, 0); -ms-transform: translate3d(0, 50px,…
0
votes
0 answers

Less - guardian mixins good practices

I created small Less @mixin for lightness value conditional. I was using Sass for my applications UI's and I'm realy confused how to create good conditionals in Less. Can anybody answer me that, is it good pice of code for Less conditionals? Less…
MagicJoseph
  • 59
  • 1
  • 4
0
votes
1 answer

Is there any possibility to build objects in Less?

At the moment I tried to build mixins for less, but it seems to be more complicated than expected. For example, I tried to build an if condition and my header looks like this: .ifClauses( @checkParam, @conditions:{ isnumber(@checkParam), …
myolli4
  • 179
  • 2
  • 11
0
votes
2 answers

Trigger hover state in a mixin

I've got a Less mixin with a default state and a hover state. Now I need to activate the hover state in the mixin when I'm hovering over the parent element. Less #icons () { .settingsIcon() { background: url("settings.png"); &:hover { …
Drkawashima
  • 8,837
  • 5
  • 41
  • 52
0
votes
1 answer

How can I interpolate a string/fraction mixin variable in LESS?

I'm trying to my grid system to LESS. I'm using fractions as strings (there's a reason) and need to interpolate them in the calc() expression. In Sass I can do this... @mixin move($fraction: '1/1') { position: relative; left: calc(99.999999% *…
corysimmons
  • 7,296
  • 4
  • 57
  • 65
0
votes
1 answer

How do you write a LESS rule to be used by a number of classes?

Say we need the a few different classes to use following: color:#ccf; background-color:#fcc; font-size:0.7em; How do we set this up in LESS? These classes can NOT be used with each other so setting them up in a comma separated list (.note,…
BillyNair
  • 277
  • 4
  • 20
0
votes
1 answer

How to use Mixin LESS in external style sheets

I have a question about the use of less mixin in different style sheets. I have 2 sheets of initial styles: reset.less and styleguide.less I wonder how can I define a mixin in styleguide.less title. And the reset just use…
Edmo Lima
  • 123
  • 10
0
votes
1 answer

Bootstrap 2 column issues when using mixins

I am using Bootstrap 2 mixins .makeRow() and .makeColumn() to make columns 4,3 and 5 which equals to a full row span 12 width. But it doesnt get applied properly. This is a screenshot of what I get. Markup:
0
votes
1 answer

Set "min-width" or "max-width" in a media query passing a parameter to a mixin

I would like to make dynamic MIN/MAX suffix in properties defined in a Less MediaQuery. I wrote this code but it does not compile: @screen-md: 800px; .MEDIAQUERY(@min-max, @size) { @media screen and (@{min-max}-width: @size) { …
Luca Detomi
  • 5,564
  • 7
  • 52
  • 77
0
votes
3 answers

How to declare same style for @media and descendant selector?

I need to define same style for elements under a media query and descendant by another class. Perfect solution in LESS could be the following [pseudo-code]: .foo { color:red; .example &, @media (min-width:800px) { color:blue; …
Luca Detomi
  • 5,564
  • 7
  • 52
  • 77
0
votes
1 answer

Convert a mixin that accepts only from & to keyframe selectors to accept multiple keyframe selectors

I have this Less mixin: .keyframes (@name, @fromRules, @toRules) { @-webkit-keyframes ~'@{name}' { from { @fromRules(); } to { @toRules(); } } @keyframes ~'@{name}' { from { @fromRules(); } to { @toRules(); } } } I call for…
quarky
  • 710
  • 2
  • 13
  • 36
1 2 3
10
11