SCSS List(Array), Loops, Variables, Mixins
SCSS Make the CSS likes JavaScript
1. What is SCSS? π©βπ»
1. SCSS is Superset
SCSS(Sassy CSS) λ CSS Pre Processor(μ μ²λ¦¬κΈ°)
μ€ νλλ‘ CSS μ κΈ°λ₯μ νμ₯νλ Script Language
λ€.
κΈ°μ‘΄ CSS κ° κ°μ§ νκ³λ₯Ό 극볡νκΈ° μν΄ λμμΌλ©°, μ€ν¬λ¦½νΈ μΈμ΄, μ¦, νλ‘κ·Έλλ° μΈμ΄μ ν΄λΉνλ€!
λ°λΌμ λ€μν μλ£νμ κΈ°λ³Έμ΄κ³ λ³μ, ν¨μ, λ°λ³΅λ¬Έ, 쑰건문 νμ₯κ³Ό κ°μ κΈ°λ₯ μμ μ 곡νλ€.
νμ§λ§ λΈλΌμ°μ λ CSS λ§ μΈμνλ€. λ°λΌμ μΉμ λ°°ν¬λκΈ° μ SCSS λ₯Ό CSS λ‘ λ³κ²½ν΄μΌνλ€. μ΄κ²μ JavaScript μ λ¬Έμ λ₯Ό ν΄κ²°νκΈ° μν΄ TypeScript λ₯Ό λ§λ€μμ§λ§, λΈλΌμ°μ μμμ JavaScript λ§ λμνλ€λ³΄λ TypeScript λ₯Ό JavaScript λ‘ νΈλμ€νμΌ ν΄μ λ°°ν¬νλ κ²μ²λΌ SCSS μμ CSS λ‘ νΈλμ€νμΌ ν΄μ λ°°ν¬ν΄μΌνλ€.
2. SCSS? SASS?
SCSS μ SASS λ λͺ¨λ CSS μ μ²λ¦¬κΈ°λ‘ κ°μ μν μ νλ€. μ€μ λ‘ SCSS μ ννμ΄μ§λ SASS μ κ°λ€. SASS μ κΈ°μ‘΄μ SASS νκΈ°λ²κ³Ό μλ‘μ΄ SCSS νκΈ°λ² λ κ°μ§κ° μλ κ²μ΄λ€. κ·Έλ¦¬κ³ SASS 3.0 λΆν°λ SCSS νκΈ°λ²μ΄ κΈ°λ³Έ νκΈ°λ²μ΄ λμλ€.
- SASS : Python, Yaml νμΌκ³Ό κ°μ΄ indent λ‘ κ΅¬λΆνλ©°
;
λ‘ μ’ λ£νμ§ μλλ€. - SCSS : CSS λ¬Έλ²κ³Ό λ μ μ¬νλ©°,
{ }
λ‘ κ΅¬λΆνκ³;
λ‘ μ’ λ£λ₯Ό νλ€.
2. Installation and Watch π©βπ»
κΈ°μ‘΄μ Ruby λ Node λ²μ μ SASS λ μ€λ₯λ‘ μΈν΄ Dart λ²μ μ SCSS μ€μΉλ₯Ό κΆμ₯νκ³ μλ€.
npm i -g sass
sass --version
1.59.3 compiled with dart2js 2.19.4
SCSS νμΌμ λ³νκ° μμ λλ§λ€ CSS νμΌλ‘ λ³νλλλ‘ Watch μ€μ μ ν΄μ£Όλλ‘ νμ.
# μλ‘ λ€λ₯Έ λλ ν 리λ₯Ό μ¬μ©νλ κ²½μ°
sass --watch scss/style.scss:css/style.css
# κ°μ λλ ν 리λ₯Ό μ¬μ©νλ κ²½μ°
sass --watch css/style.scss:css/style.css
3. SCSS Variable Declaration and Scope π©βπ»
μ¬κΈ°μ μκ°νλ λͺ¨λ λ΄μ©μ SCSS κ° νΈλμ€νμΌ λμ΄ μ΄λ»κ² CSS λ‘ λ³νλλμ§λ₯Ό 보μ¬μ€λ€.
1. Nesting
.enlarge {
font-size: 14px;
transition: {
property: font-size;
duration: 4s;
delay: 2s;
}
&:hover { font-size: 36px; }
}
.enlarge {
font-size: 14px;
transition-property: font-size;
transition-duration: 4s;
transition-delay: 2s;
}
.enlarge:hover {
font-size: 36px;
}
.info-page {
margin: auto {
bottom: 10px;
top: 2px;
}
}
.info-page {
margin: auto;
margin-bottom: 10px;
margin-top: 2px;
}
2. Hidden Declarations
λ€μκ³Ό κ°μ΄ 쑰건μ μ£Όμ΄ CSS λ‘ νΈλμ€νμΌ ν μ§, νμ§ μμμ§ μ‘°κ±΄μ μ€ μ μλ€.
$rounded-corners: false;
.button {
border: 1px solid black;
border-radius: if($rounded-corners, 5px, null);
}
.button {
border: 1px solid black;
}
3. Custom Properties
λ€μκ³Ό κ°μ΄ SCSS λ³μλ₯Ό CSS μ :root
μ μ μν ν μ μλ€.
$primary: #81899b;
$accent: #302e24;
$warn: #dfa612;
:root {
--primary: #{$primary};
--accent: #{$accent};
--warn: #{$warn};
// Even though this looks like a Sass variable, it's valid CSS so it's not
// evaluated.
--consumed-by-js: $primary;
}
:root {
--primary: #81899b;
--accent: #302e24;
--warn: #dfa612;
--consumed-by-js: $primary;
}
4. Variable Scope
$global-variable: global value;
.content {
$local-variable: local value;
global: $global-variable;
local: $local-variable;
}
.sidebar {
global: $global-variable;
// This would fail, because $local-variable isn't in scope:
// local: $local-variable;
}
.content {
global: global value;
local: local value;
}
.sidebar {
global: global value;
}
$variable: global value;
.content {
$variable: local value;
value: $variable;
}
.sidebar {
value: $variable;
}
.content {
value: local value;
}
.sidebar {
value: global value;
}
@use "sass:map";
$theme-colors: (
"success": #28a745,
"info": #17a2b8,
"warning": #ffc107,
);
.alert {
// Instead of $theme-color-#{warning}
background-color: map.get($theme-colors, "warning");
}
.alert {
background-color: #ffc107;
}
5. Flow Control Scope
$dark-theme: true !default;
$primary-color: #f8bbd0 !default;
$accent-color: #6a1b9a !default;
@if $dark-theme {
$primary-color: darken($primary-color, 60%);
$accent-color: lighten($accent-color, 60%);
}
.button {
background-color: $primary-color;
border: 1px solid $accent-color;
border-radius: 3px;
}
.button {
background-color: #750c30;
border: 1px solid #f5ebfc;
border-radius: 3px;
}
4. SCSS Basic @Rules π©βπ»
1. @use
@import κ° λμΌν μν μ νλ€. λ¨ Deprecated μνλ€ λ§μ°¬κ°μ§μ΄λ―λ‘
@use
λ₯Ό μ νΈνλΌκ³ λ§νκ³ μλ€.
@use
λ JavaScript μ import
μ μ μ¬νλ€. λ¨, νμΌ λ¨μλ‘λ§ κ°μ Έμ¬ μ μλ€.
- foundation/_code.scss
code {
padding: .25em;
line-height: 0;
}
- foundation/_lists.scss
ul, ol {
text-align: left;
& & {
padding: {
bottom: 0;
left: 0;
}
}
}
μ΄μ μ λ νμΌμ style.scss μ ν©μ³λ³΄μ.
- style.scss
@use 'foundation/code';
@use 'foundation/lists';
κ·Έλ¦¬κ³ λ€μκ³Ό κ°μ΄ νΈλμ€νμΌ λ κ²μ΄λ€.
code {
padding: .25em;
line-height: 0;
}
ul, ol {
text-align: left;
}
ul ul, ol ol {
padding-bottom: 0;
padding-left: 0;
}
2. @mixin and @include vs. @extend
μ€νμΌμ μ¬μ¬μ© ν μ μλ λ°©λ²μ ν¬κ² λ κ°μ§λ‘ λλ μ μλ€.
1 ) @mixin
and @include
λ€μκ³Ό κ°μ΄ μ¬μ¬μ© ν μ€νμΌμ μ μν΄λ³΄μ.
@mixin text-style {
font-family: Arial, sans-serif;
font-size: 16px;
line-height: 1.5;
color: #333;
}
μ΄μ μ΄ @mixin
μ λ€μκ³Ό κ°μ΄ @include
λ₯Ό μ΄μ©ν΄ μ¬μ¬μ© ν μ μλ€.
.heading {
@include text-style;
font-weight: bold;
}
.paragraph {
@include text-style;
}
νΈλμ€νμΌ κ²°κ³Όλ λ€μκ³Ό κ°λ€.
.heading {
font-family: Arial, sans-serif;
font-size: 16px;
line-height: 1.5;
color: #333;
font-weight: bold;
}
.paragraph {
font-family: Arial, sans-serif;
font-size: 16px;
line-height: 1.5;
color: #333;
}
2 ) @extend
.btn {
display: inline-block;
padding: 10px 20px;
font-size: 16px;
text-align: center;
color: #fff;
background-color: #333;
}
.btn-primary {
@extend .btn;
background-color: #007bff;
}
νΈλμ€νμΌ κ²°κ³Όλ λ€μκ³Ό κ°λ€.
.btn, .btn-primary {
display: inline-block;
padding: 10px 20px;
font-size: 16px;
text-align: center;
color: #fff;
background-color: #333;
}
.btn-primary {
background-color: #007bff;
}
μ΄λ€ κ²μ μ¨μΌ ν κΉ?
1 ) @mixin
and @include
- λͺ©μ : μ€νμΌ Text μ μ μ체λ₯Ό μ¬μ¬μ©νκΈ° μν¨.
- μ₯μ :
@mixin
κ³Ό@include
μ μ¬μ© λͺ©μ μ μ½λ μ체μ μ¬μ¬μ©μ±μ΄λ€. ν λ²λ§ μ μνκ³ , νμν κ³³μμ@include
λ₯Ό μ¬μ©ν΄ μ£Όμ λ§ νλ©΄ λλ€.@mixin
λ₯Ό ν΅ν΄ ν€κ°μ΄ λͺ μμ μΌλ‘ μ 곡λκΈ° λλ¬Έμ μ¬μ©μ΄ μ½κ³ κ°λ μ±μ΄ μ’λ€. - λ¨μ : μ€νμΌ Text μμ²΄κ° λ³΅μ¬λμ΄ λ§€λ² μ£Όμ
λκΈ° λλ¬Έμ SCSS λ₯Ό μμ±ν λλ μ½λμ μ€λ³΅μ΄ μ€μ΄λ€μ§λ§ μ΅μ’
κ²°κ³Όλ¬ΌμΈ CSS νΈλμ€νμΌμ μ€λ³΅μ΄
λ°μνλ€. μ¦,
DRY(Don't repeat yourself)
μμΉμ μλ°°λλ€. λ°λΌμ λ무 λ§μ@mixin
μ μ¬μ©μ μ’μ§ μλ€.
2 ) @extend
- λͺ©μ : μ€νμΌ Text μ μκ° μλ CSS μ νμλ₯Ό μ¬μ¬μ©νκΈ° μν¨.
- μ₯μ : κ²°κ³Όμ μΌλ‘ μμμ μ²λ¦¬νλ€. νΈλμ€νμΌμ κ²°κ³Όλ¬Όλ§ λ³΄λ©΄ DRY μμΉμ μλ°°λμ§ μμ λ§€μ° μ’μ κ²°κ³Όλ¬Όμ 보μ¬μ€λ€.
- λ¨μ : μ€νμΌ Text μ μ μ체λ₯Ό μ¬μ¬μ©νμ§λ μκΈ° λλ¬Έμ CSS μ μ μ체λ₯Ό μ νλ κ²μ΄ μ€μνλ€. μ¦, SCSS λ₯Ό κΈ°μ€μΌλ‘ CSS λ₯Ό λ§λλ λλ
λ³΄λ€ CSS λ₯Ό κΈ°μ€μΌλ‘ μ¬μ¬μ© ν λΆλΆμ κ³΅ν΅ λΆλΆμΌλ‘ μΆμΆν΄ μμμ ν΅ν΄ μ²λ¦¬νλ λλμ κ°κΉλ€. λ°λΌμ SCSS νμΌμ κ°λ
μ±μ΄
@mixin
and@include
λλΉ λΆμ‘±νλ€.
5. @if and @else π©βπ»
$light-background: #f2ece4;
$light-text: #036;
$dark-background: #6b717f;
$dark-text: #d2e1dd;
@mixin theme-colors($light-theme: true) {
@if $light-theme {
background-color: $light-background;
color: $light-text;
} @else {
background-color: $dark-background;
color: $dark-text;
}
}
.banner {
@include theme-colors($light-theme: true);
body.dark & {
@include theme-colors($light-theme: false);
}
}
.banner {
background-color: #f2ece4;
color: #036;
}
body.dark .banner {
background-color: #6b717f;
color: #d2e1dd;
}
6. Loops π©βπ»
SCSS λ₯Ό ν΅ν΄ for, forEach, while μ λͺ¨λ μ¬μ©ν μ μλ€. μ°μ κ°μ₯ κ°λ¨ν κΈ°λ³Έ ννλ₯Ό νμΈν ν μ€μ λ‘ μ΄λ€μμΌλ‘ μ¬μ©λ μ μλμ§ μμΈν νμΈν΄λ³΄λλ‘ νμ.
$colors: red, green, blue;
// for loop
@for $i from 1 through length($colors) {
.color-#{$i} {
color: nth($colors, $i);
}
}
// for loop (1λΆν° 5 λ―Έλ§κΉμ§)
@for $i from 1 to 4 {
.color-#{$i} {
color: nth($colors, $i);
}
}
// each loop
@each $color in $colors {
.color-#{index($colors, $color)} {
color: $color;
}
}
// while loop
$i: 1;
@while $i <= length($colors) {
.color-#{$i} {
color: nth($colors, $i);
}
$i: $i + 1;
}
1. @each with List or Map
1 ) @each
with List
$sizes: 40px, 50px, 80px;
@each $size in $sizes {
.icon-#{$size} {
font-size: $size;
height: $size;
width: $size;
}
}
.icon-40px {
font-size: 40px;
height: 40px;
width: 40px;
}
.icon-50px {
font-size: 50px;
height: 50px;
width: 50px;
}
.icon-80px {
font-size: 80px;
height: 80px;
width: 80px;
}
2 ) @each
with Map
$icons: ("eye": "\f112", "start": "\f12e", "stop": "\f12f");
@each $name, $glyph in $icons {
.icon-#{$name}:before {
display: inline-block;
font-family: "Icon Font";
content: $glyph;
}
}
.icon-eye:before {
display: inline-block;
font-family: "Icon Font";
content: "\f112";
}
.icon-start:before {
display: inline-block;
font-family: "Icon Font";
content: "\f12e";
}
.icon-stop:before {
display: inline-block;
font-family: "Icon Font";
content: "\f12f";
}
2. @for
$base-color: #036;
@for $i from 1 through 3 {
ul:nth-child(3n + #{$i}) {
background-color: lighten($base-color, $i * 5%);
}
}
ul:nth-child(3n + 1) {
background-color: #004080;
}
ul:nth-child(3n + 2) {
background-color: #004d99;
}
ul:nth-child(3n + 3) {
background-color: #0059b3;
}
7. Use &
Operator π©βπ»
&
μ°μ°μλ₯Ό μ¬μ©νλ©΄ @if
μ κ°μ 쑰건문 μμ΄ νΉμ CSS 쑰건μ ν κΈνλλ‘ ν μ μλ€.
main {
figure {
em {
opacity: 0;
&.on {
opacity: 0.8;
}
}
}
}
main figure em {
opacity: 0;
}
main figure em.on {
opacity: 0.8;
}
main {
&.dark_text {
header h1,
header #gnb a {
color: #555;
}
}
header {
h1 {
color: #fff;
}
#gnb {
font-weight: bold;
a {
color: #fff;
}
}
}
}
main.dark_text header h1,
main.dark_text header #gnb a {
color: #555;
}
main header h1 {
color: #fff;
}
main header #gnb {
font-weight: bold;
}
main header #gnb a {
color: #fff;
}
$times: morning, afternoon, evening, night;
.container {
@each $time in $times {
&.#{$time} {
background-image: url('../img/bg_#{$time}.jpg');
figure {
background-image: url('../img/phone_#{$time}.png');
}
}
}
}
.container.morning {
background-image: url("../img/bg_morning.jpg");
}
.container.morning figure {
background-image: url("../img/phone_morning.png");
}
.container.afternoon {
background-image: url("../img/bg_afternoon.jpg");
}
.container.afternoon figure {
background-image: url("../img/phone_afternoon.png");
}
.container.evening {
background-image: url("../img/bg_evening.jpg");
}
.container.evening figure {
background-image: url("../img/phone_evening.png");
}
.container.night {
background-image: url("../img/bg_night.jpg");
}
.container.night figure {
background-image: url("../img/phone_night.png");
}
8.keyframes π©βπ»
λ¨μ CSS μ§λ§ SCSS κ° μ΄λ°μμ μ€μ²© ꡬ쑰λ₯Ό κΉλνκ² μ²λ¦¬νλ λ°μ κ°μ μ κ°λλ€.
@keyframes cloud {
0% {
opacity: 0;
left: -50%;
}
5% {
opacity: 0.5;
}
95% {
opacity: 0.5; /* μκ° μμ΄μΌ 5 ~ 95% ꡬκ°μ opacity κ° 0.5 λ‘ μ μ§κ° λλ€ */
}
100% {
left: 80%;
opacity: 0;
}
}
Reference
- βSASS.β Sass-lang. accessed Apr. 15, 2023, SASS.