Sass Importing Files Just like CSS, Sass also supports the @import directive. The @import directive allows you to include the content of one file in another.
As of October 2021 @import will be deprecated, along with global built-in functions, and one year after that (no later than October 2022) @import support will be dropped. Make sure you go check out the newly refreshed Sass documentation for the latest updates.
scss files, Sass can import plain old . css files. The only rule is that the import must not explicitly include the . css extension, because that's used to indicate a plain CSS @import .
So, what is the difference between this @import and the one from SASS/SCSS? They fundamentally do the same conceptual thing, but SASS is preprocessed by the SASS compiler and that compiler does whatever it does with that directive, while a browser does whatever it does when encountering the directive in a . css file.
If you are using Sass in a Rails project, the sass-rails gem, https://github.com/rails/sass-rails, features glob importing.
@import "foo/*" // import all the files in the foo folder
@import "bar/**/*" // import all the files in the bar tree
To answer the concern in another answer "If you import a directory, how can you determine import order? There's no way that doesn't introduce some new level of complexity."
Some would argue that organizing your files into directories can REDUCE complexity.
My organization's project is a rather complex app. There are 119 Sass files in 17 directories. These correspond roughly to our views and are mainly used for adjustments, with the heavy lifting being handled by our custom framework. To me, a few lines of imported directories is a tad less complex than 119 lines of imported filenames.
To address load order, we place files that need to load first – mixins, variables, etc. — in an early-loading directory. Otherwise, load order is and should be irrelevant... if we are doing things properly.
This feature will never be part of Sass. One major reason is import order. In CSS, the files imported last can override the styles stated before. If you import a directory, how can you determine import order? There's no way that doesn't introduce some new level of complexity. By keeping a list of imports (as you did in your example), you're being explicit with import order. This is essential if you want to be able to confidently override styles that are defined in another file or write mixins in one file and use them in another.
For a more thorough discussion, view this closed feature request here:
Check out the sass-globbing project.
I create a file named __partials__.scss
in the same directory of partials
|- __partials__.scss
|- /partials
|- __header__.scss
|- __viewport__.scss
|- ....
In __partials__.scss
, I wrote these:
@import "partials/header";
@import "partials/viewport";
@import "partials/footer";
@import "partials/forms";
....
So, when I want import the whole partials
, just write @import "partials"
. If I want import any of them, I can also write @import "partials/header"
.
It might be an old question, but still relevant in 2020, so I might post some update. Since Octobers'19 update we generally should use @use instead of @import, but that's only a remark. Solution to this question is use index files to simplify including whole folders. Example below.
// foundation/_code.scss
code {
padding: .25em;
line-height: 0;
}
// foundation/_lists.scss
ul, ol {
text-align: left;
& & {
padding: {
bottom: 0;
left: 0;
}
}
}
// foundation/_index.scss
@use 'code';
@use 'lists';
// style.scss
@use 'foundation';
https://sass-lang.com/documentation/at-rules/use#index-files
You could use a bit of workaround by placing a sass file in folder what you would like to import and import all files in that file like this:
file path:main/current/_current.scss
@import "placeholders";
@import "colors";
and in next dir level file you just use import for file what imported all files from that dir:
file path:main/main.scss
@import "EricMeyerResetCSSv20";
@import "clearfix";
@import "current";
That way you have same number of files, like you are importing the whole dir. Beware of order, file that comes last will override the matching stiles.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With