7

I'm working on SASS with compass. I've already created some mixins but still, there is one mixin I can't make it to work. That's the user-select one. I know I can import it using @import "compass/css3/user-interface"; but that's not the point. Why my 'handmade' @mixin user-select($value){..} does not seem to work? Is there any known reason?

@mixin user-select($value) {
  -webkit-user-select: $value; 
  -moz-user-select: $value; 
  -ms-user-select: $value; 
  -o-user-select: $value; 
  user-select: $value;    
}

.myclass {
  @include user-select(none);
}
Ben Kalsky
  • 148
  • 2
  • 16
Nim
  • 71
  • 1
  • 4
  • Are you using the appropriate prefixes for user-select ? – Vangel Tzo Apr 29 '14 at 09:38
  • Would be maybe helpful if you edit your question and add your full mixin code. – pzin Apr 29 '14 at 09:55
  • The mixin looks correct, so the problem probably lies elsewhere. Make sure you define the `@mixin` before you `@include` it. Do you get an error when you compile it? – Nils Kaspersson Apr 29 '14 at 10:18
  • @NilsKaspersson that made the trick. Did not knew I had to define the mixins before including, sry. Thanks to everyone, cheers. – Nim Apr 29 '14 at 10:45
  • Are you trying to use the mixin within a media query? If so mixins and placeholders do not work within media queries – Crystal Jan 03 '17 at 17:31

2 Answers2

4

Seems to work fine for me.. You can try this approach:

@mixin user-select($select) {
  @each $pre in -webkit-, -moz-, -ms-, -o- {
    #{$pre + user-select}: #{$select};
  } 
  #{user-select}: #{$select};
}

.myclass {
  @include user-select(none);
}
Ben Kalsky
  • 148
  • 2
  • 16
0

You may even generalize this one step further.

    $PREFIXES: -webkit-, -moz-, -ms-, -o-, '';
    
    @mixin prefix-template($var, $value, $prefixes:$PREFIXES) {
        @each $pre in $prefixes { #{$pre + $var}: #{$value};  }
    }
    
    @mixin user-select($value) {
      @include prefix-template(user-select, #{$value});
    }
   
    @mixin animation($value) {
      @include prefix-template(animation, #{$value});
    }

Flint
  • 1,651
  • 1
  • 19
  • 29