Syntax for if/else condition in SCSS mixin

后端 未结 3 1773
伪装坚强ぢ
伪装坚强ぢ 2020-12-12 16:13

Hi I\'m trying to learn SASS/SCSS and am trying to refactor my own mixin for clearfix

what I\'d like is for the mixin to be based on whether I pass the mixin a width

相关标签:
3条回答
  • 2020-12-12 16:30

    You can assign default parameter values inline when you first create the mixin:

    @mixin clearfix($width: 'auto') {
    
      @if $width == 'auto' {
    
        // if width is not passed, or empty do this
    
      } @else {
    
        display: inline-block;
        width: $width;
    
      }
    }
    
    0 讨论(0)
  • 2020-12-12 16:31

    You could default the parameter to null or false.
    This way, it would be shorter to test if a value has been passed as parameter.

    @mixin clearfix($width: null) {
    
      @if not ($width) {
    
        // if width is not passed, or empty do this
    
      } @else {
    
        display: inline-block;
        width: $width;
    
      }
    }
    
    0 讨论(0)
  • 2020-12-12 16:32

    You could try this:

    $width:auto;
    @mixin clearfix($width) {
    
       @if $width == 'auto' {
    
        // if width is not passed, or empty do this
    
       } @else {
            display: inline-block;
            width: $width;
       }
    }
    

    I'm not sure of your intended result, but setting a default value should return false.

    0 讨论(0)
提交回复
热议问题