Building Your Learning Module...
Getting things ready for you!
Find videos you like?
Save to resource drawer for future reference!
Add features CSS doesn't have (yet)
Write less code with variables and mixins
Organize CSS better with nesting
Popular ones: Sass, Less, Stylus
Compile to regular CSS for browsers
Make maintaining CSS easier
The most popular. Two syntaxes: Sass (no brackets) and SCSS (looks like CSS).
Closer to CSS syntax. Used by Bootstrap (older versions).
Most flexible syntax. Optional brackets and semicolons.
💡 Recommendation: Use Sass (SCSS syntax) - it's the most popular, has the best tooling, and looks almost like regular CSS so it's easy to learn!
Store values and reuse them
$primary-color: #3b82f6;Write CSS inside other CSS
.card { .title { } }Reusable blocks of styles
@mixin flex-center { }Calculate values dynamically
darken($color, 10%)/* Variables in Sass - Start with $ */
$primary-color: #3b82f6;
$secondary-color: #6b7280;
$spacing-unit: 8px;
$border-radius: 8px;
$font-main: system-ui, sans-serif;
/* Use them anywhere */
.button {
background: $primary-color;
padding: $spacing-unit * 2; /* Math works! */
border-radius: $border-radius;
font-family: $font-main;
}
.card {
border: 1px solid $secondary-color;
padding: $spacing-unit * 3;
border-radius: $border-radius;
}
/* COMPILES TO: */
.button {
background: #3b82f6;
padding: 16px;
border-radius: 8px;
font-family: system-ui, sans-serif;
}
.card {
border: 1px solid #6b7280;
padding: 24px;
border-radius: 8px;
}$var) are compiled away and become fixed values. CSS custom properties (--var) exist in the browser and can change dynamically. Use Sass variables for build-time values, CSS variables for runtime values!/* Nesting in Sass */
.card {
padding: 20px;
border: 1px solid #e5e7eb;
/* Nested element */
.card-title {
font-size: 20px;
font-weight: 600;
margin-bottom: 10px;
}
.card-body {
font-size: 14px;
color: #6b7280;
}
/* Nested pseudo-class with & */
&:hover {
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}
/* Nested modifier with & */
&--featured {
border-color: #3b82f6;
border-width: 2px;
}
}
/* COMPILES TO: */
.card {
padding: 20px;
border: 1px solid #e5e7eb;
}
.card .card-title {
font-size: 20px;
font-weight: 600;
margin-bottom: 10px;
}
.card .card-body {
font-size: 14px;
color: #6b7280;
}
.card:hover {
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}
.card--featured {
border-color: #3b82f6;
border-width: 2px;
}.nav .menu .item .link is too specific and hard to override. Keep it shallow for maintainable CSS!/* Define a mixin */
@mixin flex-center {
display: flex;
justify-content: center;
align-items: center;
}
/* Mixin with parameters */
@mixin button($bg-color, $text-color) {
background: $bg-color;
color: $text-color;
padding: 12px 24px;
border: none;
border-radius: 6px;
cursor: pointer;
&:hover {
background: darken($bg-color, 10%);
}
}
/* Use mixins */
.modal {
@include flex-center;
height: 100vh;
}
.btn-primary {
@include button(#3b82f6, white);
}
.btn-success {
@include button(#10b981, white);
}
/* COMPILES TO: */
.modal {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.btn-primary {
background: #3b82f6;
color: white;
padding: 12px 24px;
border: none;
border-radius: 6px;
cursor: pointer;
}
.btn-primary:hover {
background: #2563eb;
}
.btn-success {
background: #10b981;
color: white;
padding: 12px 24px;
border: none;
border-radius: 6px;
cursor: pointer;
}
.btn-success:hover {
background: #059669;
}/* Color functions */
$primary: #3b82f6;
.button {
background: $primary;
}
.button:hover {
background: darken($primary, 10%); /* Darker blue */
}
.button-light {
background: lighten($primary, 20%); /* Lighter blue */
}
.button-transparent {
background: rgba($primary, 0.5); /* 50% transparent */
}
/* Math functions */
$base-spacing: 8px;
.container {
padding: $base-spacing * 2; /* 16px */
margin: $base-spacing * 3; /* 24px */
}
/* String functions */
$font-path: "/fonts";
@font-face {
src: url(#{$font-path}/main.woff); /* Interpolation */
}
/* List functions */
$colors: red, green, blue;
.box {
color: nth($colors, 2); /* Gets 'green' */
}// Variables
$primary: #3b82f6;
$spacing: 8px;
// Mixin
@mixin card-base {
padding: $spacing * 3;
border-radius: $spacing;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
// Nested styles
.card {
@include card-base;
background: white;
&__title {
font-size: 20px;
color: darken($primary, 20%);
margin-bottom: $spacing * 2;
}
&--featured {
border: 2px solid $primary;
}
}.card {
padding: 24px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
background: white;
}
.card__title {
font-size: 20px;
color: #1e40af;
margin-bottom: 16px;
}
.card--featured {
border: 2px solid #3b82f6;
}Modern build tools have Sass built-in:
.css to .scsssass-loaderInstall and compile manually:
npm install -g sass
sass styles.scss styles.css --watchInstall "Live Sass Compiler" extension - automatically compiles on save
💡 Modern Alternative: CSS now has custom properties (variables), nesting is coming soon, and PostCSS can handle most preprocessing needs. Consider if you really need Sass, or if modern CSS + PostCSS is enough!
Thousands of lines of CSS to organize
Reusable components and variables
Multiple developers need consistency
Multiple color schemes or brand variations
$primary-button-color not $blue