Introduction
The Sass @At Rules and directives in syntactically awesome style sheets are backbone features that you should at least know about, particularly when it concerns the @import rule. Some of these @-rules are extensions of Cascading Style Sheets(CSS) @-rules, while others are Sass-specific directives. Either way, you should know what these rules are for and how they work if you will be working with Sass.
This blog is Part-I of Sass @At rules and directives. In this blog, we will discuss @imports, @extend, @warn, @error in detail with their syntax.
The remaining topics are covered in Sass @At rules and directives Part-II.

@import
Syntactically awesome style sheet extends the CSS's @import rule with the ability to import CSS and SASS stylesheets, providing access to functions, mixins, and variables and combining multiple stylesheets' CSS together. Unlike plain Cascading Style Sheet(CSS) @imports, which requires the browser to make multiple HTTP requests as it renders your page, Sass imports are handled entirely during compilation.
SCSS Syntax:
// foundation/_text.scss
code {
padding: .30em;
line-height: 0;
}
// foundation/_code.scss
ul, ol {
text-align: left;
& & {
padding: {
bottom: 0;
left: 0;
}
}
}
// style.scss
@import 'foundation/code', 'foundation/lists';
SASS Syntax:
// foundation/_text.sass
code
padding: .30em
line-height: 0
// foundation/_code.sass
ul, ol
text-align: left
& &
padding:
bottom: 0
left: 0
// style.sass
@import foundation/code, foundation/lists
CSS Syntax:
code {
padding: .30em;
line-height: 0;
}
ul, ol {
text-align: left;
}
ul ul, ol ol {
padding-bottom: 0;
padding-left: 0;
}
Syntactically awesome style sheet imports have the same syntax as the CSS imports, except they allow multiple imports to be separated from each other by commas rather than requiring each one to have its @import. Also, in the indented syntax, imported URLs are not required to have quotes.
Nested@import
@imports are generally written at the top level of a stylesheet document, but they can also be nested within the style rules or plain CSS at-rules. The imported Cascading Style Sheets are nested in that context, making nested imports useful for scoping a chunk of CSS to a particular element or media query. Note that top-level functions, mixins, and variables defined in the nested import are still defined globally.
SCSS Syntax:
// _theme.scss
pre, code {
font-family: 'Source Code Pro', Helvetica, Arial;
border-radius: 4px;
}
// style.scss
.theme-sample {
@import "theme";
}
SASS Syntax:
// _theme.sass
pre, code
font-family: 'Source Code Pro', Helvetica, Arial
border-radius: 4px
// style.sass
.theme-sample
@import theme
CSS Syntax:
.theme-sample pre, .theme-sample code {
font-family: 'Source Code Pro', Helvetica, Arial;
border-radius: 4px;
}
Partial@import
When you import an SCSS/Sass file, it will be compiled to a CSS file of the same name. We usually want to import separate SASS/SCSS files into one master CSS file instead of multiple CSS files. To fulfill this, we need to import the files as partials.
We make a SASS/SCSS file into a partial by adding an underscore(_) in front of the name. The underscore tells sass not to compile the file as an individual CSS file but to concatenate it with the current file that contains the @import statement.
Let's see an example:
We changed myNinja into _myNinja.
@import "myNija";
While importing these partials, we do not have to include the underscore.
The difference is that no myninja.css file will be created, the file will be merged with the main CSS file.
Plain CSS@import
Since @import is defined in CSS, sass needs a way of compiling plain CSS @imports without trying to import the files at compile time.
To fulfill this, and to ensure SCSS is as much of a superset of CSS as possible, Sass will compile any @imports with the following characteristics to plain CSS imports:
- Imports where the Uniform Resource Locator ends with .css.
- Imports where the Uniform Resource Locator begins https:// or http://.
- Imports where the Uniform Resource Locator is written as a URL().
SCSS Syntax:
@import "theme.css";
@import "http://fonts.googleapis.com/css?family=Droid+Sans";
@import url(theme);
@import "landscape" screen and (orientation: landscape);
SASS Syntax:
@import "theme.css"
@import "http://fonts.googleapis.com/css?family=Droid+Sans"
@import url(theme)
@import "landscape" screen and (orientation: landscape)
CSS Syntax:
@import url(theme.css);
@import "http://fonts.googleapis.com/css?family=Droid+Sans";
@import url(theme);
@import "landscape" screen and (orientation: landscape);