๋ณธ๋ฌธ์œผ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

Syntax-06-mixins

Sass(SCSS) Syntax - 6. ์žฌํ™œ์šฉ(Mixins)#

โ—๏ธ ํ•ด๋‹น ๊ธ€์€ ํŒจ์ŠคํŠธ์บ ํผ์Šค - ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ ๊ฐ•์˜์—์„œ HTML & CSS, SASS(SCSS) Part์˜ ๋ฐ•์˜์›… ๊ฐ•์‚ฌ๋‹˜์˜ ๊ฐ•์˜์ž๋ฃŒ(Sass(SCSS) ์™„์ „ ์ •๋ณต!)๋ฅผ ๋ณด๋ฉฐ ์ •๋ฆฌํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์žฌํ™œ์šฉ(Mixins)#

Sass Mixins๋Š” ์Šคํƒ€์ผ ์‹œํŠธ ์ „์ฒด์—์„œ ์žฌ์‚ฌ์šฉ ํ•  CSS ์„ ์–ธ ๊ทธ๋ฃน์„ ์ •์˜ํ•˜๋Š” ๊ธฐ๋Šฅ์ด๋‹ค.

Mixin ์‚ฌ์šฉ๋ฒ•์„ ๋‘ ๊ฐ€์ง€๋กœ ๋‚˜๋ˆ„๋ฉด ์„ ์–ธํ•˜๊ธฐ( @mixin )์™€ ํฌํ•จํ•˜๊ธฐ( @include )์ด๋‹ค.

@mixin#

๊ธฐ๋ณธ์ ์ธ @mixin ์„ ์–ธ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€๋ฐ, SCSS์™€ Sass์˜ ๋ฌธ๋ฒ•์ด ์กฐ๊ธˆ ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค.

// SCSS@mixin ๋ฏน์Šค์ธ์ด๋ฆ„ {  ์Šคํƒ€์ผ;}
// Sass=๋ฏน์Šค์ธ์ด๋ฆ„  ์Šคํƒ€์ผ
// SCSS@mixin large-text {  font-size: 22px;  font-weight: bold;  font-family: sans-serif;  color: orange;}
// Sass=large-text  font-size: 22px  font-weight: bold  font-family: sans-serif  color: orange

๋˜ํ•œ @mixin ์€ ์„ ํƒ์ž๋ฅผ ํฌํ•จ ๊ฐ€๋Šฅํ•˜๊ณ , ์ƒ์œ„(๋ถ€๋ชจ) ์š”์†Œ ์ฐธ์กฐ( & ๋“ฑ)๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

@mixin large-text {  font: {    size: 22px;    weight: bold;    family: sans-serif;  }  color: orange;
  &::after {    content: "!!";  }
  span.icon {    background: url("/images/icon.png");  }}

@include#

์„ ์–ธ๋œ @mixin ์„ ์‚ฌ์šฉํ•  ๋•Œ๋Š” @include ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์—ญ์‹œ SCSS์™€ Sass๊ฐ€ ์•ฝ๊ฐ„์˜์ฐจ์ด๊ฐ€ ์žˆ๋‹ค.

// SCSS@include ๋ฏน์Šค์ธ์ด๋ฆ„;
// Sass+๋ฏน์Šค์ธ์ด๋ฆ„
// SCSSh1 {  @include large-text;}div {  @include large-text;}
// Sassh1  +large-textdiv  +large-text

์ปดํŒŒ์ผํ•˜๋ฉด

h1 {font-size: 22px;font-weight: bold;font-family: sans-serif;color: orange;}h1::after {content: "!!";}h1 span.icon {background: url("/images/icon.png");}div {font-size: 22px;font-weight: bold;font-family: sans-serif;color: orange;}div::after {content: "!!";}div span.icon {background: url("/images/icon.png");}

์ธ์ˆ˜(Arguments)#

Mixin์€ ํ•จ์ˆ˜(Functions)์ฒ˜๋Ÿผ ์ธ์ˆ˜(Arguments)๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.

// SCSS@mixin ๋ฏน์Šค์ธ์ด๋ฆ„($๋งค๊ฐœ๋ณ€์ˆ˜) {  ์Šคํƒ€์ผ;}@include ๋ฏน์Šค์ธ์ด๋ฆ„(์ธ์ˆ˜);
// Sass=๋ฏน์Šค์ธ์ด๋ฆ„($๋งค๊ฐœ๋ณ€์ˆ˜)  ์Šคํƒ€์ผ
+๋ฏน์Šค์ธ์ด๋ฆ„(์ธ์ˆ˜)
@mixin dash-line($width, $color) {  border: $width dashed $color;}
.box1 { @include dash-line(1px, red); }.box2 { @include dash-line(4px, blue); }

์ปดํŒŒ์ผํ•˜๋ฉด

.box1 {border: 1px dashed red;}.box2 {border: 4px dashed blue;}

์ธ์ˆ˜์˜ ๊ธฐ๋ณธ๊ฐ’ ์„ค์ •#

์ธ์ˆ˜(Arguments)๋Š” ๊ธฐ๋ณธ๊ฐ’(default value)๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.

@include ํฌํ•จ ๋‹จ๊ณ„์—์„œ ๋ณ„๋„์˜ ์ธ์ˆ˜๊ฐ€ ์ „๋‹ฌ๋˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์— ๊ธฐ๋ณธ๊ฐ’์„ ์‚ฌ์šฉํ•˜๊ฒŒ ๋œ๋‹ค.

@mixin ๋ฏน์Šค์ธ์ด๋ฆ„($๋งค๊ฐœ๋ณ€์ˆ˜: ๊ธฐ๋ณธ๊ฐ’) {  ์Šคํƒ€์ผ;}
@mixin dash-line($width: 1px, $color: black) {  border: $width dashed $color;}
.box1 { @include dash-line; }.box2 { @include dash-line(4px); }

์ปดํŒŒ์ผํ•˜๋ฉด

.box1 {border: 1px dashed black;}.box2 {border: 4px dashed black;}

ํ‚ค์›Œ๋“œ ์ธ์ˆ˜(Keyword Arguments)#

@mixin ์— ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ ์ผ ๋•Œ, @include ์—์„œ ์ธ์ˆ˜์˜ ๊ฐ’๋งŒ ์ž…๋ ฅํ•˜๋ฉด, ์—ฌ๋Ÿฌ๋งค๊ฐœ๋ณ€์ˆ˜์— ์ธ์ˆ˜๊ฐ€ ์ˆœ์ฐจ์ ์œผ๋กœ ์ „๋‹ฌ๋˜๊ฒŒ ๋œ๋‹ค.

ํ•˜์ง€๋งŒ ์ธ์ˆ˜์˜ ๊ฐ’๋งŒ ์ž…๋ ฅํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋ช…์‹œ์ ์œผ๋กœ ํ‚ค์›Œ๋“œ(๋ณ€์ˆ˜)๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ์ „๋‹ฌํ•˜๊ฒŒ ๋˜๋ฉด, ์ˆœ์„œ์— ์ƒ๊ด€ ์—†์ด ์›ํ•˜๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜์— ์ธ์ˆ˜๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค.

์ด ๋•Œ, ์ž‘์„ฑํ•˜์ง€ ์•Š์€ ์ธ์ˆ˜๊ฐ€ ์ ์šฉ๋  ์ˆ˜ ์žˆ๋„๋ก @mixin ์—์„œ ๊ธฐ๋ณธ๊ฐ’์„ ์„ค์ •ํ•ด ์ฃผ๋Š”๊ฒƒ์ด ์ข‹๋‹ค.

@mixin ๋ฏน์Šค์ธ์ด๋ฆ„($๋งค๊ฐœ๋ณ€์ˆ˜A: ๊ธฐ๋ณธ๊ฐ’, $๋งค๊ฐœ๋ณ€์ˆ˜B: ๊ธฐ๋ณธ๊ฐ’) {  ์Šคํƒ€์ผ;}
@include ๋ฏน์Šค์ธ์ด๋ฆ„($๋งค๊ฐœ๋ณ€์ˆ˜B: ์ธ์ˆ˜);
@mixin position(  $p: absolute,  $t: null,  $b: null,  $l: null,  $r: null) {  position: $p;  top: $t;  bottom: $b;  left: $l;  right: $r;}
.absolute {  // ํ‚ค์›Œ๋“œ ์ธ์ˆ˜๋กœ ์„ค์ •ํ•  ๊ฐ’๋งŒ ์ „๋‹ฌ  @include position($b: 10px, $r: 20px);}.fixed {  // ์ธ์ˆ˜๊ฐ€ ๋งŽ์•„์ง์— ๋”ฐ๋ผ ๊ฐ€๋…์„ฑ์„ ํ™•๋ณดํ•˜๊ธฐ ์œ„ํ•ด ์ค„๋ฐ”๊ฟˆ  @include position(    fixed,    $t: 30px,    $r: 40px  );}

์ปดํŒŒ์ผํ•˜๋ฉด

.absolute {position: absolute;bottom: 10px;right: 20px;}.fixed {position: fixed;top: 30px;right: 40px;}

์ธ์ˆ˜ ๋ฆฌ์ŠคํŠธ(Argument Lists)#

๋•Œ๋•Œ๋กœ ์ž…๋ ฅํ•  ์ธ์ˆ˜์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋ถˆํ™•์‹คํ•œ ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋Š”๋ฐ, ๊ทธ ๋•Œ๋ฅผ ์œ„ํ•ด ์ธ์ˆ˜ ๋ฆฌ์ŠคํŠธ๋ฅผ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ”— Argument Lists๐Ÿ”— Taking Arbitrary Arguments

๋งค๊ฐœ๋ณ€์ˆ˜ ๋’ค์— ... ๋ฅผ ๋ถ™์—ฌ์ฃผ๋Š” ๊ฒƒ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

@mixin ๋ฏน์Šค์ธ์ด๋ฆ„($๋งค๊ฐœ๋ณ€์ˆ˜...) {  ์Šคํƒ€์ผ;}
@include ๋ฏน์Šค์ธ์ด๋ฆ„(์ธ์ˆ˜A, ์ธ์ˆ˜B, ์ธ์ˆ˜C);
// ์ธ์ˆ˜๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ํ•˜๋‚˜์”ฉ ์ „๋‹ฌ ๋ฐ›๋‹ค๊ฐ€, 3๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜($bg-values)๋Š” ์ธ์ˆ˜์˜ ๊ฐœ์ˆ˜์— ์ƒ๊ด€์—†์ด ๋ฐ›์Œ@mixin bg($width, $height, $bg-values...) {  width: $width;  height: $height;  background: $bg-values;}
div {  // ์œ„์˜ Mixin(bg) ์„ค์ •์— ๋งž๊ฒŒ ์ธ์ˆ˜๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ์ „๋‹ฌํ•˜๋‹ค๊ฐ€ 3๋ฒˆ์งธ ์ดํ›„๋ถ€ํ„ฐ๋Š” ๊ฐœ์ˆ˜์— ์ƒ๊ด€์—†์ด ์ „๋‹ฌ  @include bg(    100px,    200px,    url("/images/a.png") no-repeat 10px 20px,    url("/images/b.png") no-repeat,    url("/images/c.png")  );}

์ปดํŒŒ์ผํ•˜๋ฉด

div {width: 100px;height: 200px;background: url("/images/a.png") no-repeat 10px 20px,url("/images/b.png") no-repeat,url("/images/c.png");}

์ธ์ˆ˜ ๋ฆฌ์ŠคํŠธ๋Š” @mixin ์œผ๋กœ ์ „๋‹ฌํ•  ๋•Œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

@mixin font(  $style: normal,  $weight: normal,  $size: 16px,  $family: sans-serif) {  font: {    style: $style;    weight: $weight;    size: $size;    family: $family;  }}div {  // ๋งค๊ฐœ๋ณ€์ˆ˜ ์ˆœ์„œ์™€ ๊ฐœ์ˆ˜์— ๋งž๊ฒŒ ๋ฆฌ์ŠคํŠธ๋กœ ์ „๋‹ฌ  $font-values: italic, bold, 16px, sans-serif;  @include font($font-values...);}span {  // ํ•„์š”ํ•œ ๊ฐ’๋งŒ ๋งต์œผ๋กœ ๋ณ€์ˆ˜์— ๋‹ด์•„ ์ „๋‹ฌ  $font-values: (style: italic, size: 22px);  @include font($font-values...);}a {  // ํ•„์š”ํ•œ ๊ฐ’๋งŒ ๋งต์œผ๋กœ ์ „๋‹ฌ  @include font((weight: 900, family: monospace)...);}

$font-values ๋Š” div ์—์„œ๋Š” ๋ฆฌ์ŠคํŠธ๋กœ, span , a ์—์„œ๋Š” ๋งต์œผ๋กœ ์‚ฌ์šฉ๋œ ๋ณ€์ˆ˜์ด๋‹ค.

... ์„ ๋ณ€์ˆ˜ ๋’ค์— ์ถ”๊ฐ€ํ•˜์—ฌ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•จ์œผ๋กœ ์—ฌ๋Ÿฌ ๋ณ€์ˆ˜๋“ค์„ ํ•œ๋ฒˆ์— ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค.

@content#

@mixin ์„ ์„ ์–ธํ•  ๋•Œ @content ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด @include ๋ฅผ ์‚ฌ์šฉํ•ด ์›ํ•˜๋Š” ์Šคํƒ€์ผ๋ธ”๋ก์„ ์ถ”๊ฐ€ํ•ด์„œ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค.

@mixin ๋ฏน์Šค์ธ์ด๋ฆ„() {  ์Šคํƒ€์ผ;  @content;}
@include ๋ฏน์Šค์ธ์ด๋ฆ„() {  // ์Šคํƒ€์ผ ๋ธ”๋ก  ์Šคํƒ€์ผ;}
@mixin icon($url) {  &::after {    content: $url;    @content;  }}.icon1 {  // icon Mixin์˜ ๊ธฐ์กด ๊ธฐ๋Šฅ๋งŒ ์‚ฌ์šฉ  @include icon("/images/icon.png");}.icon2 {  // icon Mixin์— ์Šคํƒ€์ผ ๋ธ”๋ก์„ ์ถ”๊ฐ€ํ•˜์—ฌ ์‚ฌ์šฉ  @include icon("/images/icon.png") {    position: absolute;  };}

์ปดํŒŒ์ผํ•˜๋ฉด

.icon1::after {content: "/images/icon.png";}.icon2::after {content: "/images/icon.png";position: absolute;}

@mixin ์— ์ „๋‹ฌ๋˜๋Š” ์Šคํƒ€์ผ ๋ธ”๋ก์€ @mixin ์— ์ „๋‹ฌ๋˜๊ธฐ ์ „ @include ๋ฒ”์œ„์—์„œํ‰๊ฐ€๋œ๋‹ค.

์ฆ‰, @mixin ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ผ ์ „์—ญ๋ณ€์ˆ˜๋กœ ํ•ด์„๋œ๋‹ค.

$color: red;
@mixin colors($color: blue) {  // Mixin์˜ ๋ฒ”์œ„  @content;  background-color: $color;  border-color: $color;}
div {  @include colors() {    // ์Šคํƒ€์ผ ๋ธ”๋ก์ด ์ •์˜๋œ ๋ฒ”์œ„    color: $color;  }}

์ปดํŒŒ์ผํ•˜๋ฉด

div {color: red;background-color: blue;border-color: blue;}

Reference#

ํŒจ์ŠคํŠธ์บ ํผ์Šค - ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ ๊ฐ•์˜ - HTML & CSS, SASS(SCSS) Part by ParkYoungWoong

Sass(SCSS) ์™„์ „ ์ •๋ณต!

Argument Lists

Taking Arbitrary Arguments