Is there anyway to import a regular CSS file with Sass\'s @import
command? While I\'m not using all of the SCSS syntax from sass, I do still enjoy it\'s combini
Simple workaround:
All, or nearly all css file can be also interpreted as if it would be scss. It also enables to import them inside a block. Rename the css to scss, and import it so.
In my actual configuration I do the following:
First I copy the .css file into a temporary one, this time with .scss extension. Grunt example config:
copy: {
dev: {
files: [
{
src: "node_modules/some_module/some_precompiled.css",
dest: "target/resources/some_module_styles.scss"
}
]
}
}
Then you can import the .scss file from your parent scss (in my example, it is even imported into a block):
my-selector {
@import "target/resources/some_module_styles.scss";
...other rules...
}
Note: this could be dangerous, because it will effectively result that the css will be parsed multiple times. Check your original css for that it contains any scss-interpretable artifact (it is improbable, but if it happen, the result will be hard to debug and dangerous).
You must prepend an underscore to the css file to be included, and switch its extension to scss (ex: _yourfile.scss
). Then you just have to call it this way:
@import "yourfile";
And it will include the contents of the file, instead of using the CSS standard @import directive.
Good news everyone, Chris Eppstein created a compass plugin with inline css import functionality:
https://github.com/chriseppstein/sass-css-importer
Now, importing a CSS file is as easy as:
@import "CSS:library/some_css_file"
If I am correct css is compatible with scss so you can change the extension of a css to scss and it should continue to work. Once you change the extension you can import it and it will be included in the file.
If you don't do that sass will use the css @import which is something you don't want.
You can use a third-party importer
to customise @import
semantics.
node-sass-import-once, which works with node-sass (for Node.js) can inline import CSS files.
Example of direct usage:
var sass = require('node-sass');,
importOnce = require('node-sass-import-once');
sass.render({
file: "input.scss",
importer: importOnce,
importOnce: {
css: true,
}
});
Example grunt-sass config:
var importOnce = require("node-sass-import-once");
grunt.loadNpmTasks("grunt-sass");
grunt.initConfig({
sass: {
options: {
sourceMap: true,
importer: importOnce
},
dev: {
files: {
"dist/style.css": "scss/**/*.scss"
}
}
});
Note that node-sass-import-once cannot currently import Sass partials without an explicit leading underscore. For example with the file partials/_partial.scss
:
@import partials/_partial.scss
succeeds@import * partials/partial.scss
failsIn general, be aware that a custom importer could change any import semantics. Read the docs before you start using it.
After having the same issue, I got confused with all the answers here and the comments over the repository of sass in github.
I just want to point out that as December 2014, this issue has been resolved. It is now possible to import css
files directly into your sass file. The following PR in github solves the issue.
The syntax is the same as now - @import "your/path/to/the/file"
, without an extension after the file name. This will import your file directly. If you append *.css
at the end, it will translate into the css
rule @import url(...)
.
In case you are using some of the "fancy" new module bundlers such as webpack, you will probably need to use use ~
in the beginning of the path. So, if you want to import the following path node_modules/bootstrap/src/core.scss
you would write something like @import "~bootstrap/src/core"
.
NOTE:
It appears this isn't working for everybody. If your interpreter is based on libsass
it should be working fine (checkout this). I've tested using @import
on node-sass and it's working fine. Unfortunately this works and doesn't work on some ruby instances.