Sass(SCSS)

1. Sass ์™€ SCSS์˜ ์ฐจ์ด์ 

  • Sass์˜ 3๋ฒ„์ „์— ์ƒˆ๋กญ๊ฒŒ ๋“ฑ์žฅํ•œ SCSS

  • SCSS๋Š” CSS๊ตฌ๋ฌธ๊ณผ ์™„์ „ํžˆ ํ˜ธํ™˜๋˜๋„๋ก ์ƒˆ๋กœ์šด ๊ตฌ๋ฌธ์„ ๋„์ž…ํ•ด ๋งŒ๋“  Sass์˜ ๋ชจ๋“  ๊ธฐ๋Šฅ์„ ์ง€์›ํ•˜๋Š” CSS์˜ ์ƒ์œ„์ง‘ํ•ฉ.

  • ์ฆ‰, SCSS๋Š” CSS์™€ ๊ฑฐ์˜ ๊ฐ™์€ ๋ฌธ๋ฒ•์œผ๋กœ Sass๊ธฐ๋Šฅ์„ ์ง€์›

2. ๊ฐœ์š”

  • Sass(SCSS)๋Š” ์›น์—์„œ ์ง์ ‘ ๋™์ž‘ X

  • ์ตœ์ข…์—๋Š” ํ‘œ์ค€ CSS๋กœ ๋™์ž‘ํ•ด์•ผํ•˜๊ณ , ์ „์ฒ˜๋ฆฌ๊ธฐ๋กœ ์ž‘์„ฑ ํ›„, CSS ์ปดํŒŒ์ผํ•ด์•ผํ•œ๋‹ค.

  • js ๊ฐœ๋ฐœํ™˜๊ฒฝ์—์„œ ์ถ”์ฒœํ•˜๋Š” ๋ฐฉ๋ฒ•

    • SassMeister(๊ฐ•์˜์—์„œ ์ž์ฃผ ์‚ฌ์šฉ)

    • node-sass, webpack, ....์ค‘ Parcel ์ด์šฉ!

    • node js ์‚ฌ์šฉ์ž ๊ธฐ์ค€, ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ฒˆ๋“ค๋Ÿฌ Parcel์„ ์‚ฌ์šฉํ• ๊ฒƒ

      • ํŒจํ‚ค์ง€ ๊ด€๋ฆฌ(package.json) npm init -y

      • Parcel์ „์—ญ์„ค์น˜

        npm install -g parcel-bundler

      • dev dependencies์— ์„ค์น˜

        npm i -D parcel-bundler

      • Sass ์ปดํŒŒ์ผ๋Ÿฌ์„ค์น˜(node-sass) npm install --save-dev node-sass

      • HTML์— <link>๋กœ Sass ํŒŒ์ผ ์—ฐ๊ฒฐํ•˜๊ธฐ

        <link rel="stylesheet" href="scss/main.scss">

      • ํ•„์š”ํ•œ ๋ชจ๋“ˆ ์ž๋™์œผ๋กœ ์„ค์น˜. parcel์ด ์ปดํŒŒ์ผํ•ด์„œ scss๋ฅผ ์ฝ์„์ˆ˜์žˆ๋„๋กํ•ด์คŒ.

        npx parcel index.html

3. ๋ฌธ๋ฒ•

3.1 ์ฃผ์„

  • // : ์ปดํŒŒ์ผ ๋˜์ง€ ์•Š๋Š” ์ฃผ์„

  • / /: ์ปดํŒŒ์ผ ๋˜๋Š” ์ฃผ์„

    • Sass์˜ ๊ฒฝ์šฐ์—๋Š” ์ค„์„ ๋งž์ถฐ * ์„ ์‚ฌ์šฉํ•ด์•ผํ•˜์ง€๋งŒ,

    • SCSS๋Š” ๊ฐ ์ค„์— * ์—†์–ด๋„ ํ˜ธํ™˜ ์‰ฝ๋‹ค.

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

๋ฐ์ดํ„ฐ

์„ค๋ช…

์˜ˆ์‹œ

Numbers

์ˆซ์ž

1, .82, 20px, 2emโ€ฆ

Strings

๋ฌธ์ž

bold, relative, "/images/a.png", "dotum"

Colors

์ƒ‰์ƒ ํ‘œํ˜„

red, blue, #FFFF00, rgba(255,0,0,.5)

Booleans

๋…ผ๋ฆฌ

true, false

Nulls

์•„๋ฌด๊ฒƒ๋„ ์—†์Œ

null

Lists

๊ณต๋ฐฑ์ด๋‚˜ ,๋กœ ๊ตฌ๋ถ„๋œ ๊ฐ’์˜ ๋ชฉ๋ก

(apple, orange, banana), apple orange

Maps

Lists์™€ ์œ ์‚ฌํ•˜๋‚˜ ๊ฐ’์ด Key: Value ํ˜•ํƒœ

(apple: a, orange: o, banana: b)

(์ฐธ๊ณ ) ๋ชฉ์ฐจ

3.3 ์ค‘์ฒฉ

  • ์ƒ์œ„์„ ํƒ์ž ์ฐธ์กฐ (&) : ์ƒ์œ„ ์„ ํƒ์ž๋ฅผ ์‚ฌ์šฉํ•ด์„œ, ์ผ์น˜์„ ํƒ์ž๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Œ.

      /*SCSS*/
      .fs{
          &-small{
              font-size: 14px;
          }
          &-medium{
              font-size: 16px;
          }
          &-large{
              font-size: 18px;
          }
      }
      /*CSS๋กœ ์ปดํŒŒ์ผ์‹œ*/
      .fs-small {
          font-size: 14px;
      }
      .fs-medium {
          font-size: 16px;
      }
      .fs-large {
          font-size: 18px;
      }
  • ์ค‘์ฒฉ ๋ฒ—์–ด๋‚˜๊ธฐ @at-root

      /*SCSS*/
      .section{
          $width: 100px;
          $height: 200px;
          width: $width;
          height: $height;
          .item{
              width: $width;
              height: $height;
          }
          @at-root .box{
              width: $width;
              height:$height;
          }
      }
      /*CSS*/
      .section {
          width: 100px;
          height: 200px;
      }
      .section .item {
          width: 100px;
          height: 200px;
      }
      .box {
          width: 100px;
          height: 200px;
      }
  • ์ค‘์ฒฉ๋œ ์†์„ฑ

    • ๋™์ผํ•œ ๋„ค์ž„์ŠคํŽ˜์ด์Šค (margin-, font-, padding-..)๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ

      .box{
        font:{
            size: 10px;
            weight: bold;
        };
        margin:{
            top: 100px;
            left: 20px;
            right: 30px;
        };
      }
      .box {
        font-size: 10px;
        font-weight: bold;
        margin-top: 100px;
        margin-left: 20px;
        margin-right: 30px;
      }

๋ชฉ์ฐจ

3.4 ๋ณ€์ˆ˜

  • ๋ฐ˜๋ณต ์‚ฌ์šฉ๋˜๋Š” ๊ฐ’์„ ๋ณ€์ˆ˜๋กœ ์ง€์ •($) $๋ณ€์ˆ˜์ด๋ฆ„: ์†์„ฑ๊ฐ’

      $w: 100px;
      $color: red;
      $url: "/assets/images/";
    
      .box{
      width: $w;
      margin-left: $w;
      background: $color url($url + "pretty.jpg");
      }
      .box {
          width: 100px;
          margin-left: 100px;
          background: red url("/assets/images/pretty.jpg");
      }
  • ๋ณ€์ˆ˜ ์œ ํšจ๋ฒ”์œ„(Variable Scope)

    • ์„ ์–ธ๋œ ๋ธ”๋ก {} ๋‚ด์—์„œ๋งŒ ์œ ํšจ

  • ๋ณ€์ˆ˜ ์žฌ ํ• ๋‹น

    • ๋ณ€์ˆ˜์— ๋ณ€์ˆ˜๋ฅผ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ์Œ

  • ๋ณ€์ˆ˜ ์ „์—ญ์„ค์ •

    • !global ํ”Œ๋ž˜๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ, ์œ ํšจ๋ฒ”์œ„๋ฅผ ์ „์—ญ(Global)๋กœ ์„ค์ •

  • ๋ณ€์ˆ˜ ์ดˆ๊นƒ๊ฐ’ ์„ค์ •

    • !default ํ”Œ๋ž˜๊ทธ๋Š” ํ• ๋‹น๋˜์ง€ ์•Š์€ ๋ณ€์ˆ˜์˜ ์ดˆ๊นƒ๊ฐ’์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

    • ์ฆ‰, ํ• ๋‹น๋˜์–ด ์žˆ๋Š” ๋ณ€์ˆ˜๊ฐ€ ์žˆ๋‹ค๋ฉด ๋ณ€์ˆ˜๊ฐ€ ๊ธฐ์กด ํ• ๋‹น๊ฐ’์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

    • ๊ธฐ์กด ๋ณ€์ˆ˜๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ, ํ˜„์žฌ ์„ค์ •ํ•˜๋Š” ๋ณ€์ˆ˜์˜ ๊ฐ’์€ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ฒ ๋‹ค๋Š” ๋œป.

    • ๊ธฐ์กด ์ฝ”๋“œ(์›๋ณธ)์„ Overwriteํ•˜์ง€ ์•Š๊ณ  ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉ.

  • ๋ฌธ์ž๋ณด๊ฐ„(#{})

    • #{}๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ, ์ฝ”๋“œ์˜ ์–ด๋””๋“ ์ง€ ๋ณ€์ˆ˜๊ฐ’์„ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค.

      /*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");
    • unquote()๋Š” ๋ฌธ์ž์—์„œ ๋”ฐ์˜ดํ‘œ ์ œ๊ฑฐ

๋ชฉ์ฐจ

3.5 ๊ฐ€์ ธ์˜ค๊ธฐ

  • @import๋กœ ์™ธ๋ถ€์—์„œ ๊ฐ€์ ธ์˜จ Sass ํŒŒ์ผ์€ ๋ชจ๋‘ ๋‹จ์ผ CSS์ถœ๋ ฅ ํŒŒ์ผ๋กœ ๋ณ‘ํ•ฉ

    • Sass @import ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ SassํŒŒ์ผ์„ ๊ฐ€์ ธ์˜ค๋Š”๋ฐ, CSS @import ๊ทœ์น™์œผ๋กœ ์ปดํŒŒ์ผ๋˜๋Š” ๋ช‡๊ฐ€์ง€ ์ƒํ™ฉ์ด ์žˆ๋‹ค.

      • ํŒŒ์ผํ™•์žฅ์ž๊ฐ€ .css์ผ๋•Œ

      • ํŒŒ์ผ์ด๋ฆ„์ด http://๋กœ ์‹œ์ž‘ํ•˜๋Š” ๊ฒฝ์šฐ

      • url()์ด ๋ถ™์—ˆ์„ ๊ฒฝ์šฐ

      • ๋ฏธ๋””์–ด ์ฟผ๋ฆฌ๊ฐ€ ์žˆ๋Š”๊ฒฝ์šฐ

    • ์œ„์˜ ๊ฒฝ์šฐ CSS @import ๊ทœ์น™๋Œ€๋กœ ์ปดํŒŒ์ผ๋ฉ๋‹ˆ๋‹ค.

  • ์—ฌ๋Ÿฌ๊ฐ€์ง€ ํŒŒ์ผ๊ฐ€์ ธ์˜ค๊ธฐ

      @import "header", "footer";
  • ํŒŒ์ผ ๋ถ„ํ• 

    • ํŒŒ์ผ์ด๋ฆ„ ์•ž์— _๋ฅผ ๋ถ™์ด๋ฉด ์ปดํŒŒ์ผ์‹œ ~.cssํŒŒ์ผ๋กœ ์ปดํŒŒ์ผํ•˜์ง€ ์•Š๋Š”๋‹ค.

    • Webpack ์ด๋‚˜ Parcel, Gulp ๊ฐ™์€ ์ผ๋ฐ˜์ ์ธ ๋นŒ๋“œํˆด์—์„œ๋Š” Partials ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ํ•„์š” ์—†์ด, ์„ค์ •๋œ ๊ฐ’์— ๋”ฐ๋ผ ๋นŒ๋“œ ๋œ๋‹ค. ํ•˜์ง€๋งŒ _๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์„ ๊ถŒ์žฅ.

๋ชฉ์ฐจ

3.6 ์—ฐ์‚ฐ

  • ์‚ฐ์ˆ ์—ฐ์‚ฐ์ž

  • ๋น„๊ต์—ฐ์‚ฐ์ž

  • ๋…ผ๋ฆฌ์—ฐ์‚ฐ์ž(and, or, not)

  • ์ˆซ์ž

    • ์ƒ๋Œ€์ „ ๋‹จ์œ„ ์—ฐ์‚ฐ(%, em, vm๋“ฑ)์˜ ์—ฐ์‚ฐ์˜ ๊ฒฝ์šฐ CSS ์˜ calc()๋กœ ์—ฐ์‚ฐ

        width: 50%- 20px; // ๋‹จ์œ„ ๋ชจ์Šจ ์—๋Ÿฌ
        width: calc(50%-20px); // ์—ฐ์‚ฐ๊ฐ€๋Šฅ
    • ๋‚˜๋ˆ„๊ธฐ ์—ฐ์‚ฐ ์ฃผ์˜์‚ฌํ•ญ

      • CSS๋Š” ์†์„ฑ ๊ฐ’์˜ ์ˆซ์ž๋ฅผ ๋ถ„๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ /๋ฅผ ํ—ˆ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์—ฐ์‚ฐ์ด ์•ˆ๋  ์ˆ˜๋„ ์žˆ๋‹ค.

      • ์˜ˆ๋ฅผ๋“ค์–ด

        font:16px/22px serif; ๋Š”

        font-size:16px; line-height:22px์„ ๋œปํ•˜๊ธฐ ๋•Œ๋ฌธ.

      • ๋”ฐ๋ผ์„œ /๋ฅผ ๋‚˜๋ˆ„๊ธฐ ์—ฐ์‚ฐ์œผ๋กœ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋‹ค์Œ ์กฐ๊ฑด์„ ์ถฉ์กฑํ•ด์•ผํ•œ๋‹ค.

        • ๊ฐ’ ๋˜๋Š” ๊ทธ ์ผ๋ถ€๊ฐ€ ๋ณ€์ˆ˜์— ์ €์žฅ๋˜๊ฑฐ๋‚˜ ํ•จ์ˆ˜์— ์˜ํ•ด ๋ฐ˜ํ™˜๋˜๋Š” ๊ฒฝ์šฐ

        • ๊ฐ’์ด ()๋กœ ๋ฌถ์—ฌ์žˆ๋Š” ๊ฒฝ์šฐ

        • ๊ฐ’์ด ๋‹ค๋ฅธ ์‚ฐ์ˆ  ํ‘œํ˜„์‹์˜ ์ผ๋ถ€๋กœ ์‚ฌ์šฉ๋˜๋Š” ๊ฒฝ์šฐ

      • ๋‚˜๋ˆ—์…ˆ ์—ฐ์‚ฐ์„ ํ•˜๋ ค๋ฉด /๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฅธ์ชฝ์—๋Š” ์ˆซ์ž๋งŒ ์“ฐ์ž.

  • ๋ฌธ์ž

    • ๋ฌธ์ž์—ฐ์‚ฐ์—๋Š” + ์‚ฌ์šฉ

    • ๋ฌธ์ž์—ฐ์‚ฐ์œผ ๊ฒฐ๊ณผ๋Š” ์ฒซ๋ฒˆ์งธ ํ”ผ์—ฐ์‚ฐ์ž๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ•จ.

    • ์ฒซ๋ฒˆ์งธ ํ”ผ์—ฐ์‚ฐ์ž๊ฐ€ ๋”ฐ์˜ดํ‘œ ์žˆ์—ˆ๋‹ค๋ฉด, ๊ฒฐ๊ณผ๋„ ๋”ฐ์˜ดํ‘œ๋กœ ๋ฌถ์ธ์ฑ„๋กœ ๋ฐ˜ํ™˜

    • ์ฒซ๋ฒˆ์งธ ํ”ผ์—ฐ์‚ฐ์ž์— ๋”ฐ์˜ดํ‘œ ์—†๋Š”๋ฌธ์ž์˜€๋‹ค๋ฉด, ๊ฒฐ๊ณผ๋„ ๋”ฐ์˜ดํ‘œ ์—†์ด ๋ฐ˜ํ™˜

  • ์ƒ‰์ƒ

    • ์ƒ‰์ƒ๋„ ์—ฐ์‚ฐํ•  ์ˆ˜ ์žˆ๋‹ค.

      div{
        color: #123456 + #345678;
        //R: 12 + 34 = 46 //16์ง„์ˆ˜๋กœ ํ‘œ์‹œ
        //G: 34 + 56 = 8a
        //B: 56 + 78 = ce
        background: rgba(50,100,150,.5)+ rgba(10,20,30,.5);
        //R: 50+10 = 60
        //G: 100 +20 =120
        //B: 150+30 = 180
        //A: Alpha channels must be equal
      }
      div{
        color: #468ace;
        background: rgba(60,120,180,.5)
      }
    • alpha channels ๊ฐ’์„ ์—ฐ์‚ฐํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, opacify(), transparentize() ํ•จ์ˆ˜ ์‚ฌ์šฉ.

      • oacify(์ปฌ๋Ÿฌ๋ณ€์ˆ˜, .3) : ์ปฌ๋Ÿฌ๋ณ€์ˆ˜๋ฅผ 30% ๋” ๋ถˆํˆฌ๋ช…ํ•˜๊ฒŒ(+)

      • transparentize(์ปฌ๋Ÿฌ๋ณ€์ˆ˜, .3): ์ปฌ๋Ÿฌ๋ณ€์ˆ˜๋ฅผ 30% ๋” ํˆฌ๋ช…ํ•˜๊ฒŒ(-)

  • ๋…ผ๋ฆฌ

    • @if์กฐ๊ฑด๋ฌธ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋…ผ๋ฆฌ ์—ฐ์‚ฐ

      /*SCSS*/
      $w:100px;
      .item{
        display: block;
        @if not ($w>50px and $w<90px){
            width: 400px;
        }
      }

๋ชฉ์ฐจ

3.7 ์žฌํ™œ์šฉ

  • Sass Mixins๋Š” ์Šคํƒ€์ผ ์‹œํŠธ์ „์ฒด์—์„œ ์žฌ์‚ฌ์šฉํ•  CSS ์„ ์–ธ ๊ทธ๋ฃน์„ ์ •์˜ํ•˜๋Š” ์•„์ฃผ ํ›Œ๋ฅญํ•œ ๊ธฐ๋Šฅ.

  • ์•ฝ๊ฐ„์˜ Mixin(๋ฏน์Šค์ธ)์œผ๋กœ ๋‹ค์–‘ํ•œ ์Šคํƒ€์ผ์„ ๋งŒ๋“ค์–ด ๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

  • ๊ธฐ์–ตํ• ๊ฒƒ 2๊ฐ€์ง€

    • ์„ ์–ธํ•˜๊ธฐ @Mixin

    • ํฌํ•จํ•˜๊ธฐ @include

      //SCSS
      @mixin size($w:100px, $h:100px){
        width: $w;
        heigh: $h;
        &::after{
            content:"!!";
        }
      }
      
      .box1{
        @include size;
      }
      .box2{
        @include size(200px, 300px);
      }
      .box3{
        @include size($h:400px);
      }
      .box1 {
        width: 100px;
        heigh: 100px;
      }
      .box1::after{
        content:"!!";
      }
      
      .box2 {
        width: 200px;
        heigh: 300px;
      }
      .box2::after{
            content:"!!";
        }
      .box3 {
        width: 100px;
        heigh: 400px;
      }
      .box3::after{
        content:"!!";
      }
    • ์ƒ์œ„ ์ฐธ์กฐ ์—ฐ์‚ฐ ์‚ฌ์šฉ๊ฐ€๋Šฅ

    • ํ‚ค์›Œ๋“œ์ธ์ž ์‚ฌ์šฉ๊ฐ€๋Šฅ

    • ๊ฐ€๋ณ€์ธ์ˆ˜(๋งค๊ฐœ๋ณ€์ˆ˜ ๋’ค์— ...)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž…๋ ฅํ•  ์ธ์ˆ˜๊ฐฏ์ˆ˜๊ฐ€ ๋ถˆํ™•์‹คํ• ๋•Œ ์‚ฌ์šฉ.

    • @content: ํ•ด๋‹น๋ถ€๋ถ„์— ์›ํ•˜๋Š” ์Šคํƒ€์ผ ๋ธ”๋ก์„ ์ „๋‹ฌ.

      //SCSS @content ์‚ฌ์šฉ์˜ˆ์‹œ
      @mixin icon($url){
        &::after{
            content: $url;
            @content;;
        }
      }
      
      .box1{
        @include icon("image/icon1.png")
      }
      .box2{
        @include icon("image/icon2.png"){
            display:block;
            position: absolute;
            width: 100px;
            height: 100px;
        }
      }
      /*compile ๋œ css*/
      .box1::after {
        content: "image/icon1.png";
      }
      
      .box2::after {
        content: "image/icon2.png";
        display: block;
        position: absolute;
        width: 100px;
        height: 100px;
      }

      ๋ชฉ์ฐจ

3.8 ํ™•์žฅ

  • @extend ์„ ํƒ์ž

  • ๋‹ค์ค‘์„ ํƒ์ž ํ˜•์‹์œผ๋กœ ๋“ค์–ด๊ฐ€์ง„๋‹ค.

  • ๊ถŒ์žฅ๋˜๋Š” ๋ฐฉ๋ฒ•์€ ์•„๋‹˜, ๋˜๋„๋ก mixin์„ ๋Œ€์ฒด๊ธฐ๋Šฅ์œผ๋กœ ์‚ฌ์šฉ!

    .btn{
    padding: 10px;
    margin: 10px;
    background: blue;
    }
    .btn-danger{
    @extend .btn;
    background:red; 
    }

    ```CSS

    .btn, .btn-danger {

    padding: 10px;

    margin: 10px;

    background: blue;

    }

.btn-danger { background: red; }

[๋ชฉ์ฐจ][TOC]

---

#### 3.9 ํ•จ์ˆ˜
- Mixin: ์ง€์ •ํ•œ ์Šคํƒ€์ผ์„ ๋ฐ˜ํ™˜ํ•จ
- function: ๋ณดํ†ต ์—ฐ์‚ฐ๋œ(Computed) ํŠน์ •๊ฐ’์„ @return ์ง€์‹œ์–ด๋ฅผ ํ†ตํ•ด ๋ฐ˜ํ™˜
- ํ•จ์ˆ˜์ด๋ฆ„ ๋ช…๋ช…์‹œ, ๋‚ด์žฅํ•จ์ˆ˜์™€ ๊ฒน์น  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ ‘๋‘์–ด๋ฅผ ๋ถ™์—ฌ ์ง€์„๊ฒƒ.
- ์˜ˆ: `extract-read()` ๊ฐ™์€ ์ด๋ฆ„
- ์‚ฌ์šฉ๋ฐฉ๋ฒ•
    ```SCSS
    // ์„ ์–ธ
    //Mixin
    @mixin ๋ฏน์Šค์ธ์ด๋ฆ„($๋งค๊ฐœ๋ณ€์ˆ˜){
        ์Šคํƒ€์ผ;
    }
    //Function
    @function ํ•จ์ˆ˜์ด๋ฆ„($๋งค๊ฐœ๋ณ€์ˆ˜){
        @return ๊ฐ’
    }
    //์‚ฌ์šฉ
    //Mixin
    @include ๋ฏน์Šค์ธ์ด๋ฆ„(์ธ์ˆ˜);
    //Fuction
    ํ•จ์ˆ˜์ด๋ฆ„(์ธ์ˆ˜)
  • ์‹ค์ œ ์ ์šฉ์˜ˆ์‹œ

      @function columns($number:1, $columns:12, $width:1200px){
          @return $width *($number/$columns);
      }
    
      .container{
          $width: 1200px;
          width: $width;
          .item:nth-child(1){
              width: columns();
          }
          .item:nth-child(2){
              width: columns(8, $width:$width);
          }
          .item:nth-child(3){
              width: columns(3);
          }
      }
  • if(ํ•จ์ˆ˜)

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

    • ์‚ผํ•ญ์—ฐ์‚ฐ์ž์™€ ์œ ์‚ฌ

    • if (์กฐ๊ฑด, ํ‘œํ˜„์‹1, ํ‘œํ˜„์‹2)

      $width:500px;
      div{
        width: if($width>300px, $width, null);
      }

      ๋ชฉ์ฐจ

3.10 ์กฐ๊ฑด๊ณผ ๋ฐ˜๋ณต

  • ์กฐ๊ฑด

    • @if(์ง€์‹œ์–ด)

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

    • if๋ฌธ๊ณผ ์œ ์‚ฌ

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

    • (์ง€์‹œ์–ด) ์ƒ๋žต๊ฐ€๋Šฅ

    • ์˜ˆ์ œ1

        @function limitSize($size){
            @if ($size>=0 and $size<=200px){
                @return 200px;
            }@else{
                @return 800px;
            }
        }
      
        div{
            width: limitSize(180px);
            height: limitSize(340px);
        }
        //compile to
        div {
            width: 200px;
            height: 800px;
        }
    • ์˜ˆ์ œ2

        @mixin positionCenter($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 positionCenter(10px, 20px)
        }
        .box2{
            @include positionCenter(50,50, fixed)
        }
        .box3{
            @include positionCenter(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;
        }
  • ๋ฐ˜๋ณต๋ฌธ 1. @for

    • ๊ด€๋ก€์ƒ ๋ณ€์ˆ˜๋กœ $i ๋งŽ์ด ์‚ฌ์šฉ

    • ์‚ฌ์šฉ๋ฒ•

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

        @for $i from 1 through 3{
            .box:nth-child(#{$i}){
                width: 20px * $i;
            }
        }
        .box:nth-child(1) {
            width: 20px;
        }
      
        .box:nth-child(2) {
            width: 40px;
        }
      
        .box:nth-child(3) {
            width: 60px;
        }
    • @each

      • List, Map ๋ฐ์ดํ„ฐ ๋ฐ˜๋ณตํ•ด์•ผํ• ๋•Œ ์‚ฌ์šฉ

      • for in ๋ฌธ๊ณผ ์œ ์‚ฌ

        @each $๋ณ€์ˆ˜ in ๋ฐ์ดํ„ฐ{
          //๋ฐ˜๋ณต๋‚ด์šฉ
        }
      • ์‚ฌ์šฉ์˜ˆ์‹œ1(List)

        $fruits: (apple, banana, mango, orange);
        
        .fruits{
          @each $fruit in $fruits{
              $index: index($fruits, $fruit);
              li:nth-child(#{$index}){
                  left: 50px * $index;
                  background: url("/images/#{$fruit}.png")
              }
          }
        }
        .fruits li:nth-child(1) {
          left: 50px;
          background: url("/images/apple.png");
        }
        .fruits li:nth-child(2) {
          left: 100px;
          background: url("/images/banana.png");
        }
        .fruits li:nth-child(3) {
          left: 150px;
          background: url("/images/mango.png");
        }
        .fruits li:nth-child(4) {
          left: 200px;
          background: url("/images/orange.png");
        }
      • ์‚ฌ์šฉ์˜ˆ์‹œ2(Map)

        $fruits-data:(
          apple:korea,
          orange:china,
          banana:japan
        );
        
        @each $key, $value in $fruits-data{
          // map-keys($fruits-data) =>(apple, orange, banana)
          // map-values($fruits-data) => (korea, china, japan)
          $key-list : map-keys($fruits-data);
          $index : index($key-list, $key);
          .box-#{$key}{
              width: $index * 100px;
              background: url("/images/#{$value}.png");
          }
        }
        .box-apple {
          width: 100px;
          background: url("/images/korea.png");
        }
        
        .box-orange {
          width: 200px;
          background: url("/images/china.png");
        }
        
        .box-banana {
          width: 300px;
          background: url("/images/japan.png");
        }
    • @while

3.11 ๋‚ด์žฅํ•จ์ˆ˜

๋ชฉ์ฐจ

Heropy๋‹˜

Last updated