Sass/SCSS Introduction and Setup

Module 7: CSS Preprocessors & Frameworks

What is Sass/SCSS?

Sass (Syntactically Awesome Style Sheets) is a CSS preprocessor that extends the capabilities of standard CSS. It introduces programming features like variables, nesting, mixins, and functions that make CSS more maintainable, reusable, and easier to organize for large projects.

Sass vs. SCSS: There are two syntaxes available:

  • Sass (original syntax) - Uses indentation instead of brackets and semicolons
  • SCSS (Sassy CSS) - Uses the same syntax as CSS with brackets and semicolons

SCSS is more widely used as it's compatible with regular CSS and has a gentler learning curve.

Sass Syntax (indented)

nav
  ul
    margin: 0
    padding: 0
    list-style: none
  
  li
    display: inline-block
  
  a
    display: block
    padding: 6px 12px
    text-decoration: none

SCSS Syntax

nav {
  ul {
    margin: 0;
    padding: 0;
    list-style: none;
  }
  
  li {
    display: inline-block;
  }
  
  a {
    display: block;
    padding: 6px 12px;
    text-decoration: none;
  }
}

Why Use Sass?

flowchart TB A[Regular CSS] --> B[CSS Problems] B --> C1[Repetition] B --> C2[Limited Organization] B --> C3[No Logic/Calculations] B --> C4[No Reusable Components] D[Sass/SCSS] --> E[Sass Solutions] E --> F1[Variables] E --> F2[Nesting & Partials] E --> F3[Functions & Operators] E --> F4[Mixins & Extensions]

Benefits of Using Sass

How Sass Works

flowchart LR A[Write Sass/SCSS] --> B[Sass Compiler] B --> C[Regular CSS] C --> D[Browser Rendering] style B fill:#f9f,stroke:#333,stroke-width:2px

Browsers don't understand Sass code directly. The Sass preprocessor converts your Sass/SCSS files into standard CSS that browsers can interpret. This conversion process is called "compilation" or "preprocessing".

Analogy: Sass is like a Kitchen Appliance

Think of Sass as a specialized kitchen appliance, like a food processor. Raw ingredients (your Sass code) go in, and a finished dish (CSS) comes out. The browser, like your dinner guests, only sees and consumes the final product, not all the special techniques and tools you used to prepare it.

Setting Up Sass in Your Projects

Method 1: Install Sass Globally (Command Line)

Install Sass globally on your system:

npm install -g sass

Then compile your Sass files:

sass input.scss output.css

Watch mode (automatically compiles when files change):

sass --watch input.scss output.css

Watch an entire directory:

sass --watch scss/:css/

Method 2: Project-Based Installation (Node.js)

Add Sass as a development dependency in your project:

npm install sass --save-dev

Add a script to your package.json:

{
  "scripts": {
    "sass": "sass src/scss:dist/css --watch"
  }
}

Run the script:

npm run sass

Method 3: Build Tools Integration

Sass can be integrated with various build tools:

Method 4: VS Code Extensions

For quick development and learning, use VS Code extensions:

Basic Project Structure

A typical Sass project structure might look like this:

project/
├── src/
│   ├── scss/
│   │   ├── main.scss          # Main file that imports all partials
│   │   ├── _variables.scss    # Variables (colors, fonts, etc.)
│   │   ├── _base.scss         # Base styles (resets, typography, etc.)
│   │   ├── _layout.scss       # Layout styles
│   │   ├── _components.scss   # Reusable components
│   │   └── _utilities.scss    # Utility classes
│   └── index.html
└── dist/
    └── css/
        └── main.css           # Compiled CSS output

Convention: Partial files start with an underscore (_) to indicate they shouldn't be compiled directly.

Real-World Example: Creating a Theme System

Let's look at how a company like Netflix might use Sass to manage multiple themes across their platform:

1. Theme Variables (_themes.scss)

// Default dark theme
$themes: (
  dark: (
    bg-primary: #141414,
    bg-secondary: #181818,
    text-primary: #ffffff,
    text-secondary: #b3b3b3,
    accent: #e50914
  ),
  light: (
    bg-primary: #f5f5f5,
    bg-secondary: #ffffff,
    text-primary: #000000,
    text-secondary: #757575,
    accent: #e50914
  ),
  kids: (
    bg-primary: #0e4d92,
    bg-secondary: #1e5b98,
    text-primary: #ffffff,
    text-secondary: #cccccc,
    accent: #ffde00
  )
);

2. Theme Access Function (_functions.scss)

@function theme-value($theme-name, $key) {
  $theme-map: map-get($themes, $theme-name);
  @return map-get($theme-map, $key);
}

3. Theme Mixin (_mixins.scss)

@mixin themed($theme-name) {
  .theme-#{$theme-name} & {
    @content;
  }
}

4. Usage in Components (_card.scss)

.card {
  border-radius: 4px;
  overflow: hidden;
  transition: transform 0.3s;
  
  @include themed('dark') {
    background-color: theme-value('dark', 'bg-secondary');
    color: theme-value('dark', 'text-primary');
  }
  
  @include themed('light') {
    background-color: theme-value('light', 'bg-secondary');
    color: theme-value('light', 'text-primary');
  }
  
  @include themed('kids') {
    background-color: theme-value('kids', 'bg-secondary');
    color: theme-value('kids', 'text-primary');
    border: 3px solid theme-value('kids', 'accent');
  }
  
  &:hover {
    transform: scale(1.03);
  }
}

Practice Activities

  1. Setup Challenge: Set up a Sass compilation environment using one of the methods described above.
  2. Conversion Exercise: Take a small CSS file from a previous project and convert it to SCSS, organizing it with proper nesting.
  3. Project Structure: Create a basic project folder structure with appropriate partial files.
  4. Theme Exploration: Try to recreate a simplified version of the theme example above using your favorite website or app's color scheme.

Additional Resources

Key Takeaways

Next Lecture

In our next session, we'll explore Sass variables, nesting techniques, and partials in depth, showing how they can dramatically improve your CSS workflow.