Creating Conditional Styles Using Control Directives in Sass

Sass (Syntactically Awesome Style Sheets) provides control directives that allow you to implement conditional logic in your stylesheets. The primary directive for creating conditional styles is the @if statement, which enables you to apply styles based on specific conditions. This feature enhances the flexibility and maintainability of your styles. Below, we will explore how to create conditional styles using control directives in Sass.

1. Using @if Statements

The @if statement allows you to define styles that are applied only if a specified condition is true. You can also use the @else directive to define alternative styles if the condition is not met.


$theme: dark; /* Change this variable to 'light' to see the effect */

.button {
@if $theme == light {
background-color: #3498db; /* Light theme color */
color: white;
} @else {
background-color: #2c3e50; /* Dark theme color */
color: white;
}
}

In this example, the background color of the button changes based on the value of the $theme variable. If the theme is set to light, it uses a light blue color; otherwise, it uses a dark color.

2. Nested @if Statements

You can also nest @if statements to create more complex conditions. For example, you might want to check for multiple themes:


$theme: blue;

.button {
@if $theme == light {
background-color: #3498db; /* Light theme color */
color: white;
} @else if $theme == dark {
background-color: #2c3e50; /* Dark theme color */
color: white;
} @else if $theme == blue {
background-color: #2980b9; /* Blue theme color */
color: white;
} @else {
background-color: gray; /* Default color */
color: black;
}
}

In this example, the button's background color changes based on the value of the $theme variable, with multiple conditions checked in sequence.

3. Using @if with Variables

In addition to checking for specific values, you can use @if to evaluate variables or expressions. For example, you can change styles based on a numeric value:


$font-size: 18px;

body {
@if $font-size > 20px {
font-size: 1.5em; /* Larger font size */
} @else {
font-size: 1em; /* Default font size */
}
}

In this example, the font size of the body is determined by the value of the $font-size variable. If the font size is greater than 20 pixels, it applies a larger size; otherwise, it defaults to a standard size.

4. Combining @if with Loops

You can also combine @if statements with loops to create more dynamic styles. For example, you might want to generate classes based on a list of colors:


$colors: (red, green, blue);

@each $color in $colors {
.bg-#{$color} {
@if $color == red {
background-color: red; /* Specific style for red */
} @else {
background-color: $color; /* Default for other colors */
}
}
}

In this example, the loop generates classes for each color in the list, applying a specific style for red while using the default color for others.

5. Benefits of Conditional Styles

  • Dynamic Styling: Conditional styles allow you to create styles that adapt based on variables or conditions, enhancing the flexibility of your stylesheets.
  • Cleaner Code: By using conditionals, you can avoid repetitive code and keep your styles organized.
  • Improved Maintainability: Conditional logic makes it easier to manage styles, especially in larger projects where styles may need to change based on different states or themes.

6. Conclusion

Using control directives like @if in Sass allows developers to create conditional styles that enhance the adaptability and maintainability of their stylesheets. By leveraging these directives, you can implement complex styling logic that responds to various conditions, making your CSS more dynamic and efficient. Understanding how to effectively use conditional styles can significantly improve your Sass development process.