[SCSS] @mixin VS @extend

seungยท2022๋…„ 4์›” 21์ผ
0

๐Ÿ’ก ๋ฏธ๋ฆฌ๋ณด๋Š” ๊ฒฐ๋ก 
๊ฐ€๊ธ‰์  @mixin์„ ์‚ฌ์šฉํ•ด๋ผ.


Why?
๋ฌด๋ถ„๋ณ„ํ•œ @extend ์‚ฌ์šฉ ์‹œ ์—ฐ๊ด€์„ฑ ์—†๋Š” ๊ฐ’๋“ค์ด ํ•œ ์žฅ์†Œ์— ๋ฌถ์ด๊ฒŒ ๋˜๊ณ ,
์†Œ์Šค ์ˆœ์„œ๊ฐ€ ์–ด๊ทธ๋Ÿฌ์ ธ์„œ ํ˜ผ๋ž€์„ ์ค€๋‹ค.


์ฐธ๊ณ : https://mytory.net/2016/12/23/when-to-use-extend-when-to-use-a-mixin.html


@mixin


  • ์—ฐ๊ด€์„ฑ ์—†๋Š” ์„ ํƒ์ž์˜ ์ค‘๋ณต ์ œ๊ฑฐ
  • ํ•จ์ˆ˜์ฒ˜๋Ÿผ ์ธ์ˆ˜ ์‚ฌ์šฉ๊ฐ€๋Šฅ

์„ ์–ธ

@mixin style-link-text($col) {
  text-decoration: none;
  text-transform: uppercase;
  color: $col;
}

ํ˜ธ์ถœ

  • @include ์ž…๋ ฅ ํ›„ ์‚ฌ์šฉ
$color-text-white: #fff;

.btn-main {
  @include style-link-text($color-text-white);
}
.btn-hot {
  @include style-link-text($color-text-white);
}

CSS ์ปดํŒŒ์ผ ๊ฒฐ๊ณผ

  • ์—ฐ๊ด€์„ฑ ์—†๋Š” ์„ ํƒ์ž๋“ค์ด ๋”ฐ๋กœ ์„ ์–ธ๋œ๋‹ค.
.btn-main {
  text-decoration: none;
  text-transform: uppercase;
  color: #fff;
}
.btn-hot {
  text-decoration: none;
  text-transform: uppercase;
  color: #fff;
}

@extend


  • ์—ฐ๊ด€์„ฑ ์žˆ๋Š” ์„ ํƒ์ž์˜ ์ค‘๋ณต ์ œ๊ฑฐ

์„ ์–ธ

  • %๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ๋ฐ”๋กœ ๋’ค์— ์ด๋ฆ„์„ ์„ ์–ธ
%btn-placeholder {
  padding: 10px;
  display: inline-block;
  text-align: center;
  border-radius: 100px;
  width: 100px;
  @include style-link-text($color-text-white);
}

ํ˜ธ์ถœ

  • @extend ์ž…๋ ฅ ํ›„ ์‚ฌ์šฉ
.btn-main {
  &:link {
    @extend %btn-placeholder;
  }
}

.btn-hot {
  &:link {
    @extend %btn-placeholder;
  }
}

CSS ์ปดํŒŒ์ผ ๊ฒฐ๊ณผ

  • ์—ฐ๊ด€์„ฑ ์žˆ๋Š” ์„ ํƒ์ž๋“ค์ด ๊ฐ™์ด ์„ ์–ธ๋œ๋‹ค.
.btn-main:link,
.btn-hot:link {
  padding: 10px;
  display: inline-block;
  text-align: center;
  border-radius: 100px;
  width: 100px;
  @include style-link-text($color-text-white);
}
profile
๐ŸŒธ ์ข‹์€ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ์‹ถ์€ ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž โœจ

0๊ฐœ์˜ ๋Œ“๊ธ€