본문 바로가기

프로그램 강좌

[SASS]#6 반복문 : 디자이너, 퍼블리셔들을 위한 쉬운 SASS


반복문


while

말 그대로 조건값까지 반복하는거예요. 저도 아직 익숙하진 않아서 이번 예제 만들면서 써봤네요.


index.html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <Link href="css/style.css" rel="stylesheet" />
    <title>sass연습</title>
</head>

<body>
    <div class="box-1">박스1</div>
    <div class="box-2">박스2</div>
    <div class="box-3">박스3</div>
    <div class="box-4">박스4</div>
    <div class="box-5">박스5</div>

</body>

</html>

 

css/style.scss

$i: 1;
$font-stack: ('Helvetica', 'Arial', sans-serif);

%box {
  height: 200px;
  background: red;
  margin: 20px 0;
  font-family: $font-stack;
}

@while $i <= 5 {
  .box-#{$i} {
    @extend %box;
  }
  $i: $i + 1;
}

자, 결과가 나왔나요? 코드 한번 명령으로 한꺼번에 box css를 줬어요. 물론 예제로 5개까지 밖에 안했으니까 손으로 하는게 더 쉬운거 아니냐 할수도 있는데 100개 넘으면 얘기가 달라지죠.


for to(through)

코딩에서 자주 봤던 for문이예요. to 와 through 가 있는데요. 사실 별로 어려울건 없어요. 끝수를 포함하지 않거나, 포함해서 구하는거예요.


index.html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <Link href="css/style.css" rel="stylesheet" />
    <title>sass연습</title>
</head>

<body>
    <div class="box-1">박스1</div>
    <div class="box-2">박스2</div>
    <div class="box-3">박스3</div>
    <div class="box-4">박스4</div>
    <div class="box-5">박스5</div>

</body>

</html>


css/style.scss

$i: 1;
$font-stack: ('Helvetica', 'Arial', sans-serif);

%box {
  height: 200px;
  background: red;
  margin: 20px 0;
  font-family: $font-stack;
}

@for $i from 1 to 5 {
  .box-#{$i} {
    @extend %box;
  }
}

결과를 보면 박스4까지만 css가 적용되었고, 5는 적용이 안되었어요.
박스5까지 적용하고 싶다면 through를 써보면 되겠네요.


읽어주셔서 감사드리고, 드디어 완강이네요.
지금까지 SASS 강의였습니다.


2019/07/11 - [프로그램 강좌] - [SASS]#1 설치방법, 환경구성, 작성규칙 : 디자이너, 퍼블리셔들을 위한 쉬운 SASS

2019/07/13 - [프로그램 강좌] - [SASS]#2 주석, 변수 : 디자이너, 퍼블리셔들을 위한 쉬운 SASS

2019/07/15 - [프로그램 강좌] - [SASS]#3 Extend, Mixin : 디자이너, 퍼블리셔들을 위한 쉬운 SASS

2019/07/17 - [프로그램 강좌] - [SASS]#4 Function placeholder : 디자이너, 퍼블리셔들을 위한 쉬운 SASS

2019/07/19 - [프로그램 강좌] - [SASS]#5 조건문, 삼항연산자 : 디자이너, 퍼블리셔들을 위한 쉬운 SASS



 


728x90