๐Ÿง‘โ€๐Ÿ’ป SCSS ๋ฌธ๋ฒ• ์ •๋ฆฌ

J_m2nยท2024๋…„ 1์›” 29์ผ
0
post-thumbnail

SCSS ๋ฌธ๋ฒ•์— ๋Œ€ํ•ด ์ •๋ฆฌํ•ด๋ณด๊ณ ์ž ํ•œ๋‹ค.



1. ๋ณ€์ˆ˜ ์„ ์–ธ

SCSS๋Š” ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ๊ฐ–๊ณ  ์žˆ์–ด ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅ

๋ฐ์ดํ„ฐ ํƒ€์ž…
Colors (์ƒ‰์ƒ)red, blue, green ...
Numbers (์ˆซ์ž)1, 100px, 2em...
Strings (๋ฌธ์ž)
Lists (๊ณต๋ฐฑ์ด๋‚˜ ,๋กœ ๊ตฌ๋ถ„๋œ ๊ฐ’์˜ ๋ชฉ๋ก)red blue,(red,blue),...
Maps (Lists์™€ ์œ ์‚ฌํ•˜๋‚˜ Key : Value ํ˜•ํƒœ)(red : a),(blue : b),...
Booleans (๋…ผ๋ฆฌ)true, false
Nulls (์•„๋ฌด ๊ฐ’๋„ ์—†์Œ)null
$color:red;
$number:100px;
$string:springboot;
$list:red,blue,green,...;
$map:(red:r, blue:b,...);
$boolean:true;

๋ฐ˜๋ณต์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๊ฐ’์—๋Š” ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜์—ฌ ๊ฐ„ํŽธํ•˜๊ฒŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

*์ฃผ์˜์‚ฌํ•ญ
๋ณ€์ˆ˜ ์ด๋ฆ„ ์•ž์—๋Š” ๊ผญ $๋ฅผ ๋ถ™์—ฌ์•ผ ํ•œ๋‹ค.

scss

$primary : blue;
$w : 100px;

.btn{
	background : $primary;
    width : $w;
}

css

.btn{
	background : blue;
    width : 100px;
}

์ด๋Ÿฐ์‹์œผ๋กœ ํŠน์ • ๊ฐ’์˜ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

๋‹ค๋งŒ, ๋ณ€์ˆ˜๋Š” ์„ ์–ธ๋œ ๋ธ”๋ก ๋‚ด์—์„œ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ.

.btn1{
	$color : red;
    background : $color;
} /* Okay */

.btn2{
	background : $color;
} /* Error */

โžฃ#{} : ์ฝ”๋“œ์˜ ์–ด๋””๋“ ์ง€ ๋ณ€์ˆ˜๊ฐ’ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

/* scss */
$family: unquote("Droid+Sans");
@import url("http://fonts.googleapis.com/css?family=#{$family}");

/* css */
@import url("http://fonts.googleapis.com/css?family=Droid+Sans");




2. ์ค‘์ฒฉ๋ฌธ ์‚ฌ์šฉ

css์—์„œ๋Š”

.div {
...
}

.div .list{
...
}

.div .list li{
...
}

์ฒ˜๋Ÿผ ์ผ์ผํžˆ ๋‚˜๋ˆ ์„œ ์ง์ ‘ ์ž‘์„ฑํ•ด์•ผ ํ–ˆ์ง€๋งŒ

scss์—์„œ๋Š”

.div{
 ...
	.list{
     ...
    	li{
        ...
        }
    }
}

์œผ๋กœ {์•ˆ์—๋งŒ ์ž˜ ์ž‘์„ฑํ•ด์ฃผ๋ฉด css๋กœ ์ปดํŒŒ์ผ ํ•˜๋Š” ๊ณผ์ •์—์„œ
์ž๋™์œผ๋กœ

.div {
...
}

.div .list{
...
}

.div .list li{
...
}

์ด๋ ‡๊ฒŒ ๋ณ€๊ฒฝํ•ด์ค€๋‹ค.





3. ์ƒ์œ„ ์„ ํƒ์ž ์ฐธ์กฐ (&)

scss

.btn{
	&.active{
     ...
    }
}

.btn{
 ...
	&:hover{
   	 ...
    }
}

css

.btn.active{
 ...
}

.btn{
 ...
}
.btn:hover{
 ...
}




4. Mixin (์žฌํ™œ์šฉ)


์ž‘์„ฑํ•œ css๋ฅผ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ ์ง€์ •ํ•œ ์Šคํƒ€์ผ์„ ๋ฐ˜ํ™˜

@mixin ์œผ๋กœ ์„ ์–ธํ•˜๊ณ  @include ๋กœ ์‚ฌ์šฉ

/* mixin ์„ ์–ธ */
@mixin mine{
	color : red;
    
	&:hover{
    	color : blue;
    }
}

/* include๋กœ ์‚ฌ์šฉ */
.btn1{
	@include mine;
}

.btn2{
	@include mine;
}




5. ํ•จ์ˆ˜ ์‚ฌ์šฉ

@return์„ ํ†ตํ•ด ๊ณ„์‚ฐ๋œ ํŠน์ • ๋ฐ˜ํ™˜ ๊ฐ’์„ ์‚ฌ์šฉํ•จ
scss

$w : 100px;

@function myfunc($number){
	@return $number*3;
}

.div{
	width : $w + myfunc(10);
}

css

.div{
	width : 130px;
}




6. ์กฐ๊ฑด๋ฌธ (if)

ํŠน์ • ์กฐ๊ฑด์—์„œ ์ฐธ ๊ฑฐ์ง“์— ๋”ฐ๋ผ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ธฐ๋Šฅ

1. if(์กฐ๊ฑด, ์ฐธ, ๊ฑฐ์ง“)

/* scss */
$w : 100px;

.btn{
	width : if($w > 100px, $w, null);
}

/* css */
.btn{
	width : 100px;
}


2. @if, @else if, @else

์กฐ๊ฑด์— ๋”ฐ๋ฅธ ๋ถ„๊ธฐ ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅ

@mixin mine($text){
	@if $text == 'no'{
    	color : red;
    }@else if $text == 'yes'{
    	color : green;
    }@else{
    	color : blue;
    }
}

.text1{
	@include mine('no');
}

.text2{
	@include mine('yes');
}




7. ๋ฐ˜๋ณต๋ฌธ

Style์„ ๋ฐ˜๋ณต์ ์œผ๋กœ ์ถœ๋ ฅํ•˜๋Š” ๊ธฐ๋Šฅ

through ์™€ to ์— ๋”ฐ๋ผ ์ข…๋ฃŒ ์กฐ๊ฑด์ด ๋‹ค๋ฆ„

from a through ba ๋ถ€ํ„ฐ b ๊นŒ์ง€(b ํฌํ•จ) ๋ฐ˜๋ณต
from a to ba ๋ถ€ํ„ฐ b ์ „๊นŒ์ง€(b ๋ถˆํฌํ•จ) ๋ฐ˜๋ณต
/* scss */

@for $i from 1 through 3 {
  .through:nth-child(#{$i}) {
    width: 20px * $i;
  }
}

@for $i from 1 to 3 {
  .to:nth-child(#{$i}) {
    width: 20px * $i;
  }
}


/* css */

.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;
}

@each : List๋‚˜ Map์˜ ๋ฐ˜๋ณต์—์„œ ์œ ์šฉ

/* scss */
@each $color in red, blue, green {
	.#{$color}-text{
    	color : #{$color};
    }
}

@each $fruit, $color in (apple:red, banana:yellow) {
	.#{$fruit}{
    	color : #{$color};
    }
}


/* css */
.red-text{
	color : red;
}
.blue-text{
	color : blue;
}
.green-text{
	color : green;
}

.apple{
	color : red;
}
.banana{
	color : yellow;
}
profile
์ฝ”๋”ฉ ์ดˆ์งœ์ž…๋‹ˆ๋‹ค

0๊ฐœ์˜ ๋Œ“๊ธ€