Skip to content

Instantly share code, notes, and snippets.

@yomotsu
Created May 14, 2011 07:22
Show Gist options
  • Save yomotsu/972011 to your computer and use it in GitHub Desktop.
Save yomotsu/972011 to your computer and use it in GitHub Desktop.
sass @Minxin linear-gradient() including SVG code for IE9
@mixin linear-gradient($angle, $color1, $offset1, $color2 ,$offset2:100, $color3:null, $offset3:100, $color4:null, $offset4:100, $color5:null, $offset5:100){
$angle_webkit:'left top, left bottom';
$angle_svg:'%20x2%3d%220%25%22%20y2%3d%22100%25%22';
$color-stop1_css:'#'#{$color1}' '#{$offset1}'%';
$color-stop2_css:',#'#{$color2}' '#{$offset2}'%';
$color-stop3_css:'';
$color-stop4_css:'';
$color-stop5_css:'';
$color-stop1_svg:'%3cstop%20style%3d%22stop%2dcolor%3a%23'#{$color1}'%22%20offset%3d%22'#{$offset1}'%25%22%2f%3e';
$color-stop2_svg:'%3cstop%20style%3d%22stop%2dcolor%3a%23'#{$color2}'%22%20offset%3d%22'#{$offset2}'%25%22%2f%3e';
$color-stop3_svg:'';
$color-stop4_svg:'';
$color-stop5_svg:'';
$color-stop1_webkit:'color-stop('#{$offset1}'%, #'#{$color1}')';
$color-stop2_webkit:',color-stop('#{$offset2}'%, #'#{$color2}')';
$color-stop3_webkit:'';
$color-stop4_webkit:'';
$color-stop5_webkit:'';
@if $angle == right{
$angle_webkit:'right top, left top';
$angle_svg:'%20x2%3d%22100%25%22';
}
@if $angle == bottom{
$angle_webkit:'left bottom, left top';
$angle_svg:'%20x2%3d%220%22%20y1%3d%22100%25%22';
}
@if $angle == left{
$angle_webkit:'left top, right top';
$angle_svg:'';
}
@if $color3 != null {
$color-stop3_css:',#'#{$color3}' '#{$offset3}'%';
$color-stop3_svg:'%3cstop%20style%3d%22stop%2dcolor%3a%23'#{$color3}'%22%20offset%3d%22'#{$offset3}'%25%22%2f%3e';
$color-stop3_webkit:',color-stop(#'#{$offset3}',#'#{$color3}')';
}
@if $color4 != null {
$color-stop4_css:',#'#{$color4}' '#{$offset4}'%';
$color-stop4_svg:'%3cstop%20style%3d%22stop%2dcolor%3a%23'#{$color4}'%22%20offset%3d%22'#{$offset4}'%25%22%2f%3e';
$color-stop4_webkit:',color-stop(#'#{$offset4}',#'#{$color4}')';
}
@if $color5 != null {
$color-stop5_css:',#'#{$color5}' '#{$offset5}'%';
$color-stop5_svg:'%3cstop%20style%3d%22stop%2dcolor%3a%23'#{$color5}'%22%20offset%3d%22'#{$offset5}'%25%22%2f%3e';
$color-stop5_webkit:',color-stop(#'#{$offset5}',#'#{$color5}')';
}
background-image:url(data:image/svg+xml,%3c%3fxml%20version%3d%221%2e0%22%3f%3e%3csvg%20xmlns%3d%22http%3a%2f%2fwww%2ew3%2eorg%2f2000%2fsvg%22%20width%3d%22100%25%22%20height%3d%22100%25%22%3e%3cdefs%3e%3clinearGradient%20id%3d%22G%22#{$angle_svg}%3e#{$color-stop1_svg}#{$color-stop2_svg}#{$color-stop3_svg}#{$color-stop4_svg}#{$color-stop5_svg}%3c%2flinearGradient%3e%3c%2fdefs%3e%3crect%20width%3d%22100%25%22%20height%3d%22100%25%22%20fill%3d%22url%28%23G%29%22%2f%3e%3c%2fsvg%3e);
background-image: -webkit-gradient(linear,#{$angle_webkit},#{$color-stop1_webkit}#{$color-stop2_webkit}#{$color-stop3_webkit}#{$color-stop4_webkit}#{$color-stop5_webkit});
background-image:-webkit-linear-gradient(#{$angle},#{$color-stop1_css}#{$color-stop2_css}#{$color-stop3_css}#{$color-stop4_css}#{$color-stop5_css});
background-image:-moz-linear-gradient(#{$angle},#{$color-stop1_css}#{$color-stop2_css}#{$color-stop3_css}#{$color-stop4_css}#{$color-stop5_css});
background-image:-ms-linear-gradient(#{$angle},#{$color-stop1_css}#{$color-stop2_css}#{$color-stop3_css}#{$color-stop4_css}#{$color-stop5_css});
background-image:-o-linear-gradient(#{$angle},#{$color-stop1_css}#{$color-stop2_css}#{$color-stop3_css}#{$color-stop4_css}#{$color-stop5_css});
background:-pie-linear-gradient(#{$angle},#{$color-stop1_css}#{$color-stop2_css}#{$color-stop3_css}#{$color-stop4_css}#{$color-stop5_css});
background-image:linear-gradient(#{$angle},#{$color-stop1_css}#{$color-stop2_css}#{$color-stop3_css}#{$color-stop4_css}#{$color-stop5_css});
}
background-image: url(data:image/svg+xml,%3c%3fxml%20version%3d%221%2e0%22%3f%3e%3csvg%20xmlns%3d%22http%3a%2f%2fwww%2ew3%2eorg%2f2000%2fsvg%22%20width%3d%22100%25%22%20height%3d%22100%25%22%3e%3cdefs%3e%3clinearGradient%20id%3d%22G%22%20x2%3d%220%25%22%20y2%3d%22100%25%22%3e%3cstop%20stop%2dcolor%3d%22#bce13d%22%20offset%3d%220%%22%2f%3e%3cstop%20stop%2dcolor%3d%22#f7fee7%22%20offset%3d%2250%%22%2f%3e%3cstop%20stop%2dcolor%3d%22black%22%20offset%3d%22100%%22%2f%3e%3c%2flinearGradient%3e%3c%2fdefs%3e%3crect%20width%3d%22100%25%22%20height%3d%22100%25%22%20fill%3d%22url%28%23G%29%22%2f%3e%3c%2fsvg%3e);
background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #bce13d)color-stop(0%, #bce13d)color-stop(100%, black));
background-image: -webkit-linear-gradient(top, #bce13d 0%,#f7fee7 50%,black 100%);
background-image: -moz-linear-gradient(top, #bce13d 0%,#f7fee7 50%,black 100%);
background-image: -ms-linear-gradient(top, #bce13d 0%,#f7fee7 50%,black 100%);
background-image: -o-linear-gradient(top, #bce13d 0%,#f7fee7 50%,black 100%);
background-image: linear-gradient(top, #bce13d 0%,#f7fee7 50%,black 100%); }
@import "_mixin_linear-gradient.scss";
.selector{
//引数は、<方向>,<色1>,<位置1>,<色2>,<位置2>,<色3>,<位置3>...
//色には#は付けない。また、16進数のみ
//offset にはパーセンテージで渡す。単位は付けない
@include linear-gradient(top, "BCE13D", 0, "F7FEE7", 50, "000000", 100);
}
@Kamilius
Copy link

Hi. I'm trying to use your mixin in my project, but i get "_linear-gradient_svg.scss: Not a valid color stop: Sass::Script::String: #A2A2A2 0%,#"E6E6E6" 0 100%)" error message. I'm building with Scout, under win7.
Could you please help me to figure out, what am i doing wrong? And how did you successfuly compiled this mixin?
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment