Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

CSS Pre‑processors – Sass & | CSS Basics to Advanced lesson | BraveProgrammer
  1. Courses
  2. /
  3. CSS Basics to Advanced

Lesson 40 of 50

·css

CSS Pre‑processors – Sass & Less

10 mins

Course progress80%

What you'll learn

Keep these ideas in mind as you work through the lesson.

3 points
  • Variables (`$color`) avoid hard‑coding values.
  • Mixins (`@mixin`) bundle reusable blocks of CSS.
  • Use **autoprefixer** after compilation to add vendor prefixes.

Pre‑processors give you programming constructs (variables, nesting, mixins) that plain CSS lacks.

Sass (SCSS syntax) – the industry standard

/* _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); }

Key Features

  • Variables – $color-primary for design tokens.
  • Nesting – Mirror the HTML hierarchy (nav ul li a {}) for readability.
  • Mixins – Re‑usable blocks of CSS (@include clearfix;).
  • Functions – lighten(), darken(), percentage(), custom functions via @function.
  • Control Directives – @if, @for, @each, @while for dynamic generation.

Compiling

  • CLI – sass src/main.scss dist/main.css --style=compressed
  • Build Tools – Webpack (sass-loader), Gulp (gulp-sass), or Parcel (automatic).
  • Source Maps – Enable sourceMap:true for debugging original SCSS in DevTools.

Less – Similar but uses @ for variables

@primary:#0066cc;
.button {
  background:@primary;
  &:hover { background:darken(@primary,10%); }
}

Pro Tip – Use @use instead of the older @import in Sass.

@use loads a file once, namespaces its variables, and prevents accidental variable leakage.

Post‑Processing

After compiling, run Autoprefixer (PostCSS) to add vendor prefixes automatically, ensuring cross‑browser compatibility.

When Not to Use a Pre‑processor

  • Small static sites where plain CSS suffices.
  • Projects that rely heavily on CSS‑in‑JS (e.g., styled‑components) – you may duplicate functionality.

Accessibility Reminder

Never use mixins that hide content (display:none) without also providing an accessible alternative. Keep the focus order in mind when generating styles programmatically.

Make it stickPractice what you've learned1 quiz · 6 practical tests · Optional activities from CSS Basics to AdvancedBrowse activities
Previous lessonCSS Architecture – BEM, OOCSS, SMACSS, ITCSSContinue to lesson 41PostCSS & AutoprefixerBack to CSS Basics to Advanced curriculum