The original CSS have the @import
keyword, which helps us with including an external CSS file.
So, what is the difference between this @import
and the one from SASS/SCSS?
The original CSS have the @import
keyword, which helps us with including an external CSS file.
So, what is the difference between this @import
and the one from SASS/SCSS?
@import in CSS: CSS has an import option that lets you split your CSS into smaller, more maintainable portions.
Whereas,
@import in SASS/SCSS: Sass builds on top of the current CSS
@import
but instead of requiring an HTTP request, Sass will take the file that you want to import and combine it with the file you're importing into so you can serve a single CSS file to the web browser.
For Example:
In _reset.scss
// _reset.scss
html,
body,
ul,
ol {
margin: 0;
padding: 0;
}
In base.scss
// base.scss
@import 'reset';
body {
font: 100% Helvetica, sans-serif;
background-color: #efefef;
}
References: SASS Guide
I recommend to use Sass imports wherever it is possible.
Assume that we have the following code written in native CSS:
@import "style1.css";
@import "style2.css";
@import "style3.css";
As a result, you getting one file, which is downloading another files. These requests are sent separately to your server and you get 3 files in response. Just look at the developer console to notice this problem.
In Sass way files are concatenated to one file (by the frontend workflow you use) and it's just one request to your server.
From the docs:
Sass extends CSS's @import rule with the ability to import Sass and CSS stylesheets, providing access to mixins, functions, and variables and combining multiple stylesheets' CSS together.
However, the use of @import
is now discouraged in Sass, which recommends using @use
instead.
This should reduce the confusion between css and scss @import
statements.