๊ด€๋ฆฌ ๋ฉ”๋‰ด

Outputor

SASS ๋ฌธ๋ฒ• - @mixin, @use์™€ ์–ธ๋”๋ฐ”( _ ) ๋ณธ๋ฌธ

๐ŸŽจ CSS ๐Ÿ”ป/๐Ÿฆš SASS-Note

SASS ๋ฌธ๋ฒ• - @mixin, @use์™€ ์–ธ๋”๋ฐ”( _ )

output7 2022. 1. 14. 12:46

@mixin

1. ์Šคํƒ€์ผ ์—ฌ๋Ÿฌ์ค„์„ ํ•œ ๋‹จ์–ด๋กœ ์น˜ํ™˜ํ•ด์„œ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•จ

 

2. ํ˜•ํƒœ: @mixin ์ด๋ฆ„ ( ){ ์Šคํƒ€์ผ }

 

3. @mixin์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด @include ์ด๋ฆ„( )์„ ์ž‘์„ฑํ•ด์ฃผ๋ฉด ๋จ

 

4.  @mixin ์ด๋ฆ„ ( $bgColor ) { background-color: $bgColor } ์ด๋ ‡๊ฒŒ ํ•จ์ˆ˜์ฒ˜๋Ÿผ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•จ

 

@mixin box-style ($bgColor) {
  width: 150px;
  height: 100px;
  margin:auto;
  margin-top: 20px;
  background-color: $bgColor;
}
.box {
  @include box-style(blue);
}

 

5. @mixin ์ด๋ฆ„ ( $bg ) { #{bg}: blue } ์ฒ˜๋Ÿผ ์Šคํƒ€์ผ ์ด๋ฆ„์—๋„ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•จ

 

@mixin box-style ($bg) {
  width: 150px;
  height: 100px;
  margin:auto;
  margin-top: 20px;
  #{$bg}: greenyellow;
}
.box {
  @include box-style(background-color);
}

 

@use์™€ ์–ธ๋”๋ฐ”( _ )

1. @use 'ํŒŒ์ผ์ด๋ฆ„' ์œผ๋กœ ๋ณต์‚ฌํ•  ์ˆ˜ ์žˆ์Œ

 

2. ์–ธ๋”๋ฐ”( _ )๋ฅผ ํŒŒ์ผ๋ช… ๋งจ ์•ž์— ์‚ฌ์šฉํ•˜๋ฉด CSSํŒŒ์ผ๋กœ ์ปดํŒŒ์ผํ•˜์ง€ ์•Š์Œ

 

@use๋กœ ์ฒจ๋ถ€ํ•œ ํŒŒ์ผ์— ์žˆ๋Š” ๋ณ€์ˆ˜๋ช…, @mixin์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ

1. ํŒŒ์ผ์ด๋ฆ„.$๋ณ€์ˆ˜๋ช…

 

2. @include ํŒŒ์ผ์ด๋ฆ„.mixin ์ด๋ฆ„( )

 

3. ํŒŒ์ผ๋ช… ์•ž์— ์–ธ๋”๋ฐ”( _ )๊ฐ€ ์žˆ๋Š”๊ฒฝ์šฐ ์–ธ๋”๋ฐ”๋Š” ๋นผ๊ณ  ํŒŒ์ผ๋ช…๋งŒ ์ž…๋ ฅํ•˜๋ฉด ๋จ

 

/* _reset.scss */

body {
  margin:0;
}
div {
  box-sizing: border-box;
}

$bg-color: #f06595;

@mixin box-style ($bgColor) {
  width: 150px;
  height: 100px;
  margin:auto;
  margin-top: 20px;
  background-color: $bgColor;
}

 

/* test.scss */

@use '_reset';

body {
  width: 100%;
  background-color: reset.$bg-color;
  text-align: center;
}

.box {
  @include reset.box-style(greenyellow);
}