0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Mixinを使う

Posted at

##Mixinって何に使うやつ?

  • 内部にCSSのプロパティ群を持つことができる。
  • 共通のCSSを一元管理したい時に便利。

##SCSSでかく

SCSS
//mixin設定
@mixin hoge($argmentWidth: 10px) { //引数を渡さない場合は$argmentWidthは10px(デフォルト値)
  width: $argmentWidth;
  .add {
    @content; //mixin使用先でプロパティを記述したい場合に使用
  }
}

//mixinを使う対象
.target1 {
  @include hoge(5px) {
    //@contentに代入
    display: inline-block;
  }
  height: auto;
}

.target2 {
  @include hoge {
    display: inline;
  }
  height: 0;
}

##CSSにすると…

CSS
.target1 {
  width: 5px;
  height: auto;
}

.target1 .add {
  display: inline-block;
}

.target2 {
  width: 10px;
  height: 0;
}

.target2 .add {
  display: inline;
}

##こんな場所に使う

###レスポンシブのメディアクエリに

SCSS
//mixin設定
@mixin sp($breakPoint: 768px) {
  @media screen and (max-width: $breakPoint) {
    @content;
  }
}

//mixinを使う対象
.target {
  max-width: 960px;
  @include sp(560px) {
    max-width: 100%;
  }
}
CSS
.target {
  max-width: 960px;
}

@media screen and (max-width: 560px) {
  .target {
    max-width: 100%;
  }
}

ブレークポイントを弄るのはあまりよくないかも…
引数のところは変数とかで共通化しよう。

##最後に

みなさんMixinをどのように使っていますか…?

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?