Sub topic 3
SCSS Syntax versus the Indented Syntax
This chapter assumes you know core CSS (selectors, properties, media queries) and can run commands in a terminal. It also assumes you have installed Dart Sass and can compile a file, as covered in the setup chapter.
Sass has two syntaxes. Both compile to the same CSS and support the same features. They differ only in how you write the source.
The two syntaxes
SCSS is a superset of CSS. Every valid CSS file is also a valid SCSS file. SCSS uses braces and semicolons, so it looks like the CSS you already write. Its file extension is .scss.
The indented syntax is the original Sass syntax. It drops braces and semicolons and uses line breaks and indentation to show structure. Its file extension is .sass.
Here is the same rule in both.
$primary: #3366cc;
.card {
border: 1px solid $primary;
&:hover {
background: rgba($primary, 0.1);
}
}$primary: #3366cc
.card
border: 1px solid $primary
&:hover
background: rgba($primary, 0.1)Both compile to this CSS:
.card {
border: 1px solid #3366cc;
}
.card:hover {
background: rgba(51, 102, 204, 0.1);
}Key differences
| Feature | SCSS (`.scss`) | Indented (`.sass`) |
|---|---|---|
| Blocks | Braces `{ }` | Indentation |
| Statement end | Semicolon | Line break |
| Plain CSS is valid | Yes | No |
| Multi-line values | Allowed freely | Need care with line breaks |
| Mixin shorthand | `@mixin` and `@include` | Also `=` and `+` |
The indented syntax has shorthand for mixins: =name defines one and +name includes it. SCSS does not have these.
=respond($min)
@media (min-width: $min)
@content
.nav
display: block
+respond(48rem)
display: flexHow the compiler picks a syntax
The Dart Sass command line chooses the syntax from the file extension.
sass src/styles.scss dist/styles.css
sass src/legacy.sass dist/legacy.cssIf the input comes from standard input, there is no extension to read. Pass --indented to tell Sass to expect the indented syntax.
echo -e ".box\n color: red" | sass --stdin --indentedWhich one to use
Use SCSS unless you have a reason not to. Most documentation, frameworks, and examples use it, and the rest of this tutorial does too. You can paste existing CSS into a .scss file and it works. This makes it easy to adopt Sass gradually.
The indented syntax suits people who like minimal punctuation, and some teams use it consistently. You can mix both in one project. A .scss file can load a .sass file with @use, because Sass resolves either extension.
Common errors
- Missing semicolon in SCSS. Leaving one off a declaration that is followed by another declaration causes a parse error. The last declaration in a block may omit it, but adding it is safer.
- Inconsistent indentation in
.sass. Mixing tabs and spaces, or changing the indent width within a file, gives an "Inconsistent indentation" error. - Pasting SCSS into a
.sassfile. Braces and semicolons are not valid there. Rename the file to.scssor remove the punctuation. - Wrong extension for the content. The compiler trusts the extension, so a
.scssfile containing indented code fails to parse.
Exercise
A teammate saves the following in theme.scss and the compile fails. What is wrong, and how can you fix it two ways?
.btn
color: red
padding: 1rem
Show answer
The file uses the indented syntax, but the .scss extension makes Sass parse it as SCSS, which requires braces and semicolons. Fix 1: rename the file to theme.sass. Fix 2: keep the name and rewrite the rule as .btn { color: red; padding: 1rem; }.
Exercise
How do you compile indented-syntax code that arrives on standard input?
Show answer
Use the --stdin flag together with --indented, for example sass --stdin --indented. Without --indented, Sass parses the input as SCSS.