Implementation of Sass Conditional Logic: if-else Syntax and Logic
Sass, as a CSS preprocessor, offers more powerful styling control capabilities than native CSS. Conditional logic is one of its core features. Through Sass's conditional logic, developers can dynamically generate styles based on specific conditions (such as variable values, user agents, media queries, etc.), achieving more flexible style management.
1. Syntax Structure of if-else
The syntax of Sass's if-else statement is similar to JavaScript but uses Sass-specific syntax structures, such as:
.if ($isMobile) {
width: 100%;
margin: 0 auto;
} else {
width: 80%;
margin: 10px;
}
Here, $isMobile is a variable. If its value is true, the if branch is executed; otherwise, the else branch is executed. Sass's conditional logic is based on boolean values, so it is necessary to ensure that the condition expression returns the correct boolean result.
2. Usage of Logical Operators
Sass's conditional logic supports logical operators (and, or, not) to extend the flexibility of condition checking:
.if ( $isMobile and $isTablet ) {
display: flex;
} else if ( $isTablet ) {
display: grid;
} else {
display: block;
}
Through and and or operators, developers can construct complex conditional logic, such as executing a certain style when multiple conditions are met simultaneously, or applying a style only when one of the conditions is true.
3. Common Usage Examples
(1) Judging Based on Variable Values
Sass allows condition logic to be controlled through variables, for example:
$primary-color: #333;
$secondary-color: #666;
.if ($primary-color != #333) {
background-color: $secondary-color;
} else {
background-color: $primary-color;
}
This example demonstrates how to dynamically switch the background color based on variable values.
(2) Based on User Agent
Developers can utilize Sass's @media queries combined with conditional logic to implement responsive design:
@media (max-width: 768px) {
.container {
width: 100%;
padding: 10px;
}
} else {
.container {
width: 80%;
padding: 15px;
}
}
This example demonstrates how to implement style differences under different screen sizes through media query conditional logic.
(3) Dynamic Calculation and Nesting
Sass's conditional logic can be combined with variables and nested structures to achieve more complex logic:
$base-width: 300px;
$extra-width: 50px;
.if ( $base-width > 300px ) {
.box {
width: $base-width;
}
} else if ( $extra-width > 0 ) {
.box {
width: $base-width + $extra-width;
}
}
Through dynamic calculation, developers can flexibly control style widths and avoid hardcoding.
4. Precautions
- Boolean Conversion: Sass automatically converts non-boolean values (such as numbers or strings) to boolean values. For example,
0or""are consideredfalse, while1or"true"are consideredtrue. - Null Value Handling: If the condition expression returns
false, Sass will automatically skip the corresponding branch to avoid applying invalid styles. - Performance Optimization: Overuse of conditional logic may impact performance. It is recommended to design logic reasonably according to actual needs and avoid redundant calculations.
Sass's conditional logic syntax is concise and intuitive, but attention must be paid to the correctness of logical expressions. By reasonably using if-else statements, developers can achieve more flexible style control, improving code maintainability and readability.