์ƒˆ์‹น

[์ƒˆ์‹น ํ”„๋ก ํŠธ์—”๋“œ] scss ๋ฐ˜๋ณต๋ฌธ ์‚ฌ์šฉํ•ด๋ณด๊ธฐ

ksh21 2022. 12. 5. 20:39

๐Ÿงก INTRO

์ƒˆ์‹น์—์„œ SCSS๋ฅผ ๋ฐฐ์šฐ๊ณ  ์‹ค์Šต ๋ฌธ์ œ๋ฅผ ์ง„ํ–‰ํ–ˆ๋Š”๋ฐ SCSS์˜ ์ค‘์ฒฉ ๊ธฐ๋Šฅ๋งŒ ์ฃผ๋กœ ์‚ฌ์šฉํ•˜๋‹ค๊ฐ€ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ๋“ค์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์–ด ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค 

 

 

๐Ÿ“ƒ ๋ฌธ์ œ

 

์ฒ˜์Œ ์ž‘์„ฑํ•œ ์ฝ”๋“œ

 

 

์ฒ˜์Œ์— ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ์„ ๋•Œ ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๊ตณ์ด scss๋ฅผ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์žˆ๋‚˜? ์‹ถ์„ ์ •๋„๋กœ ์ฝ”๋“œ๊ฐ€ ๊ธธ์—ˆ์Šต๋‹ˆ๋‹ค๐Ÿ˜‚ 

 

@mixin box($size, $color) {
  width: $size;
  height: $size;
  background-color: $color;
}

.container {
  display: flex;

  .box1 {
    @include box(50px, red);
  }
  .box2 {
    @include box(100px, orange);
  }
  .box3 {
    @include box(150px, yellow);
  }
  .box4 {
    @include box(200px, green);
  }
  .box5 {
    @include box(250px, blue);
  }
  .box6 {
    @include box(300px, navy);
  }
  .box7 {
    @include box(350px, purple);
  }
}

 

 

๊ทธ๋ƒฅ ์ œ์ถœํ• ๊นŒ ์ƒ๊ฐํ–ˆ์ง€๋งŒ ๋” ์ข‹์€ ๋ฐฉ๋ฒ•์ด ์žˆ์„ ๊ฒƒ ๊ฐ™์•„ scss์˜ ๊ธฐ๋Šฅ์— ๋Œ€ํ•ด ์ด๊ฒƒ ์ €๊ฒƒ ๊ฒ€์ƒ‰ํ•ด๋ณด๋‹ค๊ฐ€ scss์—์„œ ๋ฐ˜๋ณต๋ฌธ์„ ์“ธ ์ˆ˜ ์žˆ๋‹ค๋Š” ์‚ฌ์‹ค์„ ์•Œ๊ฒŒ ๋˜์—ˆ๊ณ  ์ด๋ฅผ ์ ์šฉํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค!! ์—ฌ๋Ÿฌ ๋ฐ˜๋ณต๋ฌธ์„ ์“ธ ์ˆ˜ ์žˆ์—ˆ๋Š”๋ฐ ์ฒ˜์Œ์—” for ๋ฐ˜๋ณต๋ฌธ๊ณผ each ๋ฐ˜๋ณต๋ฌธ์˜ ์‚ฌ์šฉ๋ฒ•์„ ์ฐพ์•„๋ณด๊ณ  ๋‘˜์„ ํ˜ผํ•ฉํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.

 

//for ๋ฐ˜๋ณต๋ฌธ๊ณผ each ๋ฐ˜๋ณต๋ฌธ์˜ ์‚ฌ์šฉ๋ฒ•โœ๏ธ 


//for ๋ฐ˜๋ณต๋ฌธ ์‚ฌ์šฉ ๋ฐฉ๋ฒ• 
@for ๋ณ€์ˆ˜ from (์‹œ์ž‘ ์ˆซ์ž) to (๋ ์ˆซ์ž){...}


//each ๋ฐ˜๋ณต๋ฌธ index ๊ฐ’์ด ํ•„์š”ํ•  ๊ฒฝ์šฐ ์‚ฌ์šฉ ์˜ˆ์‹œ
$fruits : (apple, banana, mango, orange);
.list_fruits {
    @each $fruit in $fruits {
        $i : index($fruits, $fruit);
        li:nth-child(#{$i}) {
            left: 100px * $i;
        }
    }
}

 

 

๋‘ ๋ฒˆ์งธ๋กœ ์ž‘์„ฑํ•œ ์ฝ”๋“œ(๋ฐ˜๋ณต๋ฌธ ์‚ฌ์šฉ)

 

//๋‘๋ฒˆ ์งธ ์ฝ”๋“œ

$default-size: 50px;
$colors: (red, orange, yellow, green, blue, navy, purple);

@mixin boxs($num) {
	width: $default-size * $num;
	height: $default-size * $num;
}

.container {
	display: flex;

	@for $i from 1 to 8 {
		.box_#{$i} {
			@include boxs($i);
		}
	}

	@each $color in $colors {
		$i: index($colors, $color);
		.box_#{$i} {
			background: $color;
		}
	}
}

 

ํ•˜์ง€๋งŒ ์‹คํ–‰ํ•ด๋ณด๋‹ˆ ํ™”๋ฉด์—๋Š” ์ž˜ ๋œจ๋Š”๋ฐ ์•„๋ž˜์ฒ˜๋Ÿผ ๋ฐ˜๋ณต๋ฌธ์ด ๋‘ ๋ฒˆ ๋Œ์•„๊ฐ€๊ณ  ์žˆ์–ด์„œ ์ „ํ˜€ ํšจ์œจ์ ์œผ๋กœ ๋ณด์ด์ง€๊ฐ€ ์•Š์•˜์Šต๋‹ˆ๋‹ค๐Ÿคฃ

 

 

 

์ตœ์ข… ์ฝ”๋“œ

 

each ๋ฐ˜๋ณต๋ฌธ์—์„œ index ๊ฐ’์„ ์“ฐ๋‹ˆ๊นŒ for๋ฌธ์„ ๊ตณ์ด ๋Œ๋ฆด ํ•„์š”๊ฐ€ ์—†์„ ๊ฒƒ ๊ฐ™์•„์„œ @include ๋ถ€๋ถ„์„ ๋ฐ‘์— ๋‚ด๋ ค์คฌ์Šต๋‹ˆ๋‹ค

 

//์ตœ์ข… ์ฝ”๋“œ

$default-size: 50px;
$colors: (red, orange, yellow, green, blue, navy, purple);

@mixin boxs($num) {
	width: $default-size * $num;
	height: $default-size * $num;
}

.container {
	display: flex;

	@each $color in $colors {
		$i: index($colors, $color);
		.box_#{$i} {
			@include boxs($i);
			background: $color;
		}
	}
}

 

๊ทธ๋žฌ๋”๋‹ˆ ๋“œ๋””์–ด ์ œ๊ฐ€ ์›ํ–ˆ๋˜ ๋Œ€๋กœ ์ปดํŒŒ์ผ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค!!!๐Ÿ’›

 

 

 

๐Ÿงก ๋งˆ๋ฌด๋ฆฌ

์ตœ๋Œ€ํ•œ scss์˜ ์žฅ์ ์„ ์‚ด๋ ค์„œ ์ฝ”๋“œ๋ฅผ ์งœ๋ณด๊ณ  ์‹ถ์–ด์„œ ์ด๊ฒƒ ์ €๊ฒƒ ๊ฒ€์ƒ‰ํ•ด๋ณด๊ณ  ๋ฐ˜๋ณต๋ฌธ๊นŒ์ง€ ์จ ๋ดค๋Š”๋ฐ ๊ฐ€์žฅ ๋ฐ”๋žŒ์งํ•œ ์ฝ”๋“œ์ธ์ง€๋Š” ๋ชจ๋ฅด๊ฒ ์ง€๋งŒ ์ฒ˜์Œ ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋ณด๋‹ค๋Š” ๋งค์šฐ ์งง์•„์ ธ์„œ ๋งŒ์กฑํ•˜๊ณ  scss์— ๋งŽ์€ ๊ธฐ๋Šฅ์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์–ด์„œ ํ•„์š”ํ•  ๋•Œ๋งˆ๋‹ค ์ฐพ์•„๋ณด๊ณ  ์ ์šฉํ•ด๋ณด๋ฉด ๋งค์šฐ ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค๋Š” ์ƒ๊ฐ์„ ํ–ˆ์Šต๋‹ˆ๋‹ค!!

 

 

 ๐Ÿ“• ์ฐธ๊ณ 

https://abcdqbbq.tistory.com/83

 

'์ƒˆ์‹นDT ๊ธฐ์—…์—ฐ๊ณ„ํ˜• ํ”„๋ก ํŠธ์—”๋“œ ์‹ค๋ฌด ํ”„๋กœ์ ํŠธ ๊ณผ์ • 7์ฃผ์ฐจ ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŒ…'