Sass Control Directives: Logic in Your Stylesheets
Sass control directives bring programming logic—if/else, loops—into your CSS preprocessor. Use them to generate component variations or utility classes without repetitive code.
Why it exists
CSS is declarative, not procedural. This makes writing repetitive styles, like twelve grid columns or five button color variations, tedious and error-prone. Sass control directives exist to introduce logic and automation, reducing manual repetition and making stylesheets more maintainable at scale.
The mental model
Treat Sass control directives like the basic control flow from any programming language, but applied to generating CSS rules. Instead of writing out every single class variation by hand, you write a template and tell Sass how to loop or branch to create the final, static CSS output.
How it works
Sass provides four main directives that are processed at compile time. First, @if and @else conditionally include a block of styles based on a boolean expression. Second, @for loops a set number of times, perfect for creating numbered utilities like col-1, col-2. Third, @each iterates over items in a list or map, ideal for generating theme variations from a color map. Finally, @while continues to output a block of styles as long as a condition is true, though it's less common than @for or @each.
When to use it
Use control directives for tasks with clear, repetitive patterns. Good examples include generating a set of spacing utilities from a list of values, creating multiple color theme classes for a component based on a map of theme names to colors, or building a responsive grid system with @for loops. They are most powerful inside mixins to create configurable, reusable style patterns.
When not to use it
Avoid using control directives for logic that could be handled better in the browser. For dynamic theming that a user can change, CSS Custom Properties (variables) are a better fit as they don't require recompiling. Also, avoid overly complex, nested loops. If your Sass logic becomes hard to read, it's a sign you are over-engineering a problem that could be solved with simpler, more composable CSS classes.
One canonical example
A common use case is generating modifier classes for button colors from a map. Imagine a map theme-colors: ("primary": blue, "danger": red). You can use an @each loop like @each name, color in theme-colors { .btn-#{name} { background-color: color; } }. This single block of Sass compiles to .btn-primary { background-color: blue; } and .btn-danger { background-color: red; }, automating the creation of variants.
Interview question
Which scenario is generally NOT an appropriate application for Sass control directives?
- a.Automating the creation of a range of spacing utility classes
- b.Implementing user-selectable dynamic themes in a web applicationCorrect
- c.Constructing a responsive grid system with iterative column definitions
- d.Generating component variations from a predefined list or map
Why? this is the answer
Sass control directives are processed at compile time to generate static CSS. Dynamic theming that a user can change at runtime is better handled by CSS Custom Properties (variables) because they can be manipulated directly in the browser without requiring recompilation.
Just read this? Test yourself on what you have been reading.
Read the original → sass-lang.com
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.
See open roles