SASS(Syntactically Awesome Style Sheets)
๊ธฐ์กด CSS์ ๋จ์ ์ ๋ณด์ํ๊ณ ํจ์จ์ฑ์ ๋์ด๊ธฐ ์ํด ๋ง๋ค์ด์ง ๋ฌธ๋ฒ
CSS ์ฝ๋ ์ค์ฒฉ, ๋ณ์ํ, ์์ฑ ์ฌ์ฌ์ฉ ๋ฑ ์ฌ๋ฌ ๊ธฐ๋ฅ ์ ๊ณต
SCSS(Sassy CSS)
SASS 3๋ฒ์ ์์ ๋ฑ์ฅํ SASS์ ์ ๋ฒ์ ์ผ๋ก ํธํ์ฑ, ๊ฐ๋ ์ฑ์ด ์ข์์ง (๊ธฐ์กด๊ณผ ๋ฌ๋ฆฌ ์ค๊ดํธ ์ฌ์ฉ)
์ฌ์ฉ ์ค๋น
์ค์น
npm install sass
scss ํ์ฅ์๋ก ๋ณ๊ฒฝ
App.scss
index.scss
+ App.jsx์ main.jsx์์ Importํ๋ cssํ์ผ๋ ๋ฐ๊ฟ์ฃผ๊ธฐ!
// app.jsx
import './App.scss'
// main.jsx
import './index.scss'
๋ฌธ๋ฒ
1. ๋ณ์
์ฌ์ฉ๋ฐฉ๋ฒ
/* ์ ์ธ */
$๋ณ์๋ช
: ๊ฐ;
/* ์ฌ์ฉ */
์ ํ์ {
์์ฑ : $๋ณ์๋ช
;
}
* ์์
App.jsx
function App() {
return (
<>
<div className="test">ํ
์คํธ์ฉ div</div>
<h2>SCSS ๋ณ์ ์ฐ์ต</h2>
<ul>
<li>์๋
ํ์ธ์</li>
<li>๋ฐ๊ฐ์ต๋๋ค</li>
<li>scss</li>
</ul>
</>
);
}
App.scss
/* ํฐํธ ํฌ๊ธฐ */
$font-large: 50px;
$font-mid: 40px;
$font-small: 30px;
/* ํฐํธ ๊ตต๊ธฐ */
$font-bold: 900;
$font-thin: 200;
/* ์์ */
$color-green: #116511;
$color-blue: rgb(96, 130, 215);
.test {
font-size: $font-large;
font-weight: $font-bold;
}
h2 {
color: $color-blue;
font-weight: $font-bold;
font-size: $font-mid;
}
ul {
display: gird;
gap: 10px;
}
li {
color: $color-green;
font-weight: $font-thin;
font-size: $font-small;
}
2. ์ค์ฒฉ
๋ค์๊ณผ ๊ฐ์ html ๊ตฌ์กฐ๊ฐ ์์ ๋ ๊ฐ๊ฐ์ ์์์ css ์์ฑ์ ์ง์ ํ๊ธฐ ์ํด์๋
<element>
<div>
<span></span>
</div>
</element>
๋ณดํต ๋ค์๊ณผ ๊ฐ์ด ์ฌ์ฉํ๊ฒ ์ง๋ง
element {
color: white;
}
element div {
color: gray;
}
element div span {
color: blue;
}
scss์์๋ ์ค์ฒฉ ๊ธฐ๋ฅ์ ์ ๊ณตํด ์๋์ ๊ฐ์ด ์ฌ์ฉํ ์ ์๋ค.
(ํ์ง๋ง 2023๋ ๋ถํฐ ๊ธฐ๋ณธ css์์๋ ์ค์ฒฉ ์ ํ์ด ๊ฐ๋ฅํด์ก๋ค... mdn ์ฌ์ดํธ)
element {
color: white;
div {
color: gray;
span {
color: blue;
}
}
}
* ์์
App.jsx
function App() {
return (
<>
<div className="test">ํ
์คํธ์ฉ div</div>
<h2>SCSS ์ค์ฒฉ ์ฐ์ต</h2>
<ul>
<li>์๋
ํ์ธ์
<p>p ์์</p>
</li>
<li>๋ฐ๊ฐ์ต๋๋ค
<section>section ์์</section>
</li>
<li>scss
<article>article ์์
<h3>h3 ์์</h3>
</article>
</li>
<li>๊ณต๋ถ
<h2>h2 ์์</h2>
</li>
</ul>
</>
);
}
App.scss
1๋ฒ ๋ณ์ ์์์์ ์ผ๋ ์ฝ๋์ ์ผ๋ถ๋ฅผ ์ถ๊ฐํ๋ค
li {
color: $color-green;
font-weight: $font-thin;
font-size: $font-small;
/* ๋ถ๋ชจ ๊ท์น์ ์ํด ์ผ์นํ๋ ์์ ์ ํ,
์ด ๊ฒฝ์ฐ์๋ li์์ ์์ ์์ฒด๋ฅผ hover์ ์ ์ฉ๋จ */
&:hover {
background-color: lightgray;
}
p {
color: white;
font-weight: $font-bold;
}
section {
color: skyblue;
}
article {
color: orange;
h3 {
color: black;
}
}
h2 {
color: pink;
}
}
3. ๋ฏน์ค์ธ(Mixin)
๋ฏน์ค์ธ์ด๋ ๋ฐ๋ณต๋๋ ์ฝ๋๋ฅผ ์ฌ์ฌ์ฉํ ์ ์๊ฒ ํด์ฃผ๋ ํ์ด๋ค.
ํจ์์ฒ๋ผ ์ฌ์ฉํ ์ ์๋ค.
์ฌ์ฉ ๋ฐฉ๋ฒ
/* ํ ๋ง๋ค๊ธฐ */
@mixin ๋ฏน์ค์ธ์ด๋ฆ {
์ฝ๋
}
/* ํ ์ฌ์ฉํ๊ธฐ */
@include ๋ฏน์ค์ธ์ด๋ฆ;
์์ 1
App.jsx
function App() {
return (
<>
<div className="test">ํ
์คํธ์ฉ div</div>
<h2>SCSS ๋ฏน์ค์ธ ์ฐ์ต</h2>
<ul>
<li>์๋
ํ์ธ์</li>
<li>๋ฐ๊ฐ์ต๋๋ค</li>
<li>scss</li>
</ul>
<section>
<article>SCSS ๋ฌธ๋ฒ์๋</article>
<article>๋ณ์ ์ฌ์ฉ,</article>
<article>์ค์ฒฉ ๋ฌธ๋ฒ,</article>
<article>๋ฏน์ค์ธ ๋ฑ์ด ์๋ค.</article>
</section>
</>
);
}
App.scss
1๋ฒ ๋ณ์์์ ์ผ๋ ์ฝ๋์ ์ผ๋ถ๋ฅผ ์ถ๊ฐํ๋ค.
- ๋ฏน์ค์ธ์ ์ฌ์ฉํ์ง ์์์ ๋
ul {
display: flex;
flex-direction: column;
align-items: start;
justify-content: center;
gap: 10px;
}
section {
display: flex;
justify-content: space-between;
align-items: center;
gap: 20px;
width: 100%;
}
- ๋ฏน์ค์ธ์ ์ฌ์ฉํ์ ๋
์ด ๋ section์์๋ flex-direction์ ๊ฐ์ ๋ฐ๋ก ์ค์ ํ์ง ์์์๋๋ฐ,
๊ทธ๋ด ๋๋ ๊ธฐ๋ณธ๊ฐ์ธ row๋ฅผ ์ ์ด์ค์ผ ํ๋ค.
@mixin flex($direction, $align, $justify, $gap) {
display: flex;
flex-direction: $direction;
align-items: $align;
justify-content: $justify;
gap: $gap;
}
ul {
@include flex(column, start, center, 10px);
}
section {
@include flex(row, center, space-between, 20px);
}
๋ง์ฝ ๊ทธ๊ฒ ๋ถํธํ๋ค๋ฉด direction์ ํ๋ผ๋ฏธํฐ์ ๋งจ ๋ค๋ก ๋นผ์ค ๋ค์, ์ด๊ธฐ๊ฐ์ ๋ฏธ๋ฆฌ ์ค์ ํด์ค ์ ์๋ค.
@mixin flex($align, $justify, $gap, $direction: row) {
display: flex;
flex-direction: $direction;
align-items: $align;
justify-content: $justify;
gap: $gap;
}
ul {
@include flex(start, center, 10px, column);
}
section {
@include flex(center, space-between, 20px);
}
๊ทธ ์ธ์๋ ์ฌ๋ฌ๊ฐ์ ์์ฑ์ ๋ฐ๋ก ์ค์ ํ๊ณ ์ถ์ง ์์ ๊ฒ์ด ์๋ค๋ฉด
์ฒ์ ์ ์ธํ ๋ ๋ชจ๋ ์ด๊ธฐ๊ฐ์ ์ค์ ํด์ค ๋ค,
์ฌ์ฉ ํ ๋ ์ธ์์ ์์ฑ๋ ํจ๊ป ๋ฃ์ด ๋ช ์ํด์ฃผ๋ฉด ๋๋ค.
@mixin flex($align: start, $justify: start, $gap: 0, $direction: row) {
display: flex;
flex-direction: $direction;
align-items: $align;
justify-content: $justify;
gap: $gap;
}
ul {
@include flex($gap: 10px, $direction: column);
}
์์ 2
App.jsx๋ ์์ 1๊ณผ ๋์ผํ๋ค.
App.scss
/* ํฐํธ ํฌ๊ธฐ */
$font-large: 50px;
$font-mid: 40px;
$font-small: 30px;
/* ํฐํธ ๊ตต๊ธฐ */
$font-bold: 900;
$font-thin: 400;
/* ์์ */
$color-green: #116511;
$color-blue: rgb(96, 130, 215);
/*...์ค๋ต...*/
@mixin font($color, $size, $weight) {
color: $color;
font-size: $size;
font-weight: $weight;
}
ul {
@include flex($gap: 10px, $direction: column);
@include font($color-blue, $font-mid, $font-bold);
}
์ด๋ฐ์ ์์ฑํ ๋ณ์๋ฅผ ๋ฏน์ค์ธ์๋ ํ์ฉํ ์ ์๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
'react' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
React / ์คํ์ผ๋ง (3) - tailwindcss (0) | 2025.04.17 |
---|---|
React / ์คํ์ผ๋ง (2) - Styled Components (1) | 2025.04.16 |
React / custom Hook (1) | 2025.04.11 |
React / useRef (0) | 2025.04.11 |
React / ์๋ช ์ฃผ๊ธฐ (0) | 2025.04.10 |