I am using SASS and there is nice feature: I can create "fake/virtual" class and then use it for extend.
Example:
%myFakeClass
{
color:#fff;
background-color:#000;
}
.myRealClass
{
@extend %myFakeClass;
}
.myRealClass2
{
@extend %myFakeClass;
}
Output:
.myRealClass, .myRealClass2
{
color:#fff;
background-color:#000;
}
The question:
Does LESS has something similar? In other words, I want to create a "virtual class" that I can inherit from, but the "virtual class" itself not exists in output.
Not Directly as of Yet
As of this date (11-22-2013) there is still a feature request that would allow this by doing extending on empty parameter mixins (which do not output css themselves). So eventually something like this would be possible (which mirrors almost exactly what you want):
And output as you expect.
Workaround for now
This was mentioned by Bass Jobsen, but not explicitly demonstrated. In LESS 1.5, you build a file for your fake classes, say
fakeClasses.less
, which for our example has this in it:Then in your file that you want to extend to it, let's say
styles.less
, you do this:This will import the
fakeClasses.less
classes but NOT compile them to css (so they are "fake" within the context ofstyles.less
, but "real" in that they can be extended to), and you will get the output you expect.I'm not entirely sure if
@extend
works the same as a "mixin", but it looks the same.The out put for this would be the same as what you have above. I added variables in the mixin for easier customization for this mixin.
Example:
The output for all three classes would be:
Like I said, I'm not entirely sure if there is a big difference between extending a class in SASS and using a mixin in LESS. Hope this helps either way.
Oh, and just to clarify, if the
.myFakeClass()
class is in a separate .less file that is imported, it will not show up in your CSS unless it is used. I tested this on a website I'm building. I have:The class
.box-shadow
does not show up in my CSS at all.Link: http://lucienconsulting.com/gs-news/wp-content/themes/TheStone/css/style.css
However, if you write a mixin like this:
It will show up like a normal class even if not used. Obviously, it looks just like a normal class and could be used by itself, or as a mixin, like so:
This would compile to:
It works, but
.myMixin
would also show up in your style sheet in this case.But, like I said, in my original example, it would not.
Maybe the following helps you
img { &:extend(.img-responsive); }
from Why gives Grunt / Recess an error and Lessc not when compiling Bootstrap 3 RC1?update from How do I create a mixin using less.js that doesn't output in the final stylesheet:
since LESS 1.5 you could also place you virtual classes in a separate file and use:
@import (reference) "file.less";