Sass / SCSS

Sass (Syntactically Awesome Style Sheets) is a CSS preprocessor — a language that extends CSS with programming constructs and compiles down to plain CSS that browsers can read. SCSS (Sassy CSS) is its dominant syntax variant, using standard CSS brace/semicolon notation plus Sass features; the Natours project uses SCSS exclusively.


Why a Preprocessor?

Plain CSS has no variables, no functions, no imports that consolidate at build time, and no way to nest selectors. As stylesheets grow, they become hard to maintain. Sass solves this with:

FeatureProblem it solves
Variables ($color-primary)Eliminates magic-value duplication across files
NestingCo-locates child rules with their parent context
Partials + @importSplits one large stylesheet into organised topic files
Mixins (@mixin / @include)Reusable rule blocks, optionally parameterised
Functions (@function)Computed values (e.g. rem() conversion)
OperatorsArithmetic directly in style values
Placeholder selectors (%)Shared rules without emitting extra classes

SCSS vs Indented Sass

SCSSSass (indented)
SyntaxCSS-like, braces + semicolonsPython-like, whitespace-sensitive
File extension.scss.sass
AdoptionNear-universalLegacy / niche preference

Natours uses .scss throughout.


Key Features in the Natours Project

Variables

Defined in sass/abstracts/_variables.scss. Design tokens for colours, typography, and grid dimensions:

$color-primary: #55c57a;
$default-font-size: 1.6rem;
$grid-width: 114rem;

Nesting

Selectors and pseudo-classes nested inside their parent block, reducing repetition and matching bem-methodology structure visually:

.navigation {
    &__nav { … }        // compiles to .navigation__nav
    &__item { … }       // compiles to .navigation__item
    &:hover { … }
}

Mixins with Arguments

The respond() mixin in _mixins.scss centralises all media query breakpoints:

@mixin respond($breakpoint) {
    @if $breakpoint == phone {
        @media only screen and (max-width: 37.5em) { @content };
    }
    // …
}
 
// Usage:
.card { @include respond(tab-port) { width: 100%; } }

Breakpoints use em units (not px) so they respect user-set browser font sizes.

Partials and the 7-1 Architecture

See css-architecture for the full 7-1 pattern. Each partial is prefixed with _ (e.g. _button.scss) to signal it should not compile standalone. sass/main.scss is the single entry point that @imports all partials in dependency order:

abstracts → base → components → layout → pages

Build Pipeline in Natours

node-sass main.scss → style.comp.css        # compile
concat icon-font.css + style.comp.css       # merge icon font
postcss --use autoprefixer → style.prefix.css  # vendor prefixes
node-sass --output-style compressed → style.css  # minify

This four-step pipeline (managed via npm-run-all) produces a single optimised style.css for production.


Tooling

ToolVersion in NatoursRole
node-sass^4.5.3Sass → CSS compilation
postcss-cli^4.1.1PostCSS runner
autoprefixer^7.1.4Adds -webkit-, -moz- vendor prefixes
concat^1.0.3Concatenates CSS files
npm-run-all^4.1.1Runs multiple npm scripts in parallel/sequence

See Also