• 회원가입
  • 로그인
  • 구글아이디로 로그인

[basic] SASS - @mixin (믹스인)・@include (인클루드)

1,053  

@mixin (믹스인) 정의

 

@mixin (믹스인)

웹사이트 전체에 걸쳐 재사용 가능한 CSS 코드 정의.

 

@include

@mixin (믹스인)을 불러올 때 사용.


 

@mixin (믹스인) 구문 - 믹스인 정의

[구문]

 

@mixin name {

  property: value;

  property: value;

  ...

}

 


[예제]

 

@mixin important-text {

  color: red;

  font-size: 25px;

  font-weight: bold;

  border: 1px solid blue;

}

 


[Tip]

 

SASS 경우, - (하이픈)과 _ (언더바) 경우 동일 코드로 간주.

즉, 아래 두 코드는 동일한 코드로 간주됨.

@mixin important-text {  ... 

@mixin important_text { ...

 


@include (인클루드) 구문 - 믹스인 사용.

[구문]

 

selector {

  @include mixin-name;

}

 


[예제] - 위에서 정의된 mixin을 인클루드 후 변환된 CSS.

 

[SCSS]

.danger {

  @include important-text;

  background-color: green;

}

 

[CSS]

.danger {

  color: red;

  font-size: 25px;

  font-weight: bold;

  border: 1px solid blue;

  background-color: green;

}

 


[Tip]

 

Mixin 경우, 믹스인 안에 다른 Mixin을 인클루드 가능.

(예제)

@mixin special-text {

    @include important-text;

    @include link;

    @include special-border;

}

 

 

Mixin (매개변수・독립변수) 설정

SCSS 

 

@mixin bordered($color, $width) {

    border: $width solid $color;

}


. a {

    @include bordered(blue, 1px);

}


.b {

    @include bordered(red, 2px);

}

 


CSS

 

.a {

    border: 1px solid blue;

}


.b {

    border: 2px solid red;

}

 

 

Mixin 매개변수 기본값 정의

SCSS

 

@mixin bordered($color: blue, $width: 1px) {

    border: $width solid $color;

}


.a {

    @include bordered($color: orange);

}

 


CSS

 

.a {

  border: 1px solid orange;

}

 


Mixin 예제 - Vendor prefix (브라우저접두어)

SCSS

 

@mixin transform($property) {

  -webkit-transform: $property;

  -ms-transform: $property;

  transform: $property;

}


.a {

  @include transform(rotate(20deg));

}

 


CSS

 

.a {

  -webkit-transform: rotate(20deg);

  -ms-transform: rotate(20deg);

  transform: rotate(20deg);

}

 



분류 제목
basic SASS - Home (입문)
basic SASS - Intro (소개)・Comments (주석)
basic SASS - Installation (설치)
basic SASS - Variables (변수)
basic SASS - Nesting (안긴 형태)
basic SASS - @import (가져오기)
basic SASS - @mixin (믹스인)・@include (인클루드)
basic SASS - @extend (상속・확장)
func SASS - String Functions (문자열 함수) 종류
func SASS - Numeric Functions (숫자 함수) 종류
func SASS - List Functions (리스트 함수) 종류
func SASS - Map Functions (맵 함수) 종류
func SASS - Selector Functions (선택자 함수) 종류
func SASS - Introspection Functions (내부검사 함수) 종류
func SASS - Color Functions (색상/색깔 함수) 종류
목록
찾아주셔서 감사합니다. Since 2012