As suggested earlier, if you change your mixing to this -
@mixin img-replace($img, $w: null, $h: null, $disp: block) { background-image: url('#{$path--rel}/#{$img}'); background-repeat: no-repeat; display: $disp; width: $w; height: $h; }
You can use your code flexibly, without having to assign width and height arguments. So if you write this -
.site-logo { @include img-replace("logo.png"); margin-top: 8px; margin-left: 6px; }
it will be compiled into
.site-logo { background-image: url("../images/logo.png"); background-repeat: no-repeat; display: block; margin-top: 8px; margin-left: 6px; }
It will also save your previously written codes without any changes.
Now, if you have any specific requirements, such as providing default values for different types of images that a developer can assign, you can add cards to your code -
$small-img: ( w: 100px, h: 100px ); $medium-img: ( w: 200px, h: 200px );
Now you can call img-replace as follows:
.site-logo { @include img-replace("logo.png", $small-img...); } .site-medium-image { @include img-replace("logo.png", $medium-img...); }
This will be compiled into
.site-logo { background-image: url("../images/logo.png"); background-repeat: no-repeat; display: block; width: 100px; height: 100px; } .site-medium-image { background-image: url("../images/logo.png"); background-repeat: no-repeat; display: block; width: 200px; height: 200px; }
In this ... the arguments are the arguments of the variable
Sass supports “variable arguments,” which are arguments at the end of a mixin declaration or function that takes all remaining arguments and pack them as a list. These arguments look the same as regular arguments, but follow ...