SCSS(재활용-Mixins)

Dev_Go·2022년 8월 9일
0

SCSS

목록 보기
8/13
post-thumbnail

재활용-Mixins


HTML

<div class="container">
  <div class="item">
    Mixin
  </div>
</div>

CSS

.container {
  width: 200px;
  height: 200px;
  background-color: orange;
  display: flex;
  justify-content: center;
  align-items: center;
}
.container .item{
  width: 100px;
  height: 100px;
  background-color: royalblue;
  display: flex;
  justify-content: center;
  align-items: center;
}

SCSS

@mixin center {
  display: flex;
  justify-content: center;
  align-items: center;
}
.container {
    @include center;
    width: 200px;
    height: 200px;
    background-color: orange;
    .item {
        @include center;
        width: 100px;
        height: 100px;
        background-color: royalblue;
    }
}

인수사용법

scss

@mixin box($size) {
    width: $size;
    height: $size;
    background-color: tomato;
}
.container {
    @include box(200px);
    .item {
        @include box(100px);
    }
}
.box {
    @include box(300px);
}

인수 기본값 설정

SCSS

@mixin box($size: 100px) {
    width: $size;
    height: $size;
    background-color: tomato;
}
.container {
    @include box(200px);
    .item {
        @include box;
    }
}
.box {
    @include box;
}

인수 여러개 사용

SCSS

@mixin box($size: 100px, $color: tomato) {
    width: $size;
    height: $size;
    background-color: $color;
}
.container {
    @include box(200px, red);
    .item {
        @include box;
    }
}
.box {
    @include box;
}

키워드 인수

인수가 2개니까 매개변수도 2개를 적어야하는데 첫번째 값이 기본값일 때, 굳이 적지 않아도 되니까 green만 적는다면 width,height도 green이 되어버린다. 그럴때 사용하는 것이 키워드 인수다.
@include box($color: green);

@mixin box($size: 100px, $color: tomato) {
    width: $size;
    height: $size;
    background-color: $color;
}
.container {
    @include box(200px, red);
    .item {
        @include box($color: green);
    }
}
.box {
    @include box;
}
profile
프론트엔드 4년차

0개의 댓글