Course Outline (Part 41)

Welcome to Part 41 of the CSS Mastery & Responsive Layouts Course. In this part, we will explore Sass/SCSS Preprocessing.

As codebases grow, plain CSS can become repetitive and difficult to organize. CSS preprocessors like Sass (Syntactically Awesome Stylesheets) solve this by extending CSS with programming-like features such as nesting, variables, reusable mixins, functions, and loop iterations. The preprocessor parses this extended code and compiles it down into standard, browser-compatible CSS.


Chapter 204: Sass/SCSS Basics & Variables

Sass supports two syntaxes: .sass (older, indentation-based) and .scss (Sassy CSS, which uses standard CSS curly braces and semicolons). We will focus on the popular .scss format.

◆ Defining Variables in SCSS

Variables in Sass start with a dollar sign $. They store colors, fonts, widths, or any other reusable CSS value:

$primary-color: #3b82f6;
$padding-base: 16px;
$font-stack: 'Inter', sans-serif;

body {
  background-color: $primary-color;
  padding: $padding-base * 2; /* Supports basic mathematical equations */
  font-family: $font-stack;
}

Chapter 205: Nesting Rules & Parent Referencing

Nesting allows you to write child selectors inside parent selectors, mirroring the structure of your HTML markup.

❖ Nesting Selectors

This keeps styles for a single component organized in one block:

.nav-bar {
  background-color: #333;
  padding: 10px;
  
  .nav-list {
    display: flex;
    list-style: none;
    
    .nav-item {
      margin-right: 15px;
    }
  }
}

◆ The Parent Referencer (&)

The ampersand & references the parent selector. It is extremely useful for hover states, active classes, or BEM element names:

.button {
  background-color: #3b82f6;
  color: white;
  
  &:hover {
    background-color: #2563eb; /* Compiles to .button:hover */
  }
  
  &--large {
    padding: 15px 30px; /* Compiles to .button--large */
  }
}

Chapter 206: Mixins, Functions, and Iteration

Mixins and functions allow you to write reusable blocks of code.

❖ Creating and Including Mixins

Mixins allow you to define a set of reusable CSS declarations. They can also take arguments:

@mixin flex-center($direction: row) {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: $direction;
}

/* Include it in your selectors */
.card-icon-wrapper {
  @include flex-center;
}
.sidebar-list {
  @include flex-center(column);
}

◆ Math and Loops

Sass supports standard loops to generate utility classes:

/* Generate column padding utilities from 1 to 3 */
@for $i from 1 through 3 {
  .p-#{$i} {
    padding: $i * 10px;
  }
}

Chapter 207: Sass Practice & Self-Check

◆ Practice Exercises

  1. Exercise 1: Theme Variable Map: Create a SCSS map containing primary, secondary, and success colors. Define a mixin that applies these border and text colors, and use an @each loop to output buttons styled for each theme.

❖ Self-Check Questions

  1. What does Sass stand for?
    • Answer: Syntactically Awesome Stylesheets.
  2. Which Sass syntax uses standard CSS braces and semicolons?
    • Answer: SCSS (.scss).
  3. How are variables declared in SCSS?
    • Answer: Using a dollar sign prefix, e.g. $primary-color.
  4. What character references the parent selector inside nested Sass blocks?
    • Answer: The ampersand (&).
  5. What directive creates a reusable chunk of styles that can accept arguments?
    • Answer: @mixin.
  6. How do you apply a mixin inside a selector block?
    • Answer: @include mixin-name;.
  7. What is the difference between a Sass mixin and a Sass function?
    • Answer: A mixin outputs CSS declarations directly, while a function returns a single calculated value via @return.
  8. What directive imports other SCSS partials into a stylesheet?
    • Answer: @use (which replaces legacy @import).
  9. What makes a SCSS file a partial that is not compiled into its own CSS file?
    • Answer: Starting the filename with an underscore (e.g. _colors.scss).
  10. Can browsers read SCSS files directly?
    • Answer: No, SCSS files must be compiled into standard CSS files before they can be loaded by web browsers.

Discussion

Loading comments...