Using Conditional Mixins with @if in Sass
📖 Scenario: You are creating a simple button style in Sass that changes its background color based on a condition.
🎯 Goal: Build a Sass mixin called button-style that uses @if to set the background color to blue if the parameter $primary is true, or gray if false.
📋 What You'll Learn
Create a mixin named
button-style with a parameter $primaryUse
@if inside the mixin to check if $primary is trueSet background color to
blue if $primary is trueSet background color to
gray if $primary is falseApply the mixin to two button classes:
.btn-primary with $primary: true and .btn-secondary with $primary: false💡 Why This Matters
🌍 Real World
Conditional mixins help you write reusable styles that change based on input, making your CSS cleaner and easier to maintain.
💼 Career
Front-end developers often use Sass mixins with conditions to create flexible design systems and theme components efficiently.
Progress0 / 4 steps