import .css file into .less file

CssImportLess

Css Problem Overview


Can you import .css files into .less files...?

I'm pretty familiar with less and use it for all my development. I regularly use a structure as follows:

@import "normalize";

//styles here

@import "mixins";
@import "media-queries";
@import "print";

All imports are other .less files and all works as it should.

My current issue is this: I want to import a .css file into .less that references styles used in the .css file as follows:

@import "../style.css";

.small {
    font-size:60%;
    .type;
}
// other styles here

The .css file contains a class called .type but when I try to compile the .less file I get the error NameError: .type is undefined

Will the .less file not import .css files, only other .less ones...? Or am I referencing it wrong...?!

Css Solutions


Solution 1 - Css

You can force a file to be interpreted as a particular type by specifying an option, e.g.:

@import (css) "lib";

will output

@import "lib";

and

@import (less) "lib.css";

will import the lib.css file and treat it as less. If you specify a file is less and do not include an extension, none will be added.

Solution 2 - Css

If you want your CSS to be copied into the output without being processed, you can use the (inline) directive. e.g.,

@import (inline) '../timepicker/jquery.ui.timepicker.css';

Solution 3 - Css

I had to use the following with version 1.7.4

@import (less) "foo.css"

I know the accepted answer is @import (css) "foo.css" but it didn't work. If you want to reuse your css class in your new less file, you need to use (less) and not (css).

Check the documentation.

Solution 4 - Css

Change the file extension of your css file to .less. You don't need to write any LESS in it; all CSS is valid LESS (except of the MS stuff that you have to escape, but that's another issue.)

Per Fractalf's answer this is fixed in v1.4.0

Solution 5 - Css

From the LESS website:

> If you want to import a CSS file, and don’t want LESS to process it, > just use the .css extension: > > @import "lib.css"; The directive will just be left as is, and end up > in the CSS output.

As jitbit points out in the comments below, this is really only useful for development purposes, as you wouldn't want to have unnecessary @imports consuming precious bandwidth.

Solution 6 - Css

Try this :

@import "lib.css";

From the Official Documentation :

> You can import both css and less files. Only less files import > statements are processed, css file import statements are kept as they > are. If you want to import a CSS file, and don’t want LESS to process > it, just use the .css extension:


Source : http://lesscss.org/

Solution 7 - Css

If you just want to import a CSS-File as a Reference (e.g. to use the classes in Mixins) but not include the whole CSS file in the result you can use @import (less,reference) "reference.css";:

my.less

 @import (less,reference) "reference.css";
 .my-class{
	 background-color:black;
	 .reference-class;
	 color:blue;
 }

reference.css

.reference-class{
	border: 1px solid red;
}

*Result (my.css) with lessc my.less out/my.css *

.my-class {
  background-color: black;
  border: 1px solid red;
  color: blue;
}

I'm using lessc 2.5.3

Solution 8 - Css

If you want to import a css file that should be treaded as less use this line:

.ie {
  @import (less) 'ie.css';
}

Solution 9 - Css

since 1.5.0 u can use the 'inline' keyword.

Example: @import (inline) "not-less-compatible.css";

You will use this when a CSS file may not be Less compatible; this is because although Less supports most known standards CSS, it does not support comments in some places and does not support all known CSS hacks without modifying the CSS. So you can use this to include the file in the output so that all CSS will be in one file.

(source: http://lesscss.org/features/#import-directives-feature)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMr Jonny WoodView Question on Stackoverflow
Solution 1 - CssFractalfView Answer on Stackoverflow
Solution 2 - CssmpenView Answer on Stackoverflow
Solution 3 - CssGudradainView Answer on Stackoverflow
Solution 4 - CssMathleticsView Answer on Stackoverflow
Solution 5 - CssJeff JenkinsView Answer on Stackoverflow
Solution 6 - CssDr.KameleonView Answer on Stackoverflow
Solution 7 - CsshinneLinksView Answer on Stackoverflow
Solution 8 - CssCharlesView Answer on Stackoverflow
Solution 9 - CssPieter-JanView Answer on Stackoverflow