Using @else and @else if branches in Sass
📖 Scenario: You are creating a simple Sass stylesheet for a website that changes the background color of a button based on its type. The button can be primary, secondary, or danger. If the type is not one of these, it should have a default background color.
🎯 Goal: Build a Sass code snippet that uses @if, @else if, and @else branches to set the background color of a button based on its $type variable.
📋 What You'll Learn
Create a Sass variable called
$type with the value 'primary'.Create a variable called
$primary-color with the value #007bff.Create a variable called
$secondary-color with the value #6c757d.Create a variable called
$danger-color with the value #dc3545.Use
@if, @else if, and @else to set a variable $bg-color based on $type.Write a CSS rule for
.button that sets background-color to $bg-color.💡 Why This Matters
🌍 Real World
Buttons on websites often change color based on their purpose, like primary actions or warnings. Using Sass conditionals helps manage these styles cleanly.
💼 Career
Knowing how to use @if, @else if, and @else in Sass is important for frontend developers to write maintainable and flexible stylesheets.
Progress0 / 4 steps