Sub topic 3
SCSS syntax versus the indented syntax
Sass has two syntaxes. SCSS looks like CSS with extra features. The indented syntax drops braces and semicolons and uses line breaks and indentation instead. This chapter explains how they differ, how the compiler chooses between them, and which mistakes to expect. It assumes you can read basic CSS and run the sass command from a terminal. Variables, nesting, and mixins appear here only as examples. Later chapters cover them in full.
Two syntaxes, one language
Both syntaxes describe the same language. They have the same variables, nesting, mixins, functions, maps, control directives, and module system. The compiler turns either one into the same kind of internal tree and then emits CSS. Only the way you write the source differs.
| Feature | SCSS (`.scss`) | Indented syntax (`.sass`) |
|---|---|---|
| Blocks | Curly braces `{ }` | Indentation |
| End of a declaration | Semicolon | Line break |
| Valid CSS is valid source | Yes | No |
| Mixin definition | `@mixin name` | `=name` (shorthand) or `@mixin name` |
| Mixin call | `@include name` | `+name` (shorthand) or `@include name` |
| Typical use today | Most projects | Small projects, personal preference |
SCSS: a superset of CSS
SCSS is a superset of CSS, so every valid .css file is also valid SCSS. You can rename styles.css to styles.scss and it compiles unchanged. You then add Sass features one at a time. This makes SCSS the easiest syntax to adopt in an existing codebase, and it is the syntax used for most Sass code you will meet.
$primary: #3366cc;
@mixin respond($min) {
@media (min-width: $min) {
@content;
}
}
.nav {
display: block;
color: $primary;
@include respond(48rem) {
display: flex;
}
}The indented syntax
The indented syntax uses the .sass extension. Nesting is expressed by indentation, and a line break ends each declaration. Here is the same stylesheet written that way:
$primary: #3366cc
=respond($min)
@media (min-width: $min)
@content
.nav
display: block
color: $primary
+respond(48rem)
display: flexBoth files compile to the same CSS:
.nav {
display: block;
color: #3366cc;
}
@media (min-width: 48rem) {
.nav {
display: flex;
}
}Three details stand out in the indented version:
- Variables still start with
$, and@mediaand@contentkeep their@. =respondand+respondare shorthands. Writing@mixin respondand@include respondalso works in.sassfiles, and many teams prefer the longer form because it is easier to read.- Blank lines are allowed and do not end a block. Only indentation decides where a block ends.
How the compiler chooses a syntax
The compiler picks the syntax from the file extension. A .scss file is parsed as SCSS and a .sass file as the indented syntax. A .css file is parsed as plain CSS.
sass src/styles.scss dist/styles.css
sass src/styles.sass dist/styles.css
sass --watch src:distWatch mode compiles both kinds of file found in the source folder. Files whose names start with an underscore, such as _tokens.scss, are partials and are not compiled on their own.
Standard input has no file extension, so Sass assumes SCSS. To feed indented syntax through a pipe, add --indented:
printf '.a\n color: red\n' | sass --stdin --indentedMixing syntaxes in one project
A project can contain both kinds of file. @use finds a partial regardless of its syntax, so an SCSS file can load an indented partial and the other way around.
// _tokens.sass
$space: 8px// main.scss
@use 'tokens';
.box {
padding: tokens.$space * 2;
}This compiles to padding: 16px. Mixing is legal, but it makes the codebase harder to read, because contributors have to switch between two styles. Most teams choose one syntax for the whole project.
Differences you will notice in practice
Comments
Both syntaxes support // silent comments, which never appear in the output. They also support /* */ loud comments, which do appear in expanded output. In the indented syntax, a loud comment can use indentation instead of a closing */. Everything indented under the opening line belongs to the comment.
Long values and selector lists
In SCSS, a declaration can span many lines because the semicolon marks the end. In the indented syntax, a line break normally ends the declaration. Inside parentheses, brackets, and braces, line breaks are allowed, so multi-line maps work in both syntaxes. A selector list can continue onto the next line if the line ends with a comma.
$colors: (primary: #3366cc, danger: #cc3333)
h1,
h2,
h3
margin: 0Conversion
Dart Sass does not include a tool for converting between the two syntaxes. The old sass-convert command belonged to Ruby Sass, which is no longer maintained. In practice, you convert by hand. Moving from SCSS to the indented syntax means removing braces and semicolons and fixing the indentation. Moving the other way means adding them back.
Common errors
- Mixing tabs and spaces in a
.sassfile. Indentation carries meaning, so the compiler rejects inconsistent indentation. Pick one style, usually two spaces, and have your editor enforce it. - Inconsistent indent depth. Every line in the same block must use the same indentation. A line indented by three spaces under a block that uses two is an error.
- Writing braces or semicolons in a
.sassfile. These are SCSS habits. The indented parser does not accept them as block delimiters. - Forgetting a semicolon in a
.scssfile. The error usually points at the line after the missing semicolon, because the parser only notices the problem when it reads the next token. Check the line above the one reported. - Using the wrong extension. If you paste SCSS into a
.sassfile, or the reverse, the compiler reports a parse error at the first brace or the first unexpected indentation. Rename the file or rewrite the code. - Piping indented code without
--indented. The compiler treats standard input as SCSS and fails on the first line.
Choosing between them
Use SCSS if you want to adopt Sass gradually, copy CSS from documentation, or work with other developers. Most tutorials, frameworks, and style guides assume SCSS, so it is the safer default. The indented syntax is a reasonable choice if you prefer less punctuation and your team agrees to it. Since the language is the same, this choice does not limit what you can build.
Exercise
Rewrite this SCSS as the indented syntax, using the = and + shorthands for the mixin.
@mixin center {
display: flex;
justify-content: center;
}
.hero {
@include center;
height: 50vh;
}
Show answer
The result is: =center display: flex justify-content: center .hero +center height: 50vh Braces and semicolons are removed, the nested lines are indented, @mixin becomes =, and @include becomes +. Writing @mixin center and @include center would also be valid.
Exercise
You run printf '.a\n color: red\n' | sass --stdin and get a parse error. What causes it, and how do you fix it?
Show answer
Standard input has no file extension, so Sass assumes SCSS and fails on the indented source. Add the --indented flag: printf '.a\n color: red\n' | sass --stdin --indented.