Sub topic 1
What Sass is and why use it
Sass (Syntactically Awesome Style Sheets) is a CSS preprocessor. You write styles in a Sass file, and a compiler turns that file into plain CSS. Browsers never see Sass. They only see the CSS that comes out. This chapter explains that model, what Sass adds to CSS, and when it is worth using.
Prerequisites
This chapter assumes you already have:
- Core CSS: selectors, properties, the cascade, and media queries.
- Basic command-line use, such as running a command in a terminal and changing directories.
- Familiarity with HTML and with linking a stylesheet using a
<link>element.
You do not need to know any Sass yet. Installation and the compile commands come in the next chapter.
The working model: compile time versus run time
Sass runs on your machine (or in your build pipeline), before the site is deployed. The browser runs CSS later, on the visitor's machine. Keeping these two stages apart explains most of how Sass behaves.
Everything Sass does happens in the compiler box: substituting variables, expanding nesting, running loops, and calling functions. By the time the output reaches the browser, none of those features exist. This is the source of one of the most common mistakes, covered in the section on common errors.
What Sass adds to CSS
Sass is a superset of CSS. Every valid CSS file is also a valid SCSS file, so you can rename styles.css to styles.scss and it compiles unchanged. You then adopt features one at a time.
The main additions are:
- Variables store values such as colors and spacing so you define them once.
- Nesting lets you write child selectors inside their parent rule instead of repeating the parent.
- Mixins are reusable blocks of declarations that can take arguments.
- Functions compute and return a value, for example converting pixels to
rem. - Maps hold named values, which you can loop over to generate rules.
- Control directives (
@if,@each,@for) add conditions and loops. - Modules (
@useand@forward) split code across files with namespaces, so names in one file do not collide with names in another.
Later chapters cover each of these. For now, one small example shows the idea.
$primary: #3366cc;
.card {
border: 1px solid $primary;
&:hover {
background: rgba($primary, 0.1);
}
}The compiler produces this CSS:
.card {
border: 1px solid #3366cc;
}
.card:hover {
background: rgba(51, 102, 204, 0.1);
}Three things happened. $primary was replaced by its value. The nested &:hover was expanded into the full selector .card:hover. The rgba() call, given a hex color and an alpha value, was computed at compile time into a plain rgba(51, 102, 204, 0.1).
Why teams use Sass
The benefits are practical, and they grow with the size of the codebase.
- One source of truth for values. A brand color used in forty places is defined once. Changing it is a one-line edit.
- Less repetition. Nesting and mixins remove repeated selectors and repeated blocks such as media query wrappers.
- Organization across files. A large stylesheet can be split into small files, each with a single job, and combined at compile time into one CSS file.
- Generated CSS. Loops over maps can produce utility classes (for example one color class per design token) without hand-writing each.
- Reviewable logic. Calculations, such as pixels to
rem, live in named functions instead of being repeated as hand-computed numbers.
The cost is a build step. You need a compiler in your workflow, and your team needs to read both Sass and the CSS it generates.
Sass versus native CSS features
Modern CSS has absorbed some of what used to require Sass. This matters for deciding whether to use it.
| Feature | Native CSS | Sass |
|---|---|---|
| Variables | Custom properties (`--color`), evaluated in the browser at run time | `$variables`, replaced at compile time |
| Nesting | Supported in current browsers | Supported, and compiled into flat selectors |
| Mixins and functions | Not available | Available |
| Loops and conditionals | Not available in stylesheets | Available |
| Splitting into files | `@import` makes a separate network request per file | `@use` combines files at compile time |
The most important row is variables. A CSS custom property can change while the page is running, for example when a user switches to a dark theme. A Sass variable cannot, because it is gone before the page loads. The two are not competitors. A common pattern is to use Sass to generate and organize custom properties, and let the browser handle theme switching.
Which Sass to use
Sass has had several implementations. Dart Sass is the reference implementation and the only one that receives new features, including the module system (@use and @forward). LibSass (used by the node-sass npm package) and Ruby Sass are deprecated. If you inherit a project that depends on node-sass, plan to move it to the sass package, which is Dart Sass compiled to JavaScript.
Common errors and misconceptions
- Linking the
.scssfile in HTML. Browsers do not understand SCSS. Link the compiled.cssfile, and compile again whenever the source changes. - Expecting Sass variables to change at run time. JavaScript cannot read or update
$primary, because it does not exist in the output. If a value must change in the browser, use a CSS custom property. - Editing the compiled CSS. The output is overwritten on the next compile. Treat it as a build artifact and make changes in the source.
- Over-nesting. Nesting copies the full selector chain into the output. Deeply nested Sass produces long, highly specific selectors that are hard to override. Keep nesting shallow, commonly no more than three levels.
- Assuming Sass makes CSS smaller. Mixins and loops can generate a lot of CSS from a few lines of source. Source length and output size are different things, so check the compiled file.
- Debugging in the wrong place. Browser developer tools show the compiled CSS. Ask the compiler for source maps so the tools can point back to the original
.scssline.
When Sass is a good fit, and when it is not
Sass pays off when a project has many files, a shared design system, repeated patterns, or generated utilities. It is also a good fit when several people edit the same stylesheets and need predictable structure.
It is less useful for a single small page with a few dozen rules. Native custom properties and native nesting may be enough there, and skipping a build step is a real saving. Choose Sass because the project needs variables, reuse, or logic at compile time, not out of habit.
What comes next
The next chapter installs Dart Sass and compiles a file from the command line, including watch mode. After that, the tutorial goes through SCSS syntax, variables, nesting, mixins, functions, maps, control directives, and the module system, and finishes with project organization and migrating from @import.
Exercise
A teammate wants a "dark mode" toggle. They plan to define $bg: white; in Sass and have JavaScript change it to black when the user clicks a button. Why will this not work, and what should they use instead?
Show answer
Sass variables are replaced with their values during compilation, so $bg does not exist in the CSS the browser loads, and JavaScript cannot see or change it. They should define a CSS custom property (for example --bg) and change its value at run time, for instance by switching a class or attribute on the root element. Sass can still help by generating those custom property declarations from a map.
Exercise
You rename styles.css to styles.scss and compile it. Will it compile, and why? Then, what file should your HTML link to?
Show answer
Yes, it will compile, because Sass is a superset of CSS and every valid CSS file is valid SCSS. The output is equivalent CSS. The HTML must link to the compiled .css file, not the .scss source, because browsers cannot read SCSS.