Sub topic 1
What Sass is and why use it
Sass is a CSS preprocessor. You write styles in a Sass file, and a compiler turns that file into plain CSS that browsers can read. Browsers never see Sass. They only see the CSS the compiler produces.
Prerequisites
This chapter assumes you already know:
- Core CSS: selectors, properties, the cascade, and media queries.
- Basic command-line use.
- How to link a stylesheet from an HTML page.
You do not need to install anything yet. The next chapter covers installation.
The working model
Sass adds features to CSS that CSS did not have, or did not have in a usable form, when Sass was created. These features include variables, nesting, mixins, functions, and a module system for splitting code across files.
The most common Sass syntax is SCSS. It is a superset of CSS, which means every valid CSS file is also a valid SCSS file. You can rename styles.css to styles.scss and it compiles unchanged. Then you add Sass features one at a time.
The build step is the key point. Sass runs at build time, on your machine or in your build pipeline. The browser receives only the compiled output.
A small example
Here is SCSS that uses a variable and nesting:
$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);
}The variable $primary is replaced by its value during compilation. The & stands for the parent selector, so &:hover becomes .card:hover. Later chapters explain both features in detail.
Why use Sass
- Less repetition. A value such as a brand color is defined once and reused. Changing it in one place updates every rule that uses it.
- Reusable patterns. Mixins and functions package repeated blocks and calculations.
- Organized code. You can split a large stylesheet into many small files and combine them into one CSS file at build time.
- Generated rules. Loops and maps can produce many similar rules, such as one utility class per color, from a short definition.
Where Sass does not help
Modern CSS now has custom properties (--name variables) and native nesting. Those are evaluated by the browser, so they can change at runtime, for example when a user switches themes. Sass variables are fixed at compile time and cannot do that.
Sass is not a replacement for CSS knowledge. It adds a build step, and you must debug the compiled output when the result is not what you expected. For a small site with a few dozen rules, the extra tooling may not be worth it.
Exercise
A teammate says "the browser reads our .scss files and applies the variables." What is wrong with this statement, and what does the browser actually receive?
Show answer
Browsers do not understand Sass. The Sass compiler runs at build time and replaces variables, nesting, and other Sass features with plain CSS. The browser receives only the compiled .css file.
Exercise
Why can a Sass variable not be used to switch a site between light and dark themes after the page has loaded?
Show answer
Sass variables are resolved during compilation, so their values are written into the CSS as fixed values. Changing a value at runtime requires a CSS custom property, which the browser evaluates while the page is running.