Lesson 40 of 50
cssCSS Pre‑processors – Sass & Less
10 mins
Course progress80%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 40 of 50
css10 mins
Keep these ideas in mind as you work through the lesson.
Pre‑processors give you programming constructs (variables, nesting, mixins) that plain CSS lacks.
/* _variables.scss */
$primary:#0066cc;
$spacing:1rem;
/* _mixins.scss */
@mixin button($bg) {
background:$bg; color:#fff; border:none; padding:$spacing; cursor:pointer;
&:hover { background:darken($bg,10%); }
}
/* main.scss */
@use 'variables' as *;
@use 'mixins' as *;
.btn-primary { @include button($primary); }
$color-primary for design tokens.nav ul li a {}) for readability.@include clearfix;).lighten(), darken(), percentage(), custom functions via @function.@if, @for, @each, @while for dynamic generation.sass src/main.scss dist/main.css --style=compressedsass-loader), Gulp (gulp-sass), or Parcel (automatic).sourceMap:true for debugging original SCSS in DevTools.@ for variables@primary:#0066cc;
.button {
background:@primary;
&:hover { background:darken(@primary,10%); }
}
@use instead of the older @import in Sass.@use loads a file once, namespaces its variables, and prevents accidental variable leakage.Never use mixins that hide content (display:none) without also providing an accessible alternative. Keep the focus order in mind when generating styles programmatically.