> For the complete documentation index, see [llms.txt](https://yamoo9.gitbook.io/scss-mixins/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://yamoo9.gitbook.io/scss-mixins/mixins/spacing/margin.md).

# margin()

## 용도 <a href="#use" id="use"></a>

마진(margin)의 개별 속성을 빠르게 설정할 때 사용하는 믹스인입니다.

## 사용법 <a href="#usage" id="usage"></a>

`margin()` 믹스인에 설정 가능한 값을 리스트로 전달합니다.

> margin($args:**list**)

```ruby
.usage {
  @include margin(l 20 r 24 y 10);
}
```

| 매개변수(parameter) | 유형(type) | 필수(required) | 기본 값(default) |
| :-------------: | :------: | :----------: | :-----------: |
|      $args      |   list   |      ✔︎      |               |

위 예시 코드는 아래의 CSS로 컴파일 되어 출력됩니다.

```css
.usage {
  margin-top: 10px;
  margin-right: 24px;
  margin-bottom: 10px;
  margin-left: 20px;
}
```

#### 믹스인에 설정 가능한 속성 키워드 및 값 유형

| 속성 키워드          | 값 유형                                   | 컴파일 속성                        |
| --------------- | -------------------------------------- | ----------------------------- |
| `top` 또는 `t`    | `number`, `number(unitless)`, `string` | `margin-top`                  |
| `right` 또는 `r`  | `number`, `number(unitless)`, `string` | `margin-right`                |
| `bottom` 또는 `b` | `number`, `number(unitless)`, `string` | `margin-bottom`               |
| `left` 또는 `l`   | `number`, `number(unitless)`, `string` | `margin-left`                 |
| `x`             | `number`, `number(unitless)`, `string` | `margin-right`, `margin-left` |
| `y`             | `number`, `number(unitless)`, `string` | `margin-top`, `margin-bottom` |

## 로직 <a href="#logic" id="logic"></a>

`margin()` 믹스인은 다음의 로직에 의해 작성되었습니다.&#x20;

```javascript
@mixin margin($args...) {
  @if length($args) == 0 {
    @error 'margin() 믹스인은 margin 속성의 설정 방향 top, right, bottom, left와 값을 연이어 전달 받을 수 있습니다. 뿐만 아니라 x, y 방향으로 속성을 일괄 설정 가능합니다.';
  }

  @each $value in $args {
    $type: type-of($value);

    @if $type == 'number' or $type == 'string' {
      margin: get-number-or-string($value);
    }

    @if $type == 'list' {
      @if is-include-items($value, top t) {
        margin-top: get-number-or-string(
          get-match-value-of-keys($value, top t)
        );
      }
      @if is-include-items($value, right r) {
        margin-right: get-number-or-string(
          get-match-value-of-keys($value, right r)
        );
      }
      @if is-include-items($value, bottom b) {
        margin-bottom: get-number-or-string(
          get-match-value-of-keys($value, bottom b)
        );
      }
      @if is-include-items($value, left l) {
        margin-left: get-number-or-string(
          get-match-value-of-keys($value, left l)
        );
      }
      @if is-include-items($value, x) {
        $x-value: get-value-after-key($value, x);
        margin-left: get-number-or-string($x-value);
        margin-right: get-number-or-string($x-value);
      }
      @if is-include-items($value, y) {
        $y-value: get-value-after-key($value, y);
        margin-top: get-number-or-string($y-value);
        margin-bottom: get-number-or-string($y-value);
      }
    }
  }
}
```

1. 전달 받은 인자가 없을 경우, 오류 메시지 출력
2. 전달 받은 인자가 있을 경우, 인자의 타입을 확인
3. 타입이 숫자 또는 문자인 경우, [get-number-or-string()](/scss-mixins/utilities/units/get-number-or-string.md) 믹스인 처리
4. 타입이 리스트인 경우, 전달 인자의 키워드를 분석하여 개별 속성 처리

## 참고 <a href="#reference" id="reference"></a>

믹스인에 사용된 Sass의 빌트인 모듈은 다음과 같습니다.

* [@mixin / @include](https://sass-lang.com/documentation/at-rules/mixin)
* [@if / @else](https://sass-lang.com/documentation/at-rules/control/if)
* [@error](https://sass-lang.com/documentation/at-rules/error)
* [length()](https://sass-lang.com/documentation/modules/list#length)
* [type-of()](https://sass-lang.com/documentation/modules/meta#type-of)

믹스인에 사용된 자체 제작 모듈은 다음과 같습니다.

* [get-match-value-of-keys()](/scss-mixins/utilities/list/get-match-value-of-keys.md)
* [get-number-or-string()](/scss-mixins/utilities/units/get-number-or-string.md)
* [is-include-items()](/scss-mixins/utilities/validation/is-include-items.md)
