๐ŸŽฒ

4. Mixin

ย 

1. Mixin์ด๋ž€

Mixin์€ ์ฝ”๋“œ๋ฅผ ์žฌ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๋งŒ๋“ค์–ด์ง„ ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค. ์„ ํƒ์ž๋“ค ์‚ฌ์ด์—์„œ ๋ฐ˜๋ณต๋˜๊ณ  ์žˆ๋Š” ์ฝ”๋“œ๋“ค์€ mixin์„ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ”๋“œ ๋ฐ˜๋ณต์„ ์ค„์ž…๋‹ˆ๋‹ค. ์ค‘๋ณต๋˜๋Š” ์ฝ”๋“œ๋Š” mixin์œผ๋กœ ๋งŒ๋“ค์–ด ๋†“๊ณ  ์›ํ•˜๋Š” ์„ ํƒ์ž ๋ธ”๋Ÿญ์— mixin์„ includeํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.
ย 

2. Mixin ์‚ฌ์šฉํ•˜๊ธฐ

@mixin ์ด๋ฆ„(๋งค๊ฐœ๋ณ€์ˆ˜) //์ƒ์„ฑ @include ์ด๋ฆ„(์ธ์ˆ˜) //์‚ฌ์šฉ
  • ์•ž์— @Mixin์„ ์“ฐ๊ณ  ์ด๋ฆ„์„ ์ •ํ•ด์ค€ ํ›„, ์ค‘๊ด„ํ˜ธ { } ์•ˆ์— ์ค‘๋ณต๋˜๋Š” ์ฝ”๋“œ๋ฅผ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.
  • @include๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์Šคํƒ€์ผ ํ•˜๊ณ ์ž ํ•˜๋Š” ์š”์†Œ์— ํฌํ•จ ์‹œํ‚ค๋ฉด ๋ฉ๋‹ˆ๋‹ค.
  • mixin์€ ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด์„œ importํ•˜์—ฌ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜, mixin์„ ์‚ฌ์šฉํ•  ํŒŒ์ผ ๋‚ด์—์„œ ์„ ์–ธ ํ›„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ, ์—ฌ๋Ÿฌ ๊ฐœ์˜ mixin์„ ๋งŒ๋“ค์–ด ์‚ฌ์šฉํ•œ๋‹ค๋ฉด, _mixins.scss ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด์„œ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
ย 
.card { display: flex; justify-content: center; align-items: center; background: gray; } .aside { display: flex; justify-content: center; align-items: center; background: white; } /*.card์™€ .aside ํด๋ž˜์Šค ์„ ํƒ์ž์˜ ์ ์šฉํ•œ ์Šคํƒ€์ผ์ด ๊ฒน์นจ*/
// scss๋ฅผ ์‚ฌ์šฉ @mixin center-xy{ display: flex; justify-content : center; align-items : center; } .card{ @include center-xy; // ์ •์˜ํ•œ center-xy mixin์„ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ”๋“œ ํ•œ์ค„์ด๋ฉด ๋! } .aside{ @include center-xy; }
โš ๏ธ ๋ฐ˜๋ณตํ•˜๋Š” ๋ชจ๋“  ์ฝ”๋“œ๋ฅผ ํ•˜๋‚˜์˜ mixin์— ๋ชฐ์•„๋„ฃ์–ด์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฑด ๋ฐ”๋ฅธ mixin ์‚ฌ์šฉ๋ฒ•์ด ์•„๋‹™๋‹ˆ๋‹ค. ์œ„์— ์ฝ”๋“œ์ฒ˜๋Ÿผ ์Šคํƒ€์ผ๋ณ„๋กœ ๋‚˜๋ˆ„์–ด์„œ mixin์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์„œ๋กœ ์—ฐ๊ด€ ์žˆ๋Š” ์Šคํƒ€์ผ ์†์„ฑ๋ผ๋ฆฌ ๋ฌถ์–ด์„œ ๋งŒ๋“ค์–ด์•ผ ์ข€ ๋” ์‚ฌ์šฉ์„ฑ์ด ๋†’์€ mixin์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
ย 

3. Arguments

1) ์ธ์ˆ˜(Arguments)
mixin ์ด๋ฆ„ ๋’ค์— ์ธ์ˆ˜๋ฅผ ๋„ฃ์–ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ผ๋ฐ˜ ์–ธ์–ด์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋งค๊ฐœ๋ณ€์ˆ˜์™€ ์ธ์ˆ˜์˜ ๊ฐœ์ˆ˜๊ฐ€ ๊ฐ™์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. mixin์— ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, ๋Šฅ๋™์ ์œผ๋กœ ์Šคํƒ€์ผ์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. mixin์˜ ๋งค๊ฐœ๋ณ€์ˆ˜์— ๋“ค์–ด๊ฐ€๋Š” ์ธ์ž๋“ค์˜ ๊ฐ’์— ๋”ฐ๋ผ ํ˜•ํƒœ๋Š” ๊ฐ™์ง€๋งŒ ์Šคํƒ€์ผ์ด ์กฐ๊ธˆ์”ฉ ๋‹ฌ๋ผ์ง€๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.
@mixin image-style($ul, $size, $repeat, $positionX, $positionY) { background-image: url($ul); background-size: $size; background-repeat: $repeat; background-position: $positionX $positionY; } //background๊ด€๋ จ ์Šคํƒ€์ผ ์ฝ”๋“œ๊ฐ€ ๋“ค์–ด์žˆ๋‹ค. //mixin์˜ ์ธ์ˆ˜์— ๋”ฐ๋ผ ์กฐ๊ธˆ์”ฉ ๋‹ค๋ฅธ ์Šคํƒ€์ผ๋ง์ด ๊ฐ€๋Šฅํ•˜๋‹ค. .profile { @include image-style("./assets/user.jpg", cover, no-repeat, center, center); } .box-one { @include image-style(url("/images/poster1.svg"), cover, no-repeat, 40%, 50%); }
.profile { background-image: url("./assets/user.jpg"); background-size: cover; background-repeat: no-repeat; background-position: center center; } .box-one { background-image: url(url("/images/poster1.svg")); background-size: cover; background-repeat: no-repeat; background-position: 40% 50%; }
ย 
2) ๊ธฐ๋ณธ๊ฐ’ ์„ค์ •
๊ธฐ๋ณธ๊ฐ’์„ ์„ค์ •ํ•˜์—ฌ ๋งค๊ฐœ๋ณ€์ˆ˜์— ๊ฐ’์ด ๋“ค์–ด์˜ค์ง€ ์•Š์„ ๋•Œ ๊ธฐ๋ณธ์œผ๋กœ ์„ค์ •ํ•œ ๊ฐ’์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค๋‹ˆ๋‹ค.
// ์œ„์— ์ฝ”๋“œ๋ฅผ ๊ฐ€์ ธ์™€์„œ ๊ธฐ๋ณธ๊ฐ’์„ ์„ค์ •ํ•ด์ฃผ์—ˆ๋‹ค. @mixin image-style($ul, $size, $repeat, $positionX : 50%, $positionY : 50%) { background-image: url($ul); background-size: $size; background-repeat: $repeat; background-position: $positionX $positionY; } .profile { @include image-style("./assets/user.jpg", cover, no-repeat); } // profile๊ณผ .box-one์€ ์„œ๋กœ ๊ด€๊ณ„๊ฐ€ ์—†์ง€๋งŒ, ์ฝ”๋“œ๊ฐ€ ์ค‘๋ณต๋˜๊ธฐ๋•Œ๋ฌธ์— mixin์„ ๋งŒ๋“ค์–ด์„œ // ๊ฐ ์š”์†Œ์—์„œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
.profile { background-image: url("./assets/user.jpg"); background-size: cover; background-repeat: no-repeat; background-position: 50% 50%; }
ย 

4. Content

@content๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์›ํ•˜๋Š” ๋ถ€๋ถ„์— ์Šคํƒ€์ผ์„ ์ถ”๊ฐ€ํ•˜์—ฌ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
// ์ •์˜ํ•˜๋Š” ๊ณณ์—์„œ @mixin sample{ display: flex; justify-content: center; align-items: center; @content }
// ์‚ฌ์šฉํ•˜๋Š” ๊ณณ์—์„œ (์ถ”๊ฐ€๋กœ ์Šคํƒ€์ผ๋ง์„ ํ•  ์ˆ˜ ์žˆ์Œ) a { @include sample{ color: white; } }
ย 

5. ์ „๋‹ฌ์ธ์ž ์—†๋Š” ๋ฏน์Šค์ธ

๋ฏน์Šค์ธ์€ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง€์ง€ ์•Š๋”๋ผ๋„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ „๋‹ฌ์ธ์ž๊ฐ€ ์—†๋Š” ๋ฏน์Šค์ธ์—์„œ๋Š” @ inlcudeํ‚ค์›Œ๋“œ์—๋‹ค๊ฐ€ ๋ฏน์Šค์ธ ์ด๋ฆ„๋งŒ ๋„ฃ์–ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋กœ ๊ด„ํ˜ธ๋ฅผ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
a { @include text-style; }
ย 

6. Example

์˜ˆ์‹œ๋ฅผ ๋ณด๋ฉด์„œ ๋‹ค์‹œ ํ•œ๋ฒˆ ํ™•์ธํ•ด๋ด…์‹œ๋‹ค.
// Scss // box์˜ ์‚ฌ์ด์ฆˆ๋ฅผ ์ •ํ•ด์ฃผ๋Š” mixin @mixin size($width, $height, $padding){ width : $width; height : $height; padding : $padding; } article{ @include size(100%, 65px, 10px 20px); }
/*CSS*/ article { width: 100%; height: 65px; padding: 10px 20px; }

7. ์ „์ฒด ์‹ค์Šต ์ฝ”๋“œ