What does @if do in SCSS?
@if in SCSS is a conditional branching directive that lets you execute different blocks of code depending on a given condition. It resembles a conditional operator in programming languages and is applied inside the preprocessor before compilation, affecting which styles end up in the final CSS.
Example
scss
$theme: dark;
@if $theme == dark {
body {
background: #111;
color: #fff;
}
} @else {
body {
background: #fff;
color: #111;
}
}Only one variant makes it into the final CSS: the one whose block passed the condition.
Where this is useful
- Dark/light theme
- Generating different grids for different parameters
- Creating responsive styles for project configurations
- Optimizing the final CSS by excluding unneeded code
The key meaning of @if
It lets you add simple logic to SCSS, making styles more dynamic and manageable, without duplicating identical blocks.
Ready to keep going in this same format. Let me know when you ask the next question.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.