Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

たとえばこんなときに、

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;
}
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away