iPod Video White

๐Ÿ’ป dev/๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป ํ•€ํ…Œํฌ ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž ๊ณผ์ •

[SCSS] ๊ธฐ๋ณธ ๋ฌธ๋ฒ• ๋ฐ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•

k_m_jin 2022. 5. 9. 15:59

๐Ÿ”ฅ ์ฃผ์˜์ 

- ํฐ๋”ฐ์˜ดํ‘œ ์‚ฌ์šฉ! (convention ๋•Œ๋ฌธ๊ณผ ํ˜น์‹œ ๋ชจ๋ฅผ ์˜ค๋ฅ˜)

 

 

์ถ”์ฒœ ์‚ฌ์ดํŠธ

scss ๊ฐ€ css ๋กœ ๋ณ€ํ™˜๋œ ์ฝ”๋“œ๋ฅผ ๋ฐ”๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ์‚ฌ์ดํŠธ

https://www.sassmeister.com

 

SassMeister | The Sass Playground!

SassMeister: The sassiest way to play with Sass, Compass, & LibSass! Loading...

www.sassmeister.com

- scss ๊ณต์‹ ์‚ฌ์ดํŠธ ๋ชจ๋“ˆ

https://sass-lang.com/documentation/modules

 

Sass: Built-In Modules

Compatibility: Dart Sass since 1.23.0 LibSass โœ— Ruby Sass โœ— Only Dart Sass currently supports loading built-in modules with @use. Users of other implementations must call functions using their global names instead. Sass provides many built-in modules w

sass-lang.com

 

 

Sass: Built-In Modules

Compatibility: Dart Sass since 1.23.0 LibSass โœ— Ruby Sass โœ— Only Dart Sass currently supports loading built-in modules with @use. Users of other implementations must call functions using their global names instead. Sass provides many built-in modules w

sass-lang.com

์ฃผ์„

// css๋กœ ์ปดํŒŒ์ผํ•˜๋ฉด ์‚ฌ๋ผ์ง

/**/ ์ปดํŒŒ์ผํ•ด๋„ ์‚ฌ๋ผ์ง€์ง€ ์•Š์Œ

 

์ค‘์ฒฉ

/*css*/
.container ul li{
 font-size: 40px;
}
.container ul li .name{
 color: royalblue;
}
.container ul li .age{
 color: orange;
}

/*scss*/
.container{
 ul{
   li{
     font-size: 40px;
     .name{
       color: royalblue;
     }
     .age{
       color: orange;
     }
   }
 }

์ƒ์œ„ ์„ ํƒ์ž ์ฐธ์กฐ

&: ์— ํผ์„ผ๋“œ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๋ฒ”์œ„์˜ ์ƒ์œ„์„ ํƒ์ž๋ฅผ ์ฐธ์กฐ / ์ƒ์œ„์„ ํƒ์ž๊ฐ€ ์— ํผ์„ผ๋“œ๋กœ ์น˜ํ™˜

/*css*/
.list li:last-childI
          margin-right: 0;
          
/*scss*/
.list{
 li{
    &:last-child{
      margin-right: 0;
    }
 }
}

์ค‘์ฒฉ๋œ ์†์„ฑ

๋„ค์ž„์ŠคํŽ˜์ด์Šค : ์ด๋ฆ„์€ ํ†ตํ•ด ๊ตฌ๋ถ„ ๊ฐ€๋Šฅํ•œ ๋ฒ”์œ„๋ฅผ ๋งŒ๋“ค์–ด๋‚ด๋Š” ๊ฒƒ (์ผ์ข…์˜ ์œ ํšจใ…‚๋จธ์œ„๋ฅผ ์ง€์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•)

/*css*/
.box{
/*font- ์˜ ๋„ค์ž„์ŠคํŽ˜์ด์Šค*/
  font-weight: bold;
  font-size: 10px;
  font-family: sans-serif;
  margin-top: 10px;
  margin-left: 20px;
  padding-top: 10px;
  padding-bottom: 40px;
  padding-left: 20px;
  padding-right: 30px;
}

/*scss*/
.box{
  font:{
    weight: bold;
    size: 10px;
    family: sans-serif;
  };
 margin:{
    top: 10px;
    left: 20px;
  };
  padding:{
    top: 10px;
    bottom: 40px;
    left: 20px;
    right: 30px;
  };
}

๊ธฐ๋ณธ๊ฐ’ !default

๊ธฐ๋ณธ๊ฐ’์„ ์ง€์ •ํ•ด๋‘์ง€๋งŒ ์‚ฌ์šฉ์ž๊ฐ€ ์ƒˆ๋กœ ์ง€์ •ํ•˜๋ฉด ๊ทธ ๊ฐ’์„ ์‚ฌ์šฉํ•˜๋ผ

$primary: red; !default

.serch{
	$primary: blue;
	background-color: $primary; /*blue*/
  }

๋ณ€์ˆ˜ (Variables)

$๋ณ€์ˆ˜๋ช…: ๊ฐ’;

๋ธ”๋Ÿญ๋ ˆ๋ฒจ ์œ ํšจ๋ฒ”์œ„๊ฐ€ ์žˆ์Œ

/*css*/
.container{
position: fixed;
top: 200px;
/*size์˜ ๊ฐ’์ด ์žฌํ• ๋‹น ๋˜์—ˆ๊ธฐ ๋–„๋ฌธ์— ๋ฐ”๋€ ๊ฐ’์œผ๋กœ ์„ค์ •*/
left: 100px; 
}
.container .item{
width: 200px;
height: 200px;
transform: translateX(200px);
}

/*scss*/
$size: 200px;
.container{
    position: fixed;
    top: $size;
    .item{
   		$size: 100px;
        width: $size;
        height: $size;
        transform: translateX($size);
    }
	left: $size;
}

์‚ฐ์ˆ  ์—ฐ์‚ฐ

 ๋‚˜๋ˆ„๊ธฐ ๊ธฐํ˜ธ์ธ / ๋Š” font ๋‹จ์ถ•์†์„ฑ์˜ ๊ตฌ๋ถ„์ž๋กœ ์‚ฌ์šฉํ•จ์œผ๋กœ ์•„๋ž˜์˜ ๋ฐฉ๋ฒ•์„ ์จ์•ผํ•จ

1 () ์†Œ๊ด„ํ˜ธ๋กœ ๊ฐ์‹ธ๊ธฐ

2 ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉ

3 ๋‹ค๋ฅธ ์—ฐ์‚ฐ์ž์™€ ์‚ฌ์šฉํ•˜๊ธฐ

/*css*/
div{
 width: 40px;
 height: 30px;
 font-size: 20px;
 margin: 11px;
 padding: 6px;
}

/*scss*/
div {
    sizer 30px;
     width: 20px+20px;
    height: 40p
    font-size: Nopx*2;
                    10px;
    margin: (10px+12px)/2; ///
    padding: 20px%7;
    }

๋‹ค๋ฅธ ๋‹จ์œ„ ์‚ฐ์ˆ ์—ฐ์‚ฐ

width: calc(100%- 200px)

: 100% ์—์„œ 200px ๋ฅผ ๋บ€ ๋งŒํผ์˜ ๋„ˆ๋น„

@mixin

: ์—ฌ๋Ÿฌ ์ค„์˜ ์ฝ”๋“œ๋ฅผ ํ•œ๋ฒˆ์— ๋‹ด์•„ ์žฌ์‚ฌ์šฉ

vs ๋ณ€์ˆ˜ ํ•˜๋‚˜์˜ ๋ณ€์ˆ˜์—๋Š” ํ•˜๋‚˜์˜ ๊ฐ’๋งŒ ์‚ฌ์šฉ

 

์ƒ์„ฑ: @mixin ์ด๋ฆ„

์‚ฌ์šฉ: @ include ์ด๋ฆ„

/*css*/
.container{
 display: flex;
 justify-content: center;
 align-items: center;
}
.container.item{
 display: flex;
 justify-content: center;
 align-items: center;
}
.box{
 display: flex;
 justify-content: center;
 align-items: center;
}

/*scss*/
@mixin çenter{ //์ƒ์„ฑ
    disptay flex;
    justify-content: center;
    align-items: center;
}
.container{
   @include center; // ์‚ฌ์šฉ
     item{
        @include center;
    }
}
.box{
    @include center;
}

์ธ์ˆ˜ ์‚ฌ์šฉ

/*css*/
.container{
 width: 200px;
 height: 200px;
 background-color: tomato;
}
.container .item{
 width: 100px;
 height: 100px;
 background-color: tomato;
}
.box{
 width: 100px;
 height: 100px;
 background-color: tomato;
}
/*scss*/
@mixin box ($size){
   width: $size;
   height: $size;
   background-color: tomato;
}
.container{
   @include box(200px)
    .item{
       @include box(100px);
    }
}
.box{
   @include box(100px);
}

์ธ์ˆ˜์˜ ๊ธฐ๋ณธ๊ฐ’ ์ง€์ •

- ๊ธฐ๋ณธ๊ฐ’์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ์ธ์ˆ˜ ์ƒ๊ฐน

- ํ‚ค์›Œ๋“œ ์ธ์ˆ˜: ์ธ์ˆ˜๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์ˆœ์„œ๋Œ€๋กœ ๋งค์นญ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ’์„ ๋„ฃ์„ ์ธ์ˆ˜๋ฅผ ํ‚ค์›Œ๋“œ๋กœ ์ง์ ‘์ ์œผ๋กœ ์ง€์ •ํ•ด์คŒ

/*scss ๊ธฐ๋ณธ๊ฐ’ ์ง€์ •*/
@mixin box ($size: 80px, $color: tomato){
   width: $size;
   height: $size;
   background-color: tomato;
}
.container{
   @include box(200px,red)
    .item{
       @include box; // ๊ธฐ๋ณธ๊ฐ’์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ์ธ์ˆ˜ ์ƒ๋žต
    }
}
.box{
   @include box($color: green); // ํ‚ค์›Œ๋“œ ์ธ์ˆ˜: ๊ฐ’์„ ๋„ฃ์„ ์ธ์ˆ˜๋ฅผ ์ง์ ‘์ ์œผ๋กœ ์ง€์ •
}

/*css*/
.container{
 width: 200px;
 height: 200px;
 background-color: red;
.container jtem{
 width:(80px;
 height 80px
 background-cólor: tomato;
.box{
 width: 80px;
 height: 80px;
 background-color: green;

@content

@ mixin ์˜ ์ถ”๊ฐ€ ๋‚ด์šฉ์ด ์žˆ์„ ๊ฒฝ์šฐ๋ฅผ ๋Œ€๋น„ํ•œ

/*css*/
.container{
 width: 100px;
 height: 100px;
 position: absolute;
 top: 0;
 left: 0;
}
.box{
 width: 200px;
 height: 300px;
 position: absolute;
 top: 0;
 left: 0;
 bottom: 0;
  right: 0;
 margin: auto;
}

/*scss*/
@mixin left-top{
   position: absolute;
   top: 0;
    left: 0;
   @content;
 }
 .container{
   width: 100px;
   height: 100px;
   @include left-top;
}
.box{
   width: 200px;
   height: 300px;
   @include left-top{
       bottom: 0;
       right: 0;
       margin: auto;
    }
}

๋ฐ˜๋ณต๋ฌธ

๋ณด๊ฐ„๋ฒ• #{} vs javascript ${}

์ œ๋กœ ๋ฒ ์ด์Šค๋“œ๊ฐ€ ์•„๋‹˜

//js
for (let i = 0; i < 10; i += 1){
    console.logCloop-${i}`)  
    }
/*scss*/
@for $i from 1 through 10{
  box:nth-child(#{$i}){ // ๊ฐ’์„ ์ ๋Š” ๋ถ€๋ถ„์ด ์•„๋‹ˆ๋ผ์„œ ๋ณด๊ฐ„๋ฒ• ์‚ฌ์šฉ
    width: 100px * $i ; //๊ฐ’์„ ๋ช…์‹œํ•˜๋Š” ๋ถ€๋ถ„์ด๋ฏ€๋กœ ๊ทธ๋Œ€๋กœ ์ ์Œ
  }
}

@each

list data

: list ์˜ ์•ˆ์— ์žˆ๋Š” ํ•ญ๋ชฉ๋“ค์„ c ๋ผ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์•„์„œ ์ฐจ๋ก€๋Œ€๋กœ {} ์•ˆ์—์„œ ์ฒ˜๋ฆฌ

/*css*/
.box{
  color: orange;
}
.box{
  color: royalblue;
}
.box{
  color: yellow;
}

/*scss*/
$list: orange, royalblue, yellow;

@each $c in $list{
    .box{
        color: $c;
    }
}

map data

: key์™€ value ๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— each ๋‹ค์Œ์— ๋‘๊ฐœ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ ์–ด์•ผํ•จ 

/*css*/
.box-o {
 colork orange;
}
.box-r {
 co tor royalblue;
}
.bpx-y {
 kolor yellow;
}

/*scss*/
$map:(
   o: orange,
    r: royalblue,
    y: yellow
);

@each $k, $v in $map{
    .box-#{$k}{
        color: $v;
    }
}

 

ํ•จ์ˆ˜

: ์–ด๋–ค ๊ฐ’์„ ์—ฐ์‚ฐํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•˜์—ฌ ์‚ฌ์šฉ

vs @mixin ์€ ์Šคํƒ€์ผ ์ •์˜ ์ฝ”๋“œ์˜ ๋ชจ์Œ

/*css*/
.box{
 width: 160px;
  height: 90px;
  display: flex;
  justify-content: center;
  align-items: center;
}

/*scss*/
@mixin center{
    display: flex;
    justify-content: center;
    align-items: center;
}
@function ratio($size, $ratio){
    @return $size*$ratio
}
.box{
    $width: 160px;
    width: $width;
    height: ratio($width, 9/16);
    @include center;
}

์ƒ‰์ƒ ๋‚ด์žฅ ํ•จ์ˆ˜

mix(color1, color2) : color1 ๊ณผ color2 ๋ฅผ ์„ž์Œ

lighten(color , %) : ์ƒ‰์ƒ์˜ ๋ฐ๊ธฐ๋ฅผ %๋งŒํผ ๋†’์ž„

darken(color , %) : ์ƒ‰์ƒ์˜ ๋ฐ๊ธฐ๋ฅผ %๋งŒํผ ๋‚ฎ์ถค

saturate(color , %) : ์ƒ‰์ƒ์˜ ์ฑ„๋„๋ฅผ %๋งŒํผ ๋†’์ž„

desaturate(color , %) : ์ƒ‰์ƒ์˜ ์ฑ„๋„๋ฅผ %๋งŒํผ ๋‚ฎ์ถค

grayscale(color) : ์ƒ‰์ƒ์„ ํšŒ์ƒ‰์„ ๋ณ€๊ฒฝ

invert(color) : ์ƒ‰์ƒ์„ ๋ฐ˜์ „

rgba(color, ํˆฌ๋ช…๋„) : vs css rgba(๋ ˆ๋“œ, ๊ทธ๋ฆฐ, ๋ธ”๋ฃจ, ํˆฌ๋ช…๋„)  

 

๊ฐ€์ ธ์˜ค๊ธฐ

url ์ƒ๋žต๊ฐ€๋Šฅ

ํ™•์žฅ์ž ์ƒ๋žต๊ฐ€๋Šฅ 

์—ฌ๋Ÿฌ๊ฐœ ๊ฐ€์ ธ์˜ค๊ธฐ ๊ฐ€๋Šฅ

/*css*/
@import url("./sub.scss")

/*scss*/
@import "./su1", "./sub2"

๋ฐ์ดํ„ฐ ์ข…๋ฅ˜

number: 1; // .5, 100px, lem
string: bold; // relative, "../images/a.png"

color: red; // blue, #FFFF00, rgba(0, 0,0,.1)
boolean: true; // false

null ๊ฐ’์„ ์ฃผ๋ฉด ์ถœ๋ ฅ๋˜์ง€ ์•Š์Œ

null: null; 

js ์˜ ๋ฐฐ์—ด ๋ฐ์ดํ„ฐ
list: orange, royalblue, yellow;

js ์˜ ๊ฐ์ฒด ๋ฐ์ดํ„ฐ

map:(
   o: orange,
    r: royalblue,
    y: yellow
);

๋ฐ˜์‘ํ˜•