동영상 강좌는 유튜브 채널 '웹학교'를 이용하시기 바랍니다.
Sass Mixins
@mixin 지시어를 사용하면 웹 사이트 전체에서 재사용 할 CSS 코드를 만들 수 있습니다.
@include 지시문은 여러분이 mixin을 사용(포함) 할 수 있도록 만들어졌습니다.
믹스인(mixin) 정의하기
mixin은 @mixin 지시어로 정의됩니다.
@mixin name {
property: value;
property: value;
...
}
다음 예제는 "important-text"라는 이름의 mixin을 생성합니다 :
@mixin important-text {
color: red;
font-size: 25px;
font-weight: bold;
border: 1px solid blue;
}
** Sass의 하이픈(-) 및 밑줄(_)에 대한 팁 : 하이픈과 밑줄은 동일한 것으로 간주됩니다.
** 이것은 @mixin important-text {}와 @mixin important_text {}가 같은 mixin으로 간주된다는 것을 의미합니다!
믹스인(Mixin) 사용하기
@include 지시문은 mixin을 포함하는 데 사용됩니다.
selector {
@include mixin-name;
}
위에서 작성한 important-text 믹스를 포함하려면 다음을 수행하십시오.
.danger {
@include: important-text;
background-color: green;
}
Sass transpiler는 위의 내용을 일반 CSS로 변환합니다 :
.danger {
color: red;
font-size: 25px;
font-weight: bold;
border: 1px solid blue;
background-color: green;
}
mixin에는 다른 mixin도 포함될 수 있습니다.
@mixin special-text {
@include: important-text;
@include: link;
@include: special-border;
}
Mixin에 변수 전달하기
Mixins는 인수를 허용합니다. 이렇게 하면 mixin에 변수를 전달할 수 있습니다.
인수를 사용하여 mixin을 정의하는 방법은 다음과 같습니다.
/* Define mixin with two arguments */
@mixin bordered($color, $width) {
border: $width solid $color;
}
.myArticle {
@include bordered(blue, 1px); // Call mixin with two values
}
.myNotes {
@include bordered(red, 2px); // Call mixin with two values
}
인수는 변수로 설정되고 border 속성의 값 (색상 및 너비)으로 사용됩니다.
컴파일이 끝나면 CSS는 다음과 같이 보입니다.
.myArticle {
border: 1px solid blue;
}
.myNotes {
border: 2px solid red;
}
Mixin의 기본값
mixin 변수의 기본값을 정의 할 수도 있습니다.
@mixin bordered($color: blue, $width: 1px) {
border: $width solid $color;
}
그런 다음 mixin을 포함시킬 때 변경되는 값만 지정하면 됩니다.
.myTips {
@include bordered(@color: orange);
}
공급 업체 접두사용 Mixin 사용
mixin의 다른 좋은 사용법은 공급 업체 접두어입니다.
@mixin transform($property) {
-webkit-transform: $property;
-ms-transform: $property;
transform: $property;
}
.myBox {
@include transform(rotate(20deg));
}
컴파일이 끝나면 CSS는 다음과 같이 보입니다.
.myBox {
-webkit-transform: rotate(20deg);
-ms-transform: rotate(20deg);
transform: rotate(20deg);
}
등록된 댓글이 없습니다.