Search code examples
sassmixins

Finding if a string is in string in sass


I want an if statement to show if a string is inside another string in sass.

How do i do this in a mixin?

 @mixin hello($mystring) {
 }

Solution

  • You can use str-index to achieve this.

    SCSS

    @mixin hello($mystring) {
      @if (str-index("Hello World", $mystring)) {
        background-color: green;
      }
      @else {
        background-color: blue;
      }
    }
    
    .test {
      @include hello("World");
    }
    

    CSS Output

    .test {
      background-color: green;
    }