💡깨달음💡/SASS(SCSS)

Sass(SCSS) At-Rules(@mixin, @include)

과부하가즈아 2024. 5. 27. 15:40

@mixin

재사용할 그룹을 선언(정의 및 지정)

/* SCSS 선언 방식 */
@mixin 믹스인 이름 {
  재사용할 스타일 속성들
}

/* Sass 선언 방식(주로 SCSS를 사용하니 참고만) */
= 믹스인 이름 {
  재사용할 스타일 속성들
}

 

@include

정의된 @mixin을 사용(포함)

/* SCSS 사용 방식*/
선택자 {
  @include 믹스인 지정 이름
}

/* Sass 사용 방식*/
선택자 {
  + 믹스인 지정 이름
}

@mixin과 @include 사용

  <body>
    <h1 class="heading">codingworks <span>publishing</span> class</h1>
    <h2 class="new-title">new <span>letter</span> broadcast</h2>
  </body>
/* .scss */

/* Google Web Font - "Montserrat", sans-serif*/
@import url("https://fonts.googleapis.com/css?family=Montserrat:300,400,500&display=swap");

body {
  font-family: "Montserrat", sans-serif;
}

$primary-color: crimson;

@mixin headline {
  text-align: center;
  text-transform: uppercase;
  position: relative;
  padding-bottom: 15px;
  span {
    color: $primary-color;
  }
  &::before {
    content: "";
    position: absolute;
    width: 100px;
    height: 4px;
    background-color: $primary-color;
    bottom: 0;
    left: 50%;
    transform: translateX(-50%);
  }
}

.heading,
.new-title {
  @include headline;
}


@mixin과 @include 사용시 파일 관리

<!-- index.html -->

<!DOCTYPE html>
<html lang="ko">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="style.css" />
    <title>14 @mixin, @include 응용</title>
  </head>
  <body>
    <h1 class="heading">mini web</span> project</h1>
  </body>
</html>

@mixin을 사용해 사용할 속성을 모두 한 곳에 작성

/* mixin.scss */

@mixin default {
  font-family: "Montserrat", sans-serif;
  font-size: 15px;
  margin: 0;
  padding: 0;
  color: #333;
  background-color: #dfbbbb;
  line-height: 1.6em;
}

 

메인이 될 .scss파일에 mixin을 작성한 파일을 import해주고 사용하고자 하는 곳에 include해줌

/* style.scss */

/* Google Web Font - "Montserrat", sans-serif*/
@import url("https://fonts.googleapis.com/css?family=Montserrat:300,400,500&display=swap");

@import "mixin";

body {
  @include default
}

결과적으로 메인이 되는 .css에 이렇게 출력되면서 다음과 같은 결과로 출력된다.

/* style.css */
/* Google Web Font - "Montserrat", sans-serif*/
@import url("https://fonts.googleapis.com/css?family=Montserrat:300,400,500&display=swap");
body {
  font-family: "Montserrat", sans-serif;
  font-size: 15px;
  margin: 0;
  padding: 0;
  color: #333;
  background-color: #dfbbbb;
  line-height: 1.6em;
}


다음과 같은 폴더 구조를 가지게 된다.


<!DOCTYPE html>
<html lang="ko">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="style.css">
  <title>Document</title>
</head>
<body>
  <div class="btns">
    <button class="complete">완료</button>
    <button class="loading">전송중</button>
    <button class="error">오류</button>
  </div>
</body>
</html>
/* style.scss */

/* Google Web Fonts - 'Noto Sans KR', sans-serif */
@import url("https://fonts.googleapis.com/css?family=Noto+Sans+KR:300,400,500,700,900&display=swap");

* {
  font-family: "Noto Sans KR", serif;
}
body {
  margin: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

// Button Mixin
@mixin notice-button {
  font-size: 16px;
  width: 150px;
  padding: 7px;
  background-color: #fff;
  outline: none;
  cursor: pointer;
  border-radius: 5px;
}

.complete {
  @include notice-button;
  border: 3px solid skyblue;
  color: skyblue;
}

.loading {
  @include notice-button;
  border: 3px solid yellowgreen;
  color: yellowgreen;
}
.error {
  @include notice-button;
  border: 3px solid crimson;
  color: crimson;
}

 

빌드된 .css파일

/* style.css */

/* Google Web Fonts - 'Noto Sans KR', sans-serif */
@import url("https://fonts.googleapis.com/css?family=Noto+Sans+KR:300,400,500,700,900&display=swap");
* {
  font-family: "Noto Sans KR", serif;
}

body {
  margin: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

.complete {
  font-size: 16px;
  width: 150px;
  padding: 7px;
  background-color: #fff;
  outline: none;
  cursor: pointer;
  border-radius: 5px;
  border: 3px solid skyblue;
  color: skyblue;
}

.loading {
  font-size: 16px;
  width: 150px;
  padding: 7px;
  background-color: #fff;
  outline: none;
  cursor: pointer;
  border-radius: 5px;
  border: 3px solid yellowgreen;
  color: yellowgreen;
}

.error {
  font-size: 16px;
  width: 150px;
  padding: 7px;
  background-color: #fff;
  outline: none;
  cursor: pointer;
  border-radius: 5px;
  border: 3px solid crimson;
  color: crimson;
}


@mixin에 인수 사용하기

/* 사용방식 */

@mixin 믹스인 이름 ($매개변수, $매개변수) {
  CSS 속성: 인수1 인수2 인수3
}

@include 믹스인 이름(값1, 값2, 값3)

<body>
  <div class="buttons">
    <button class="appoval">승인</button>
    <button class="refuse">거절</button>
  </div>
</body>
/* style.scss */

/* Google Web Fonts - 'Noto Sans KR', sans-serif */
@import url("https://fonts.googleapis.com/css?family=Noto+Sans+KR:300,400,500,700,900&display=swap");

* {
  font-family: "Noto Sans KR", sans-serif;
}

body {
  margin: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

// Buttons mixin
@mixin border-style($width, $style, $color) {
  border: $width $style $color;
}

@mixin button-padding($updown, $leftright) {
  padding: $updown $leftright;
}

.buttons {
  button {
    padding: 7px;
    background-color: #fff;
    outline: none;
    cursor: pointer;
    font-size: 18px;
    @include button-padding(10px, 100px); 
    &.appoval {
      @include border-style(2px, solid, red);
    }
    &.refuse {
      @include border-style(2px, dashed, royalblue);
    }
  }
}
/* 컴파일된 style.css */
/* Google Web Fonts - 'Noto Sans KR', sans-serif */
@import url("https://fonts.googleapis.com/css?family=Noto+Sans+KR:300,400,500,700,900&display=swap");
* {
  font-family: "Noto Sans KR", sans-serif;
}

body {
  margin: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

.buttons button {
  padding: 7px;
  background-color: #fff;
  outline: none;
  cursor: pointer;
  font-size: 18px;
  padding: 10px 100px;
}
.buttons button.appoval {
  border: 2px solid red;
}
.buttons button.refuse {
  border: 2px dashed royalblue;
}


  <body>
    <a href="#none">mini web portfolio</a>
  </body>
/* Google Web Fonts - 'Noto Sans KR', sans-serif */
@import url("https://fonts.googleapis.com/css?family=Noto+Sans+KR:300,400,500,700,900&display=swap");

* {
  font-family: "Noto Sans KR", sans-serif;
}

body {
  margin: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

a {
  text-decoration: none;
  color: #222;
  text-transform: uppercase;
}

@mixin font-color($red, $blue, $green) {
  a {
    &:hover {
      color: $red;
    }
    &:visited {
      color: $blue;
    }
    &:active {
      color: $green;
    }
  }
}

@include font-color(red, blue, green);

 

 

 

🔗참고🔗

https://www.codingfactory.net/10110#Mixin