Sass(SCSS)๋ž€?

๊ธฐ์กด CSS์˜ ํ•œ๊ณ„

  • ์„ ํƒ์ž(Selector)์˜ ๋ถˆํ•„์š”ํ•œ ์‚ฌ์šฉ

  • ์—ฐ์‚ฐ ยท ์กฐ๊ฑด ยท ๋ฐ˜๋ณต๋ฌธ ๋ถˆ๊ฐ€

  • CSS ์ฝ”๋“œ์˜ ๊ธธ์ด ยท ๋ณต์žก์„ฑ ์ฆ๊ฐ€

CSS Preprocessor(์ „์ฒ˜๋ฆฌ๊ธฐ)

  • CSS ๋™์ž‘ ์ „ ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ๋Šฅ. ์ „์ฒ˜๋ฆฌ๊ธฐ๋กœ ์ž‘์„ฑํ•˜๊ณ  CSS๋กœ ์ปดํŒŒ์ผ(Compile)ํ•˜์—ฌ ๋™์ž‘

    // styles.scss
    $primary-color: #fff;
    
    * {
      margin: 0 auto;
      box-sizing: border-box;
      color: $primary-color;
    }
    /* styles.css(compiled) */
    * {
    margin: 0 auto;
    box-sizing: border-box;
    color: #fff; }
  • CSS์— ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ ์ธ ์š”์†Œ(๋ณ€์ˆ˜, ํ•จ์ˆ˜, ์ƒ์† ๋ฐ ์ค‘์ฒฉ ๋“ฑ)๋ฅผ ์ ‘๋ชฉํ•˜์—ฌ CSS ๋ฌธ์„œ ์ž‘์„ฑ์— ๋„์›€

    • ์žฅ์ : ์žฌ์‚ฌ์šฉ์„ฑ, ๋‚ด์žฅ ํ•จ์ˆ˜ ์‚ฌ์šฉ(์ƒ์‚ฐ์„ฑ ์ฆ๊ฐ€), ์œ ์ง€ ๊ด€๋ฆฌ ์šฉ์ด

    • ์˜ˆ์‹œ) Sass(SCSS), Less, Stylus ๋“ฑ

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

  • SCSS๊ฐ€ Sass์˜ ์ƒ์œ„ ์ง‘ํ•ฉ(Superset)

    • SCSS๊ฐ€ ๊ธฐ์กด CSS ๋ฌธ๋ฒ•๊ณผ ๋” ์œ ์‚ฌ({}, ; ์กด์žฌ)

Sass(SCSS)์˜ ์‚ฌ์šฉ

  1. ๋ณ€์ˆ˜: $ ๊ธฐํ˜ธ๋ฅผ ํ†ตํ•ด ์ƒ์„ฑ

     $primary-color: #fff;
    
     body {
       background-color: $primary-color;
     }
  2. ์ค‘์ฒฉ(Nesting): ์„ ํƒ์ž(Selector) ์ค‘์ฒฉ ๊ฐ€๋Šฅ

     nav {
       i {
         width: 100%;
         height: 100%;
       }
    
       ul {
         margin: 0;
         padding: 0;
         list-style: none;
    
         li {
           display: flex;
           flex-direction: column;
           align-items: center;
         }
    
         a {
           text-decoration: none;
         }
       }
     }
  3. ๋ชจ๋“ˆ(Module): CSS snippet์„ ๋‹ค๋ฅธ SCSS ํŒŒ์ผ์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

     // _module.scss
     $primary-color: #fff;
    
     * {
       margin: 0;
       box-sizing: border-box;
     }
    
     // styles.scss
     // No file extension(ํŒŒ์ผ ํ™•์žฅ์ž)
     @ use 'module';
    
     .container {
       background-color: module.$primary-color;
     }
  4. ๋ฏน์Šค์ธ(Mixins): ์žฌ์‚ฌ์šฉํ•  CSS ์„ ์–ธ์„ ๊ทธ๋ฃนํ™”ํ•˜์—ฌ ์‚ฌ์šฉ

     @mixin wh($width, $height) {
       width: $width;
       height: $height;
     }
    
     img {
       @include wh(100%, 50%);
     }
  5. ์ƒ์†(Extend/ Inheritance)

     %centering {
       display: flex;
       align-items: center;
       justify-content: center;
     }
    
     .p1 {
       @extend %centering;
       color: #ff622b; 
     }
    
     .p2 {
       color: #106b06;
     }
  6. ์—ฐ์‚ฐ์ž(Operators)

    .container {
     width: 100%;
    }
    
    article {
     float: left;
     width: 600px / 960px * 100%;
    }
    
    aside {
     float: right;
     width: 300px / 960px * 100%;
    }

Reference

Sass Doc

Sass(SCSS) ์™„์ „ ์ •๋ณต | Heropy

Last updated