1
1

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 3 years have passed since last update.

SCSSの記述をすっきりさせる方法 〜mixin(共通レイアウトをまとめる)〜

Last updated at Posted at 2019-12-25

mixinで共通レイアウトをまとめる

styles.scss
.side-header{
  display:flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.main-header{
  display:flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

上記のように同じレイアウトのクラスがあった場合に@mixinでレイアウトをまとめるとscssの記述がすっきりします。

_mixin.scss
@mixin verticality_centering {
    display:flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}
styles.scss
@import "mixin.scss"

.side-header{
  @include verticality_centering;
}

.main-header{
  @include verticality_centering;
}

@mixin 変数名{レイアウト}で定義して、@include変数名で使用できます。

また引数を渡すこともでき、

mixin.scss
@mixin flex_style($value) {
    display: flex;
    justify-content: $value;
}
styles.scss
@import "mixin.scss"

.side-header{
  @include flex_style(space-between);
}

このような使い方もできます。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?