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

Add argument into @keyframes property Less

I have a property @keyframes, I compiled with autoprefixer to add the needed prefixes. What I would like to do, is to add an argument to the animation name (or wherever is possible) to change a value of properties into the keyframes key. This is…
jeremybarbet
  • 890
  • 1
  • 15
  • 28
4
votes
3 answers

Name clash between Less mixin and CSS selector

I have this simplified Less script .placeholder(@color: #333333) { &::-webkit-input-placeholder { color: @color; } } input { .placeholder(); } .placeholder { margin-top: 20px; } The output when I run this through my local compiler or…
opex
  • 234
  • 2
  • 6
4
votes
1 answer

How do I extend a class/mixin which has dynamically formed selector

How do I extend a Less class which is dynamically formed using & combinator? Less which generates expected output: .hello-world { color: red; } .foo { &:extend(.hello-world); font-size: 20px; } Expected CSS output: .hello-world, .foo { …
Andrew
  • 5,525
  • 5
  • 36
  • 40
4
votes
4 answers

How to pass a property name as an argument to a mixin in less

I want to make a function/mixin that will make a color darker if it is already dark but lighter when it is light (normalize/extremeize?) Is it possible to do this by passing a property name (color, background-color, border-right-color,…
Joe Flateau
  • 1,225
  • 1
  • 20
  • 32
3
votes
1 answer

converting less mixins to scss

I was using the following LESS mixins to generate css for bidi usage. .ltr(@ltrRules) { body[dir="ltr"] & , body[dir="rtl"] *[dir="ltr"] & , body[dir="rtl"] *[dir="ltr"]& { @ltrRules(); } } .rtl (@rtlRules) { body[dir="rtl"] & , …
epeleg
  • 10,347
  • 17
  • 101
  • 151
3
votes
2 answers

LESS: How can I pass a mixin as an argument to another mixin?

I have some basic mixins that apply some rules using media queries .on-small(@rules) { @media (@minWidthSmall) { @rules(); } } .on-medium(@rules) { @media (@minWidthMedium) { @rules(); } } // and .on-large, .on-x-large and so on And I'm…
Amr M. AbdulRahman
  • 1,820
  • 3
  • 14
  • 22
3
votes
1 answer

Can I use mixins to generate new mixins in LESS?

I'm trying to use LESS to dynamically generate a set of mixins that would help me write cleaner media query code. So far in my limited knowledge of the language I've put together code that looks like this: @sizes: xxs, xs, sm, md,…
styke
  • 2,116
  • 2
  • 23
  • 51
3
votes
1 answer

How do I replace "&" in a Less variable with the parent selector?

I have a Less mixin like so: http://codepen.io/sirlancelot/pen/QjzzEb?editors=110 // The mixin to toggle containers #toggle-container( @collapsed-selector: ~"&.__collapsible .section--body", @expanded-selector: ~"&.__open .section--body" )…
matpie
  • 17,033
  • 9
  • 61
  • 82
3
votes
1 answer

how to write less mixin for states like :hover, : active, :disabled?

This is how I wrote it, but I get Parse Error: Unrecognised Input How can I circumvent this? I do not want to declare individual mixin for focus, active and disabled states. I am using WinLess for compiling on Windows 7. WinLess…
vssadineni
  • 454
  • 4
  • 11
3
votes
2 answers

How to ignore passing parameter in mixin

I want to use Less mixin for borders in a project, but often I need to use different sides of border, not all. I have written a simple mixin: .border-all (@border; @border-type; @border-color) { border-top: @border @border-type @border-color; …
Alex Sokol
  • 124
  • 8
3
votes
1 answer

Convert from pixels to em

I watched a really good tutorial about bombproof webdesign from Tutsplus the other day. The instructor had an awesome function that was made in Stylus. $base_font_size = 16 $base_font_size_ems = unit($base_font_size / 16, em) $px = unit(1 /…
Jonas Bröms
  • 53
  • 1
  • 5
3
votes
1 answer

Less Mixins: Border smart mixins

I've created separate mixins for border Shorthand property and another one for different sides. Shorthand .border(@width: 1px, @style: solid, @color: black){ border: @arguments; } Usage .class1{ .border; } For different…
FrontDev
  • 837
  • 5
  • 19
3
votes
1 answer

Not able to extend Less mixin

I would like to have below CSS output using Less .selected-values, .selected-values a { background-color: #505050; color: #fff; } .selected-values { display: block; } .selected-values a { text-decoration: none; } So far i have…
khagesh
  • 948
  • 1
  • 6
  • 18
3
votes
1 answer

Use a mixin named parameter in LESS as a pseudo selector

My idea here is to create a LESS mixin that could add a pseudo element :before or :after to any element in my page. I have tried to use the "Named parameter" of LESS mixins to create a generic mixin in which the user could specify whether the pseudo…
3
votes
1 answer

!important in mixins

I have a function: .box-shadow(@bxsh: none) { -moz-box-shadow: @bxsh; -webkit-box-shadow: @bxsh; box-shadow: @bxsh; } How to add !important to function parameter? I right this: .box-shadow(inset 0 0 10px rgba(0,0,0,0.2), 0 1px 0 #fff…
acidernt
  • 2,173
  • 2
  • 19
  • 33
1
2
3
10 11