1. Mixin์ด๋2. Mixin ์ฌ์ฉํ๊ธฐ3. Arguments4. Content 5. ์ ๋ฌ์ธ์ ์๋ ๋ฏน์ค์ธ6. Example 7. ์ ์ฒด ์ค์ต ์ฝ๋
ย
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; }