SCSS ๋?
Sassy CSS ์ ์ค์๋ง๋ก (๋ฌธ๋ฒ์ ์ผ๋ก) ๋ฉ์ง CSS ๋ผ๋ ๋ป์ธ๋ฏ..
์ ํํ๋ CSS์ ๋ฉํ์ธ์ด. (= CSS์ ๋ํด ๊ธฐ๋ฅ์ ํ์ฅํ ์ธ์ด)
์ฝ๋์ ์ฌํ์ฉ์ฑ์ ์ฌ๋ฆฌ๊ณ , ๊ฐ๋ ์ฑ์ ์ฌ๋ฆฌ๋ ๋ฑ์ CSS์์ ๋ณด์ด๋ ๋จ์ ์ ๋ณด์ํ๊ณ , ๊ฐ๋ฐ์ ํจ์จ์ ์ฌ๋ฆฌ๊ธฐ ์ํด ๋ฑ์ฅํ ๊ฐ๋ .
์ ์ฒ๋ฆฌ๊ธฐ๋ฅผ ์ํ ๋๊ตฌ๊ฐ ํ์ํจ, ์ปดํ์ผ ์๊ฐ ์์ ๋ฑ์ ๋จ์ ์ด ์์ง๋ง ๊ทธ ์ธ์ ์ฅ์ ์ด ๋ช ํํ ํธ์ด์ด์ ์ฃผ๋ก ์ฌ์ฉ๋จ.
SASS ์ SCSS์ ์ฐจ์ด?
SASS(Syntactically Awesome Style Sheets) ๋ฌธ๋ฒ์ CSS์ ํํ๊ฐ ๋ฌ๋ผ ๋ฌ๋ ์ปค๋ธ๊ฐ ์กด์ฌ.
(๊ธฐ์กด CSS์๋ ๋ฌ๋ฆฌ ์ฝ๋ก ์ด๋ ๊ดํธ ๋ฑ์ด ์๋ต๋ ๋ถ๋ถ์ด ๋ง์ ์ฒ์ ์ ํ๋ฉด ์ต์ํด์ง๋ ๋ฐ์ ์๊ฐ์ด ์์๋จ. ๊ธฐ์กด CSS ๋ฌธ๋ฒ์ ์ง์ํด์ฃผ๋ ๊ฒ๋ ์๋.)
SASS์ ๋ฌธ๋ฒ์ ๋ณด์ํ๊ณ ์ ๋์จ ๊ฒ SCSS.
CSS์ ์ํผ์ (superset)์ด๊ธฐ ๋๋ฌธ์ CSS ๋ฌธ๋ฒ ๊ทธ๋๋ก ์ฌ์ฉํด๋ ์ ์ฉ์ด ์ ๋๊ณ , ๋ฌธ๋ฒ ๋ํ CSS์ ๋ค๋ฅด์ง ์์.
์ ๊ณตํ๋ ๊ธฐ๋ฅ
๋ณ์(Variable) ํ ๋น
/* CSS */
body {
font: 100% Helvetica, sans-serif;
color: #333;
}
/* SCSS */
$font-stack: Helvetica, sans-serif;
$primary-color: #333;
body {
font: 100% $font-stack;
color: $primary-color;
}
์์ฃผ ์ฌ์ฉํ๋ ์์ด๋ ํฐํธ ๊ฐ์ ๊ฒ๋ค์ ๋ณ์๋ก ์ง์ ํด์ ์ฌ์ฌ์ฉํ ์ ์์.
์ค์ฒฉ(Nesting) ๊ตฌ๋ฌธ
/* CSS */
nav ul {
margin: 0;
padding: 0;
list-style: none;
}
nav li {
display: inline-block;
}
/* SCSS */
nav {
ul {
margin: 0;
padding: 0;
list-style: none;
}
li { display: inline-block; }
}
์ค์ฒฉ ๊ธฐ๋ฅ์ ์ฌ์ฉํ์ฌ ์ฌ์ด ๊ตฌ์ฑ๊ณผ ๋์ ๊ฐ๋ ์ฑ์ ๊ฐ์ง๊ณ ๊ฐ ์ ์์.
๋ชจ๋ํ(Modularity)
/* CSS */
body {
font: 100% Helvetica, sans-serif;
color: #333;
}
.inverse {
background-color: #333;
color: white;
}
/* -------------------------------------------------- */
/* _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;
}
@use ๋ฌธ๋ฒ์ ์ฌ์ฉํ์ฌ ํ์ผ์ ๋ถํ ํ๊ณ ๋ชจ๋ํ ํ ์ ์์.
๋ฏน์ค์ธ(Mixin)
/* 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;
}
/* SCSS */
@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);
}
ํจ์์ฒ๋ผ default parameter๋ฅผ ์ง์ ํ ์ ์๊ณ , parameter๋ฅผ ๋ฐ์์ ์์ฑ์ ๋ถ์ฌํ ์ ์์.
(์ฌ์ฌ์ฉ์ฑ ํ์ฅ)
ํ์ฅ&์์(Extend&Inheritance)
/* CSS */
/* This CSS will print because %message-shared is extended. */
.message, .success, .error, .warning {
border: 1px solid #ccc;
padding: 10px;
color: #333;
}
.success {
border-color: green;
}
.error {
border-color: red;
}
.warning {
border-color: yellow;
}
/* ------------------------------------------------ */
/* SCSS */
/* This CSS will print because %message-shared is extended. */
%message-shared {
border: 1px solid #ccc;
padding: 10px;
color: #333;
}
/* This CSS won't print because %equal-heights is never extended. */
%equal-heights {
display: flex;
flex-wrap: wrap;
}
.message {
@extend %message-shared;
}
.success {
@extend %message-shared;
border-color: green;
}
.error {
@extend %message-shared;
border-color: red;
}
.warning {
@extend %message-shared;
border-color: yellow;
}
@extend ์ฌ์ฉ ์ css ์์ฑ ์งํฉ์ ์์ ๋ฐ์ ์ ์๋ค.
์ฐ์ฐ์(Operators)
/* CSS */
.container {
display: flex;
}
article[role="main"] {
width: 62.5%;
}
aside[role="complementary"] {
width: 31.25%;
margin-left: auto;
}
/* ----------------------------------- */
/* 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;
}
math.div(๋๋๊ธฐ) ์ธ์๋ sin, cos, tan, rendom, max, min ๋ฑ๋ฑ์ ์ํ์ ๊ธฐ๋ฅ์ ์ฌ์ฉํ ์ ์์.