Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LESS mixin a variable class name

Tags:

I am using Font Awesome 4.0.0, and want to do something like this in LESS:

.btn-github {   .btn;   .btn-primary;   margin-left: 3em;    i {     .@{fa-css-prefix};     .@{fa-css-prefix}-github;     .@{fa-css-prefix}-lg;     margin-right: 1em;   } } 

That doesn't compile with the error:

ParseError: Unrecognised input in - on line ...

Is it possible to accomplish this? It would certainly make a beautiful button for me.

like image 671
Peter Sankauskas Avatar asked Oct 26 '13 04:10

Peter Sankauskas


People also ask

What can be passed to a LESS mixin?

Mixins are a group of CSS properties that allow you to use properties of one class for another class and includes class name as its properties. In LESS, you can declare a mixin in the same way as CSS style using class or id selector. It can store multiple values and can be reused in the code whenever necessary.

What does the special variable @arguments do in a LESS mixin?

Additionally, LESS has a special variable named @arguments which will pass the default values all together.

How do you define a LESS variable?

Less variables are defined with a symbol @ and the values are assigned in the variable with a colon ( : ). Variables are actually "constants" in that they can only be defined once.

What is Mixins in CSS?

The @mixin directive lets you create CSS code that is to be reused throughout the website. The @include directive is created to let you use (include) the mixin.


1 Answers

There are at least 2 problems with what you are trying to do (for LESS >=1.6 see update bellow):

1) Unfortunately it is not possible to call a mixin using selector interpolation.

With selector interpolation LESS expects the construct to be of following format:

.@{selector-string} { property:value; } 

(the interpolated selector can have also some static string pre or post the interpolation)

so

.@{selector-string}; 

is Unrecognised by the LESS compiler. See more here: How can I call a mixin by reference in LESS?

2) A ruleset with an interpolated selector gets directly printed to the CSS output and does not exist as a mixin that you could reuse in the LESS run

For example:

@foo: test;  .@{foo} {   length: 20px; }  .bar {   .test; } 

will return:

    Name error: .test is undefined     .bar {  .test;} 

See more on that here: LESS CSS: Reuse generated .@{name} class as a mixin

Possible solution for your problem would be redifinig the font awesome rules as some kind of reusable mixins (without using interpolation). Something like this:

@fa-var-github: "\f09b";  .fa-mixin() {   display: inline-block;   font-family: FontAwesome;   font-style: normal;   font-weight: normal;   line-height: 1;   -webkit-font-smoothing: antialiased;   -moz-osx-font-smoothing: grayscale; } .fa-mixin-lg() {   font-size: (4em / 3);   line-height: (3em / 4);   vertical-align: -15%; } .fa-mixin-icon(@icon){   @var: "fa-var-@{icon}";   &:before { content: @@var; } } i {   .fa-mixin;   .fa-mixin-lg;   .fa-mixin-icon(github); } 

If you don't really need the variables and just want to include the rules, the best way would be just to import the compiled CSS version of the FontAwesome (see answer here):

@import (less) 'font-awesome.css'; 

and then you can just use the CSS rules like LESS mixins or extend their selectors as you see fit and it should work perfectly.


Update:

As of LESS >= 1.6 rules with interpolated selectors can be accessed as mixins ... so the #2 limitation form above does not apply anymore (but you still can not call a mixin dynamically with interpolation). So you can simply call LESS mixins and variables from the imported font-awesome.less file like so:

i {   .fa;   .fa-lg;   &:before{     content: @fa-var-github;   } } 
like image 114
Martin Turjak Avatar answered Oct 21 '22 23:10

Martin Turjak