Sass full reference: variables ($), nesting, mixins, functions, loops (@for/@each), if/else guards, @extend inheritance, math operations, modules (@use/@forward), Tailwind integration, and production architecture patterns.
Sass 2026: Syntactically Awesome Style Sheets Complete Guide 🚀
- Sass (Syntactically Awesome Style Sheets) transforms CSS into a full programming language with variables, nesting, mixins, functions, control directives (@if/@for/@each), inheritance (@extend), and modular imports (@use/@forward).
- Two syntaxes: SCSS (CSS-compatible) and Sass (indented). Powers 95% enterprise design systems.
🎯 Sass vs CSS vs PostCSS vs Tailwind
| Feature | CSS | Sass/SCSS | PostCSS | Tailwind |
|---|---|---|---|---|
| Variables | CSS Vars | Full | Plugins | Design tokens |
| Nesting | @nest | Full | postcss-nesting | ❌ |
| Mixins | ❌ | Yes | Plugins | ❌ |
| Functions | calc() | Full | Plugins | ❌ |
| Modules | ❌ | @use/@forward | Plugins | CSS layers |
| Learning Curve | Easy | Medium | Hard | Hard |
🏗️ SCSS Syntax (CSS-Superset) vs Sass (Indented)
/* SCSS (CSS compatible) */
$primary: #3b82f6;
$radius: 0.5rem;
.button {
background: $primary;
border-radius: $radius;
&:hover {
background: darken($primary, 10%);
}
}
/* Sass (indented syntax) */
$primary: #3b82f6
$radius: 0.5rem
.button
background: $primary
border-radius: $radius
&:hover
background: darken($primary, 10%)
🚀 CORE SASS FEATURES
1. VARIABLES + MAPS (Design Tokens)
/* Global design system */
$colors: (
primary: #3b82f6,
success: #10b981,
danger: #ef4444,
gray: (
50: #f9fafb,
900: #111827
)
);
$spacing: (
xs: 0.25rem,
sm: 0.5rem,
md: 1rem,
lg: 1.5rem
);
/* Usage */
.btn {
background: map-get($colors, primary);
padding: map-get($spacing, md) map-get($spacing, lg);
&--success {
background: map-get($colors, success);
}
}
2. NESTING (DRY Selectors)
.nav {
background: #111827;
&__list {
display: flex;
list-style: none;
&__item {
padding: 1rem;
&--active {
background: rgba(59, 130, 246, 0.1);
}
&:hover {
background: rgba(255, 255, 255, 0.05);
}
}
&__link {
color: white;
text-decoration: none;
&:hover {
color: map-get($colors, primary);
}
}
}
}
3. MIXINS (Reusable Code Blocks)
@mixin flex-center($direction: row) {
display: flex;
align-items: center;
justify-content: center;
flex-direction: $direction;
}
@mixin button($bg: $primary, $size: md, $rounded: true) {
@include flex-center;
gap: 0.5rem;
padding: if($size == lg, 1.5rem 2.5rem, 1rem 2rem);
background: $bg;
color: white;
border: none;
border-radius: if($rounded, $radius, 0);
cursor: pointer;
transition: all 0.2s;
&:hover {
transform: translateY(-1px);
box-shadow: 0 4px 12px rgba(darken($bg, 20%), 0.3);
}
}
/* Usage */
.primary-btn { @include button(); }
.danger-btn { @include button(map-get($colors, danger), lg); }
.square-btn { @include button($rounded: false); }
4. FUNCTIONS (Custom Logic)
@function spacing($scale) {
@return map-get($spacing, $scale);
}
@function color($name, $shade: 500) {
@if map-has-key($colors, $name) {
@return map-get(map-get($colors, $name), $shade);
} @else {
@warn "Color #{$name} not found";
@return #000;
}
}
@function px-to-rem($px, $base: 16) {
@return #{$px / $base}rem;
}
/* Usage */
.card {
padding: spacing(lg);
border: 1px solid color(gray, 200);
border-radius: px-to-rem(12);
}
5. CONTROL DIRECTIVES (@if/@for/@each)
@if/@else
@mixin triangle($direction, $size, $color) {
width: 0;
height: 0;
@if $direction == up {
border-left: $size solid transparent;
border-right: $size solid transparent;
border-bottom: ($size * 2) solid $color;
} @else if $direction == right {
border-top: $size solid transparent;
border-bottom: $size solid transparent;
border-left: ($size * 2) solid $color;
}
}
@for Loop
/* 12-column grid */
@for $i from 1 through 12 {
.col-#{$i} {
width: percentage($i / 12);
}
.span-#{$i} {
grid-column: span #{$i};
}
}
@each Loop
@each $name, $color in $colors {
.bg-#{$name} {
background: $color;
}
.text-#{$name} {
color: $color;
}
}
6. INHERITANCE (@extend)
/* Base styles */
.btn {
display: inline-block;
padding: 0.75rem 1.5rem;
border: none;
cursor: pointer;
transition: all 0.2s;
}
.btn-primary { @extend .btn; background: map-get($colors, primary); }
.btn-success { @extend .btn; background: map-get($colors, success); }
.btn-danger { @extend .btn; background: map-get($colors, danger); }
🏗️ MODULE SYSTEM (@use/@forward)
styles/
├── abstracts/
│ ├── _variables.scss
│ ├── _mixins.scss
│ └── _functions.scss
├── base/
│ ├── _reset.scss
│ └── _typography.scss
├── components/
│ ├── _button.scss
│ └── _card.scss
└── main.scss
/* abstracts/_variables.scss */
$primary: #3b82f6;
/* abstracts/_mixins.scss */
@use 'variables';
@mixin container {
max-width: 1200px;
margin: 0 auto;
padding: variables.$spacing(md);
}
/* main.scss */
@use 'abstracts/variables';
@use 'abstracts/mixins';
.page {
@include mixins.container;
}
🎨 THEME SYSTEM (Dark/Light Mode)
$themes: (
light: (
bg: #ffffff,
text: #111827,
border: #e5e7eb
),
dark: (
bg: #111827,
text: #f9fafb,
border: #374151
)
);
@mixin theme($name) {
background: map-get(map-get($themes, $name), bg);
color: map-get(map-get($themes, $name), text);
border-color: map-get(map-get($themes, $name), border);
}
:root { @include theme(light); }
@media (prefers-color-scheme: dark) {
:root { @include theme(dark); }
}
🛠️ FRAMEWORK INTEGRATION
Vite + React
// vite.config.ts
export default {
css: {
preprocessorOptions: {
scss: {
additionalData: `@use "@/styles/variables.scss" as *;`
}
}
}
};
Next.js 15
// next.config.js
module.exports = {
sassOptions: {
includePaths: ['./styles'],
prependData: `@use "variables" as *;`
}
};
🎯 PRODUCTION ARCHITECTURE (7-1 Pattern)
- SETTINGS → _variables.scss
- TOOLS → _mixins.scss, _functions.scss
- GENERIC → _reset.scss
- ELEMENTS → _typography.scss
- OBJECTS → _grid.scss
- COMPONENTS → _button.scss, _card.scss
- UTILITIES → _utilities.scss
🚀 COMPLETE PRODUCTION EXAMPLE
@use 'sass:math';
@use 'variables' as v;
@mixin responsive-padding($base: md) {
padding: v.spacing($base);
@media (min-width: 768px) {
padding: v.spacing(lg);
}
}
.card {
@include responsive-padding;
background: white;
border-radius: v.$radius;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
&__header {
@include v.flex-center;
padding: v.spacing(md);
border-bottom: 1px solid v.color(gray, 200);
}
}
📊 SASS vs CSS Bundle Impact
| Metric | CSS | Sass (compiled) |
|---|---|---|
| File Size | 15KB | 12KB |
| Development | Manual | Auto |
| Maintenance | Hard | Easy |
| Consistency | Manual | Automatic |
🎯 PRODUCTION CHECKLIST
-
✅ Variables + Maps (Design tokens)
-
✅ Nesting (DRY selectors)
-
✅ Mixins (Reusable patterns)
-
✅ Functions (Custom logic)
-
✅ Loops (@for/@each utilities)
-
✅ @if/@else (Conditional styles)
-
✅ @extend (Inheritance)
-
✅ @use/@forward (Modules)
-
✅ 7-1 Architecture
-
✅ Theme switching
-
✅ Framework integration
-
Sass 2026 = Production CSS solved. 90% less repetition, design systems, theme switching, zero runtime cost.
-
Single source of truth for colors, spacing, components.
Continue Reading