๋ณธ๋ฌธ์œผ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

Syntax-09-flow-control

Sass(SCSS) Syntax - 9. ํ๋ฆ„ ์ œ์–ด(Flow Control)#

โ—๏ธ ํ•ด๋‹น ๊ธ€์€ ํŒจ์ŠคํŠธ์บ ํผ์Šค - ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ ๊ฐ•์˜์—์„œ HTML & CSS, SASS(SCSS) Part์˜ ๋ฐ•์˜์›… ๊ฐ•์‚ฌ๋‹˜์˜ ๊ฐ•์˜์ž๋ฃŒ(Sass(SCSS) ์™„์ „ ์ •๋ณต!)๋ฅผ ๋ณด๋ฉฐ ์ •๋ฆฌํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

ํ๋ฆ„ ์ œ์–ด(Flow Control)#

if (ํ•จ์ˆ˜)#

์กฐ๊ฑด์˜ ๊ฐ’( true , false )์— ๋”ฐ๋ผ ๋‘ ๊ฐœ์˜ ํ‘œํ˜„์‹ ์ค‘ ํ•˜๋‚˜๋งŒ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

JS์˜ ์กฐ๊ฑด๋ถ€ ์‚ผํ•ญ ์—ฐ์‚ฐ์ž(conditional ternary operator)์™€ ๋น„์Šทํ•œ ๊ฐœ๋…

์กฐ๊ฑด์˜ ๊ฐ’์ด true ์ด๋ฉด ํ‘œํ˜„์‹1 ์„,

์กฐ๊ฑด์˜ ๊ฐ’์ด false ์ด๋ฉด ํ‘œํ˜„์‹2 ๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.

if(์กฐ๊ฑด, ํ‘œํ˜„์‹1, ํ‘œํ˜„์‹2)
$width: 555px;div {  width: if($width > 300px, $width, null);}

์ปดํŒŒ์ผํ•˜๋ฉด

div {width: 555px;}

@if#

@if ๋Š” ์กฐ๊ฑด์ด ๋”ฐ๋ฅธ ๋ถ„๊ธฐ ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.

๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ง€์‹œ์–ด๋กœ๋Š” @else , @else if ๊ฐ€ ์žˆ๋‹ค.

// @if@if (์กฐ๊ฑด) {  /* ์กฐ๊ฑด์ด ์ฐธ์ผ ๋•Œ ๊ตฌ๋ฌธ */}
// @if @else@if (์กฐ๊ฑด) {  /* ์กฐ๊ฑด์ด ์ฐธ์ผ ๋•Œ ๊ตฌ๋ฌธ */} @else {  /* ์กฐ๊ฑด์ด ๊ฑฐ์ง“์ผ ๋•Œ ๊ตฌ๋ฌธ */}
// @if @else if@if (์กฐ๊ฑด1) {  /* ์กฐ๊ฑด1์ด ์ฐธ์ผ ๋•Œ ๊ตฌ๋ฌธ */} @else if (์กฐ๊ฑด2) {  /* ์กฐ๊ฑด 1์ด ๊ฑฐ์ง“์ด๊ณ , ์กฐ๊ฑด2๊ฐ€ ์ฐธ์ผ ๋•Œ ๊ตฌ๋ฌธ */} @else {  /* ๋ชจ๋‘ ๊ฑฐ์ง“์ผ ๋•Œ ๊ตฌ๋ฌธ */}

์กฐ๊ฑด์— () ๋Š” ์ƒ๋žต ๊ฐ€๋Šฅํ•˜๋‹ค.

$color: orange;div {  @if $color == strawberry {    color: #FE2E2E;  } @else if $color == orange {    color: #FE9A2E;  } @else if $color == banana {    color: #FFFF00;  } @else {    color: #2A1B0A;  }}

์ปดํŒŒ์ผํ•˜๋ฉด

div {color: #FE9A2E;}

๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž and , or , not ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

@function limitSize($size) {  @if $size >= 0 and $size <= 200px {    @return 200px;  } @else {    @return 800px;  }}
div {  width: limitSize(180px);  height: limitSize(340px);}

์ปดํŒŒ์ผํ•˜๋ฉด

div {width: 200px;height: 800px;}

๋‚ด์žฅ ํ•จ์ˆ˜ unitless() (์ˆซ์ž์— ๋‹จ์œ„๊ฐ€ ์žˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜)๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ์ œ

@mixin pCenter($w, $h, $p: absolute) {  @if    $p == absolute    or $p == fixed    or not $p == relative    or not $p == static  {    width: if(unitless($w), #{$w}px, $w);    height: if(unitless($h), #{$h}px, $h);    position: $p;    top: 0;    bottom: 0;    left: 0;    right: 0;    margin: auto;  }}
.box1 {  @include pCenter(10px, 20px);}.box2 {  @include pCenter(50, 50, fixed);}.box3 {  @include pCenter(100, 200, relative);}

์ปดํŒŒ์ผํ•˜๋ฉด

.box1 {width: 10px;height: 20px;position: absolute;top: 0;bottom: 0;left: 0;right: 0;margin: auto;}.box2 {width: 50px;height: 50px;position: fixed;top: 0;bottom: 0;left: 0;right: 0;margin: auto;}

@for#

@for ๋Š” ์Šคํƒ€์ผ์„ ๋ฐ˜๋ณต์ ์œผ๋กœ ์ถœ๋ ฅํ•œ๋‹ค.

@for ๋Š” through ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํ˜•์‹๊ณผ to ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํ˜•์‹์œผ๋กœ ๋‚˜๋‰œ๋‹ค.

๋‘ ํ˜•์‹์€ ์ข…๋ฃŒ ์กฐ๊ฑด์ด ํ•ด์„๋˜๋Š” ๋ฐฉ์‹์ด ๋‹ค๋ฅด๋‹ค.

// through// ์ข…๋ฃŒ ๋งŒํผ ๋ฐ˜๋ณต@for $๋ณ€์ˆ˜ from ์‹œ์ž‘ through ์ข…๋ฃŒ {  // ๋ฐ˜๋ณต ๋‚ด์šฉ}
// to// ์ข…๋ฃŒ ์ง์ „๊นŒ์ง€ ๋ฐ˜๋ณต@for $๋ณ€์ˆ˜ from ์‹œ์ž‘ to ์ข…๋ฃŒ {  // ๋ฐ˜๋ณต ๋‚ด์šฉ}

์ฐจ์ด์ ์„ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•œ ๋‹ค์Œ ์˜ˆ์ œ

๋ณ€์ˆ˜๋Š” ๊ด€๋ก€์ƒ $i ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

// 1๋ถ€ํ„ฐ 3๋ฒˆ ๋ฐ˜๋ณต@for $i from 1 through 3 {  .through:nth-child(#{$i}) {    width : 20px * $i  }}
// 1๋ถ€ํ„ฐ 3 ์ง์ „๊นŒ์ง€๋งŒ ๋ฐ˜๋ณต(2๋ฒˆ ๋ฐ˜๋ณต)@for $i from 1 to 3 {  .to:nth-child(#{$i}) {    width : 20px * $i  }}

์ปดํŒŒ์ผํ•˜๋ฉด

.through:nth-child(1) { width: 20px; }.through:nth-child(2) { width: 40px; }.through:nth-child(3) { width: 60px; }.to:nth-child(1) { width: 20px; }.to:nth-child(2) { width: 40px; }

to ๋Š” ์ฃผ์–ด์ง„ ๊ฐ’ ์ง์ „๊นŒ์ง€๋งŒ ๋ฐ˜๋ณตํ•ด์•ผํ•  ๊ฒฝ์šฐ ์‚ฌ์šฉ.

ํ•˜์ง€๋งŒ :nth-child() ์—์„œ ํŠนํžˆ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉ๋˜๋Š” @for ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ through ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

@each#

@each ๋Š” List์™€ Map ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜๋ณตํ•  ๋•Œ ์‚ฌ์šฉ.

for in ๋ฌธ๊ณผ ์œ ์‚ฌํ•˜๋‹ค.

@each $๋ณ€์ˆ˜ in ๋ฐ์ดํ„ฐ {  // ๋ฐ˜๋ณต ๋‚ด์šฉ}

List ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜๋ณตํ•˜๋Š” ์˜ˆ์ œ.

// List Data$fruits: (apple, orange, banana, mango);
.fruits {  @each $fruit in $fruits {    li.#{$fruit} {      background: url("/images/#{$fruit}.png");    }  }}

์ปดํŒŒ์ผํ•˜๋ฉด

.fruits li.apple {background: url("/images/apple.png");}.fruits li.orange {background: url("/images/orange.png");}.fruits li.banana {background: url("/images/banana.png");}.fruits li.mango {background: url("/images/mango.png");}

๋งค๋ฒˆ ๋ฐ˜๋ณต๋งˆ๋‹ค Index ๊ฐ’์ด ํ•„์š”ํ•  ๊ฒฝ์šฐ์— index() ๋‚ด์žฅ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

$fruits: (apple, orange, banana, mango);
.fruits {  @each $fruit in $fruits {    $i: index($fruits, $fruit);    li:nth-child(#{$i}) {      left: 50px * $i;    }  }}

์ปดํŒŒ์ผํ•˜๋ฉด

.fruits li:nth-child(1) {left: 50px;}.fruits li:nth-child(2) {left: 100px;}.fruits li:nth-child(3) {left: 150px;}.fruits li:nth-child(4) {left: 200px;}

๋™์‹œ์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ List ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜๋ณต ์ฒ˜๋ฆฌํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

$apple: (apple, korea);$orange: (orange, china);$banana: (banana, japan);
@each $fruit, $country in $apple, $orange, $banana {  .box-#{$fruit} {    background: url("/images/#{$country}.png");  }}

์ปดํŒŒ์ผํ•˜๋ฉด

.box-apple {background: url("/images/korea.png");}.box-orange {background: url("/images/china.png");}.box-banana {background: url("/images/japan.png");}

Map ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜๋ณตํ•  ๊ฒฝ์šฐ์—๋Š” ํ•˜๋‚˜์˜ ๋ฐ์ดํ„ฐ์— ๋‘ ๊ฐœ์˜ ๋ณ€์ˆ˜๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

@each $key๋ณ€์ˆ˜, $value๋ณ€์ˆ˜ in ๋ฐ์ดํ„ฐ {  // ๋ฐ˜๋ณต ๋‚ด์šฉ}
$fruits-data: (  apple: korea,  orange: china,  banana: japan);
@each $fruit, $country in $fruits-data {  .box-#{$fruit} {    background: url("/images/#{$country}.png");  }}

์ปดํŒŒ์ผํ•˜๋ฉด

.box-apple {background: url("/images/korea.png");}.box-orange {background: url("/images/china.png");}.box-banana {background: url("/images/japan.png");}

@while#

@while ์€ ์กฐ๊ฑด์ด false ๋กœ ํ‰๊ฐ€๋  ๋•Œ๊นŒ์ง€ ๋‚ด์šฉ์„ ๋ฐ˜๋ณตํ•œ๋‹ค. while ๋ฌธ๊ณผ ์œ ์‚ฌํ•˜๊ฒŒ ์ž˜๋ชป๋œ ์กฐ๊ฑด์œผ๋กœ ์ธํ•ด ์ปดํŒŒ์ผ ์ค‘ ๋ฌดํ•œ ๋ฃจํ”„์— ๋น ์งˆ ์ˆ˜ ์žˆ๋‹ค.

@while ์กฐ๊ฑด {  // ๋ฐ˜๋ณต ๋‚ด์šฉ}
$i: 6;
@while $i > 0 {  .item-#{$i} {    width: 2px * $i;  }  $i: $i - 2;}

์ปดํŒŒ์ผํ•˜๋ฉด

.item-6 { width: 12px; }.item-4 { width: 8px; }.item-2 { width: 4px; }

Reference#

ํŒจ์ŠคํŠธ์บ ํผ์Šค - ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ ๊ฐ•์˜ - HTML & CSS, SASS(SCSS) Part by ParkYoungWoong

Sass(SCSS) ์™„์ „ ์ •๋ณต!