LoginSignup
5
4

More than 5 years have passed since last update.

【Sass】mixinで引数を省略したときプロパティを出力しないようにする

Last updated at Posted at 2017-12-13

たとえばこんなときに、

style.scss
@mixin foo($w, $h){
 width: $w;
 height: $h;
}

div {
 @include foo(100px);
}

こうなってほしい

style.css
div {
 width: 100px;
}

引数の初期値をnullにする

Sass(scss)では引数の後ろに初期値を指定できます。

style.scss
@mixin foo($w, $h: 50px){
 width: $w;
 height: $h;
}

div {
 @include foo(100px);
}
style.css
div {
 width: 100px;
 height: 50px;
}

この初期値にnullを指定すると実装したかった動きになります。

style.scss
@mixin foo($w, $h: null){
 width: $w;
 height: $h;
}

div {
 @include foo(100px);
}
style.css
div {
 width: 100px;
}

ちなみに

引数に真偽値を指定することもできます。

style.scss
@mixin foo($w, $h: true){
 width: $w;
 @if($h){
  height: 80px;
 }
 @else {
  height: 40px;
 }
}

div {
 @include foo(100px, false);
}
style.css
div {
 width: 100px;
 height: 40px;
}
5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4