๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
CSS

CSS | SCSS

by ์ฝ”๋”ฉํ•˜์ž9 2022. 8. 18.
728x90

๐Ÿค” SCSS, ๊ทธ๊ฒŒ ๋ญ”๋ฐ?

CSS์˜ ๋‹จ์  |
CSS ๋Š” HTML ํƒœ๊ทธ์— ๋””์ž์ธ์„ ๋„ฃ์–ด์ค„ ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ํ”„๋กœ์ ํŠธ ๊ทœ๋ชจ๊ฐ€ ์ปค์ง€๋ฉด ์ปค์งˆ์ˆ˜๋ก ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์ง„๋‹ค๊ฑฐ๋‚˜ ์œ ์ง€๋ณด์ˆ˜๊ฐ€ ์–ด๋ ค์›Œ์ง€๋Š” ์ผ์ด ๋ฐœ์ƒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์ด๋Ÿฌํ•œ CSS์˜ ๋‹จ์ ์„ ๋ณด์™„ํ•˜์—ฌ ์ฝ”๋“œ์˜ ์žฌํ™œ์šฉ์„ฑ๊ณผ ํšจ์œจ์„ฑ์„ ๋†’์ด๊ธฐ ์œ„ํ•ด ๋‚˜์˜จ ๊ฐœ๋…์ด SCSS๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.


Variables(๋ณ€์ˆ˜)

๋ณ€์ˆ˜๋ฅผ ์ง€์ •ํ•ด๋†“๊ณ  ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์˜ค์ฐจ ๋ฒ”์œ„๋ฅผ ์ค„์—ฌ์ฃผ๋ฉฐ, ๋””์ž์ธ ์ปจ์…‰์ด ๋ณ€๊ฒฝ๋˜๊ฑฐ๋‚˜ ๋ณ€๊ฒฝ์‚ฌํ•ญ์ด ์ƒ๊ฒผ์„๋•Œ๋„ ์œ ์ง€๋ณด์ˆ˜๊ฐ€ ๊ฐ„ํŽธํ•ฉ๋‹ˆ๋‹ค.

$font-stack: Helvetica, sans-serif;
$primary-color: #333;

body {
    font: 100% $font-stack;
    color: $primary-color;
}
CSS ํ™•์ธํ•˜๊ธฐ
body {
    font: 100% Helvetica, sans-serif;
    color: #333;
}

Nesting(์ค‘์ฒฉ)

CSS์—์„œ๋Š” nav ul / mav li / nav a ์ด๋ ‡๊ฒŒ ํƒœ๊ทธ๋ณ„๋กœ ๋‚˜๋ˆ ์„œ ์ž‘์—…์„ ํ•ด์คฌ๋‹ค๋ฉด, SCSS๋Š” ์ค‘์ฒฉ๊ธฐ๋Šฅ์ด ๊ฐ€๋Šฅํ•˜์—ฌ ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ์ด ์ข‹์•„์ง‘๋‹ˆ๋‹ค.

nav {
    ul {
      margin: 0;
      padding: 0;
      list-style: none;
    }
  
    li { display: inline-block; }
  
    a {
      display: block;
      padding: 6px 12px;
      text-decoration: none;
    }
}
CSS ํ™•์ธํ•˜๊ธฐ
nav ul {
    margin: 0;
    padding: 0;
    list-style: none;
}
nav li {
    display: inline-block;
}
nav a {
    display: block;
    padding: 6px 12px;
    text-decoration: none;
}

Modules(๋ชจ๋“ˆ)

ํ•œ ํŒŒ์ผ์— ๋ชจ๋“  SCSS๋ฅผ ์ž‘์„ฑํ•˜์ง€ ์•Š๊ณ  ๋‹ค๋ฅธ SCSS ํŒŒ์ผ์„ ๋ชจ๋“ˆ๋กœ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

// _base.scss
$font-stack: Helvetica, sans-serif;
$primary-color: #333;

body {
    font: 100% $font-stack;
    color: $primary-color;
}
// styles.scss
@use 'base';

.inverse {
    background-color: base.$primary-color;
    color: white;
}
CSS ํ™•์ธํ•˜๊ธฐ
body {
    font: 100% Helvetica, sans-serif;
    color: #333;
}

.inverse {
    background-color: #333;
    color: white;
}

Mixins(์žฌํ™œ์šฉ)

@mixin ์ง€์‹œ๋ฌธ์„ ๋งŒ๋“ค์–ด ์žฌ์‚ฌ์šฉํ•˜๋ ค๋Š” CSS๋ผ๋ฆฌ ๋ฌถ์–ด ๊ทธ๋ฃน์„ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.
@mixin theme($theme: ๋ฏน์Šค์ธ ์ด๋ฆ„) {}
"@include ๋ฏน์Šค์ธ์ด๋ฆ„" ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

@mixin theme($theme: DarkGray) {
    background: $theme;
    box-shadow: 0 0 1px rgba($theme, .25);
    color: #fff;
}

.info {
        @include theme;
}
.alert {
    @include theme($theme: DarkRed);
}
.success {
    @include theme($theme: DarkGreen);
}
// styles.scss
@use 'base';

.inverse {
    background-color: base.$primary-color;
    color: white;
}
CSS ํ™•์ธํ•˜๊ธฐ
.info {
    background: DarkGray;
    box-shadow: 0 0 1px rgba(169, 169, 169, 0.25);
    color: #fff;
}

.alert {
    background: DarkRed;
    box-shadow: 0 0 1px rgba(139, 0, 0, 0.25);
    color: #fff;
}

.success {
    background: DarkGreen;
    box-shadow: 0 0 1px rgba(0, 100, 0, 0.25);
    color: #fff;
}

Operators(์—ฐ์‚ฐ์ž)

SCSS์˜ ์—ฐ์‚ฐ์€ ํ”ฝ์…€ ๊ฐ’์„ ์ง์ ‘ ์ž…๋ ฅํ•˜์ง€ ์•Š๊ณ  ๋ฐฑ๋ถ„๋ฅ ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋“ฑ ์ž‘์—…ํ•ด ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

@use "sass:math";

.container {
    display: flex;
}

article[role="main"] {
    width: math.div(600px, 960px) * 100%;
}

aside[role="complementary"] {
    width: math.div(300px, 960px) * 100%;
    margin-left: auto;
}
CSS ํ™•์ธํ•˜๊ธฐ
.container {
    display: flex;
}

    article[role="main"] {
    width: 62.5%;
}

    aside[role="complementary"] {
    width: 31.25%;
    margin-left: auto;
}

๋Œ“๊ธ€


It's cording time

์ฝ”๋”ฉ ์—ฌ๊ธฐ์„œ ์ •๋ฆฌํ•˜๊ณ  ๋ฐฐ์›Œ๋ณด์ž๊ตฌ :9

๊ด‘๊ณ  ์ค€๋น„์ค‘์ž…๋‹ˆ๋‹ค.