Sass Mixin Error for IE specific filters like -ms-filter

11,450

Solution 1

Solved it like this, but still looking for alternative suggestions on the best way...

=default_button(!lighter, !darker) 
  text-shadow= 1px 1px 3px darken(!darker, 8)
  border= 1px !darker solid
  background-color= !lighter
  background= -webkit-gradient(linear, 0 0, 0 100%, from(!lighter), to(!darker)) repeat-x, !darker
  background= -moz-linear-gradient(90deg, !darker, !lighter) repeat-x scroll 0 0 !darker
  -ms-filter = "progid:DXImageTransform.Microsoft.gradient(startColorstr='#{!lighter}', endColorstr='#{!darker}')"
  :zoom 1
  :margin 0 0 0 0
  :width auto

The syntax for Sass has changed since this answer was originally posted. The modern sass (indented) syntax looks like this:

=default_button($lighter, $darker) 
  text-shadow: 1px 1px 3px darken($darker, 8)
  border: 1px $darker solid
  background-color: $lighter
  background: -webkit-gradient(linear, 0 0, 0 100%, from($lighter), to($darker)) repeat-x, $darker
  background: -moz-linear-gradient(90deg, $darker, $lighter) repeat-x scroll 0 0 $darker
  -ms-filter: unquote("progid:DXImageTransform.Microsoft.gradient(startColorstr='#{$lighter}', endColorstr='#{$darker}')")
  zoom: 1
  margin: 0 0 0 0
  width: auto

Solution 2

Interpolation #{} doesn't work sometimes because it shortens hex color values. For example, it will shorten #334455 to #345, which breaks the filter syntax.

SASS has a new function in version 3.2: ie-hex-str().

Here is how I got it to work:

filter: unquote("progid:DXImageTransform.Microsoft.gradient(startColorstr='")
+ ie-hex-str($start)
+ unquote("', endColorstr='")
+ ie-hex-str($stop)
+ unquote("',GradientType=0)"); /* IE6-9 */

Solution 3

Update your syntax to use : instead of = for the property definitions:

=mixin($variable) 
  property: value
  property: $variable

Check out the SASS Reference, though the examples are in SCSS rather than SASS indented style. Full index of the SASS documentation.

Share:
11,450
kinet
Author by

kinet

Updated on June 17, 2022

Comments

  • kinet
    kinet almost 2 years

    I'm trying to make a button mixin like this:

    =default_button(!lighter, !darker) 
      :border= 1px !lighter solid
      :background-color #e3e3e3
      :background= -webkit-gradient(linear, 0 0, 0 100%, from(!lighter), to(!darker)) repeat-x, #d0581e
      :background= -moz-linear-gradient(90deg, !darker, !lighter) repeat-x scroll 0 0 #d0581e
      :filter= progid:DXImageTransform.Microsoft.gradient(startColorstr='!lighter', endColorstr='!darker')
      :-ms-filter= "progid:DXImageTransform.Microsoft.gradient(startColorstr='!lighter', endColorstr='!darker')"
      :zoom 1
      :margin 0 0 0 0
      :width auto
      :padding 2px 14px 2px 14px
      :border-radius 10px
      :-webkit-border-radius 10px
      :-moz-border-radius 10px
      :color #FFF
    

    When I compile the sass, i get this error for the lines beginning with -filter and -ms-filter:

    SASS::SyntaxError: Expected rparen token, was single_eq token

    I'm pretty sure it's my placement of the ='s, but I'm not exactly sure how to write it correctly. It works if I pass the hex values instead of !lighter, !darker, because then I can remove the = sign like so:

    :filter progid:DXImageTransform.Microsoft.gradient(startColorstr='#F89F16', endColorstr='#d0581e')
    :-ms-filter "progid:DXImageTransform.Microsoft.gradient(startColorstr='#F89F16', endColorstr='#d0581e')"
    
  • voetsjoeba
    voetsjoeba over 12 years
    Upvote for the #{...} syntax. Needed this to have SASS replace variables for the startColorstr/endColorstr arguments of the MS gradient filter on SASS 3.1.10. That is, startColorstr=$foo wouldn't work (as in it wouldn't substitute $foo), but startColorstr=#{$foo} did.
  • Orlando
    Orlando about 12 years
    thanks.. my gradient was blue to black (ie's default) before this haha.. thanks again
  • cimmanon
    cimmanon over 9 years
    If your colors have alpha transparency (rgba), then this method will not work. You will need to use Matthias Dailey's answer below.