HTML & CSS/SASS(SCSS)

Sass/SCSS 문법 3. Lists

breeghty 2023. 1. 26. 20:18

1. Lists(목록형 변수)

리스트는 순서가 있는 값이다. lists의 요소들을 쉼표공백 또는 / 로 구분하여 생성하고, 다른 타입의 변수들과 다르게 특수 괄호를 사용하지 않아도 lists로 인식한다.

빈 lists를 만들 때나 lists에 들어있는 값이 하나인 경우, [] 나 ()를 사용하여 생성한다.

lists 값의 인덱스는 1부터 시작한다.

 

<body>
    <button class="first-btn">첫번째</button>
    <button class="second-btn">두번째</button>
    <button class="third-btn">세번째</button>
</body>
//SCSS

$main-color: orange;
$font-color:white;
$btn-width: 100px, 200px, 300px;

button{
    &:nth-child(1){
        color:$font-color;
        background:$main-color;
        width: nth($btn-width,1);
        // width: 100px
    }
    &:nth-child(2){
        color:$font-color;
        background: $main-color;
        width: nth($btn-width,2);
        //width: 200px
    }
    &:nth-child(3){
        color:$font-color;
        background: $main-color;
        width: nth($btn-width,3);
        //width: 300px
    }
}

//CSS

button:nth-child(1) {
  color: white;
  background: orange;
  width: 100px;
}
button:nth-child(2) {
  color: white;
  background: orange;
  width: 200px;
}
button:nth-child(3) {
  color: white;
  background: orange;
  width: 300px;
}/*# sourceMappingURL=style.css.map */

결과

2. Lists 내장함수

  • nth(list명, index): lists의 인덱스에 해당하는 값을 리턴하는 함수
  • index(list, value): lists의 값에 대한 인덱스를 리턴하는 함수
  • append(list, value, [separator]): lists의 값을 추가하는 함수