Sass ๊ธฐ๋ณธ ๋ฌธ๋ฒ ์ ๋ฆฌ
1) ๋ณ์์ ๋ฐ์ดํฐ ์ ์ฅํด์ ์ฌ์ฉํ๊ธฐ
$๋ณ์๋ช : ๋ณ์์ ๋ฃ์ ๊ฐ;
$mainColor : #ff0000;
.red {
color: $mainColor;
}
2) ํ์ผ import ํ๊ธฐ
@import 'ํ์ผ ๊ฒฝ๋ก';
\- ์ฃผ๋ก ๋ชจ๋ ํ์ด์ง์ ํ์ํ CSS reset์ ํ์ผ์ ์๋ก ์์ฑํ์ฌ ์์ฑํ๊ณ import
//src/reset.scss
body {
margin: 0;
}
div {
box-sizing: border-box;
}
\- Detail.scss์ reset.scss ํ์ผ์ import ํฉ๋๋ค.
@import './reset.scss';
3) Nesting
- ์ ๋ ํฐ ๋์ ์ฌ์ฉ ๊ฐ๋ฅ
// Selector
div.container h4 {
color: blue;
}
div.container p {
color: green;
}
// Nesting
div.container {
h4 {
color: blue;
}
p {
color: green;
}
}
4) Extend
@extend #์์ด๋๋ช ; OR @extend .ํด๋์ค๋ช ;
- ์ฐ์ Detail.js์์ ๊ฐ๋จํ๊ฒ alert๋ฅผ ๋ง๋ค๊ฒ ์ต๋๋ค.
<div className='my-alert'>
<p>์ฌ๊ณ ๊ฐ ์ผ๋ง ๋จ์ง ์์์ต๋๋ค!</p>
</div>
\- Detail.scss์ my-alert์ ๋ํ css ์ฝ๋๋ฅผ ์์ฑํ๊ฒ ์ต๋๋ค.
.my-alert {
background: #eeeeee;
padding: 20px;
border-radius: 5px;
max-width: 500px;
width: 100%;
margin: auto;
p {
margin-bottom: 0;
}
}
\- ๋ค๋ฅธ ์์์ alert UI๊ฐ ํ์ํ๋ค๋ฉด?
: ์ด๋ฐ์์ผ๋ก ์ฝ๋๋ฅผ ๋ณต์ฌ ๋ถ์ฌ๋ฃ๊ธฐ ํ๋ฉด ์ฝ๋ ์๊ฐ ๊ธธ์ด์ง๊ณ ๊ฐ๋ ์ฑ๋ ๋จ์ด์ง๋๋ค.
.my-alert {
background: #eeeeee;
padding: 20px;
border-radius: 5px;
max-width: 500px;
width: 100%;
margin: auto;
p {
margin-bottom: 0;
}
}
.my-alert-red {
background: red;
padding: 20px;
border-radius: 5px;
max-width: 500px;
width: 100%;
margin: auto;
p {
margin-bottom: 0;
}
}
\- ์๋์ ๊ฐ์ด my-alert๋ฅผ extendํ์ฌ ์ฌ์ฉ
.my-alert {
background: #eeeeee;
padding: 20px;
border-radius: 5px;
max-width: 500px;
width: 100%;
margin: auto;
p {
margin-bottom: 0;
}
}
.my-alert-red {
@extend .my-alert;
background: #fa8072;
}
\- Detail.js์์ <div>์ ํด๋์ค๋ช ์ 'my-alert-red'๋ก ์ ์ฉํ๋ฉด ๋ค์๊ณผ ๊ฐ์ด ์คํ์ผ์ด ์ ์ฉ๋ ๊ฒ์ ํ์ธ.
5) @mixin / @include
@mixin ํจ์๋ช () {} / @include ํจ์๋ช ();
\- SASS์ ํจ์๋ฅผ ์ฌ์ฉํ๊ณ ์ถ์ ๋ @mixin์ผ๋ก ์ ์ธํ๊ณ , @include๋ก ํธ์ถํฉ๋๋ค.
@mixin func() {
background: #eeeeee;
padding: 20px;
border-radius: 5px;
max-width: 500px;
width: 100%;
margin: auto;
p {
margin-bottom: 0;
}
}
.my-alert {
@include func();
}
Reference
https://dori-coding.tistory.com/entry/React-SASS-๊ฐ๋ -์ค์น-๋ฐ-๊ธฐ๋ณธ-๋ฌธ๋ฒ?category=1038534
'Development > CSS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Sass] Scss ๋ฌธ๋ฒ ์ ๋ฆฌ ๋งํฌ ๋ชจ์ (0) | 2022.09.12 |
---|---|
[Sass]Sass๋ฅผ ์ ์ฌ์ฉํ ๊น?/ Next.js์ Sass์ค์นํ๊ธฐ (0) | 2022.09.06 |
๋๊ธ