Sass Dynamic Variables and Conditional Logic

Basic Concepts of Dynamic Variables

Dynamic variables in Sass allow developers to define variable values within the code that can be reused multiple times, enhancing code maintainability. These dynamic variables typically start with $, for example:

$primary-color = #333  
$font-size = 16px  

Assignment and Scope of Dynamic Variables

Dynamic variables can be assigned through various methods, including direct assignment, calculation expressions, and recursive definitions within nested structures.

  1. Direct Assignment
    The value of a variable can be specified directly in the code:

    $background-color = #f0f0f0  
    $text-color = $background-color !important  
    

    This approach is suitable for scenarios requiring explicit values, such as overriding default styles.

  2. Calculation Expressions
    Values can be dynamically generated through calculation expressions, for example:

    $border-radius = 5px + $font-size  
    

    The calculated result updates automatically and is suitable for styles requiring dynamic adjustment.

  3. Scope in Nested Structures
    Variables defined within nested structures are only effective at that specific level, for example:

    .box {  
      $inner-color = #000  
      .inner {  
        background-color: $inner-color  
      }  
    }  
    

    This design avoids the issue of variables polluting the global scope in deeply nested structures.

Implementation of Conditional Logic

Sass supports conditional logic, allowing developers to select different styles based on variable values using if-else statements.

  1. Basic Conditional Judgment
    Use an if statement to judge a variable's value:

    .button {  
      background-color: $primary-color  
      if ($primary-color == #333) {  
        border: 1px solid #666  
      }  
    }  
    

    This method provides flexible style control, but clarity of the conditional logic must be maintained.

  2. Multiple Conditional Judgments
    Multi-level conditional judgments can be implemented using else if:

    .button {  
      background-color: $primary-color  
      if ($primary-color == #333) {  
        border: 1px solid #666  
      }  
      else if ($primary-color == #666) {  
        border: 1px solid #999  
      }  
    }  
    

    Multi-level conditional judgments are suitable for scenarios requiring layered style control.

  3. Conditional Expressions Combined with Variables
    Conditional logic can be combined with variable values to implement more complex logic:

    .button {  
      background-color: $primary-color  
      if ($primary-color == #333) {  
        font-weight: bold  
      }  
      else {  
        font-weight: normal  
      }  
    }  
    

    This approach allows dynamic style adjustment, improving code flexibility.

Practice of Dynamic Variables and Conditional Logic

  1. Reusability of Dynamic Variables
    By defining variables, repetitive writing of identical styles can be avoided:

    .card {  
      background-color: $primary-color  
      border: 1px solid $border-color  
    }  
    

    This method reduces redundancy and improves code readability.

  2. Optimization of Conditional Logic
    Using conditional logic enables finer-grained style control, for example:

    .button {  
      background-color: $primary-color  
      if ($primary-color == #333) {  
        color: #fff  
      }  
      else {  
        color: #000  
      }  
    }  
    

    This approach is suitable for scenarios requiring differentiation based on different colors while keeping the code concise.

  3. Variable Management in Nested Structures
    In nested structures, scope restrictions of variables help avoid style conflicts:

    .container {  
      $inner-color = #000  
      .inner {  
        background-color: $inner-color  
      }  
    }  
    

    This design ensures the independence of variables within nesting levels and avoids global pollution.

Summary

Sass dynamic variables and conditional logic are core features for variable definition. By reasonably utilizing these characteristics, developers can achieve more flexible and maintainable style control. Assignment methods and scope management for dynamic variables need to be optimized based on specific scenarios, while conditional judgments enable complex style logic through logical branches. Mastering these technologies helps improve the efficiency and readability of CSS code.