0
0
SASSmarkup~8 mins

Theming with mixins in SASS - Performance & Optimization

Choose your learning style9 modes available
Performance: Theming with mixins
MEDIUM IMPACT
This affects CSS generation size and browser style recalculation speed when themes change.
Applying multiple themes using mixins in Sass
SASS
:root {
  --color-primary: red;
  --color-secondary: blue;
}

@mixin theme-colors {
  color: var(--color-primary);
  background-color: var(--color-secondary);
}

.button {
  @include theme-colors;
}

/* Changing theme only updates CSS variables, no extra CSS generated */
Uses CSS variables with mixins to keep CSS size small and enable fast theme switching without style recalculation.
📈 Performance GainSingle CSS block reused; theme changes only update variables, reducing style recalculations and CSS size.
Applying multiple themes using mixins in Sass
SASS
@mixin theme-colors($primary, $secondary) {
  color: $primary;
  background-color: $secondary;
}

.button {
  @include theme-colors(red, blue);
}

.card {
  @include theme-colors(green, yellow);
}

/* Repeated mixin calls generate duplicated CSS for each theme */
Each mixin call generates separate CSS rules, increasing CSS size and causing longer style recalculations.
📉 Performance CostAdds extra CSS bytes proportional to number of theme variations; increases style recalculation time.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Repeated mixin calls with hardcoded colorsNoneNoneHigh due to large CSS and style recalculations[X] Bad
Mixin using CSS variables for colorsNoneNoneLow, minimal CSS size and fast style recalculation[OK] Good
Rendering Pipeline
Mixins generate CSS during build time. Large repeated mixin usage increases CSS size, affecting style calculation and paint. Using CSS variables with mixins reduces CSS size and speeds up style recalculation during theme changes.
Style Calculation
Layout
Paint
⚠️ BottleneckStyle Calculation due to large CSS and frequent theme changes
Core Web Vital Affected
LCP
This affects CSS generation size and browser style recalculation speed when themes change.
Optimization Tips
1Avoid repeating mixins with hardcoded values to prevent CSS bloat.
2Use CSS variables inside mixins for flexible, small CSS and fast theme switching.
3Test theme changes in DevTools Performance panel to catch style recalculation bottlenecks.
Performance Quiz - 3 Questions
Test your performance knowledge
What is a main performance benefit of using CSS variables inside Sass mixins for theming?
AIncreases CSS specificity for better style overrides
BReduces CSS file size and speeds up style recalculation on theme changes
CAutomatically caches CSS for offline use
DPrevents any reflows during page load
DevTools: Performance
How to check: Record a performance profile while switching themes or loading the page. Look for long style recalculation or layout times.
What to look for: High 'Recalculate Style' or 'Layout' times indicate inefficient theming; low times indicate optimized CSS variable usage.