SCSS๋ ๋ค์ํ ๋ฐ์ดํฐ ํ์ ์ ๊ฐ๊ณ ์์ด ๋ณ์๋ก ์ฌ์ฉ์ด ๊ฐ๋ฅ
๋ฐ์ดํฐ ํ์ | |
---|---|
Colors (์์) | red, blue, green ... |
Numbers (์ซ์) | 1, 100px, 2em... |
Strings (๋ฌธ์) | |
Lists (๊ณต๋ฐฑ์ด๋ ,๋ก ๊ตฌ๋ถ๋ ๊ฐ์ ๋ชฉ๋ก) | red blue,(red,blue),... |
Maps (Lists์ ์ ์ฌํ๋ Key : Value ํํ) | (red : a),(blue : b),... |
Booleans (๋ ผ๋ฆฌ) | true, false |
Nulls (์๋ฌด ๊ฐ๋ ์์) | null |
$color:red;
$number:100px;
$string:springboot;
$list:red,blue,green,...;
$map:(red:r, blue:b,...);
$boolean:true;
*์ฃผ์์ฌํญ
๋ณ์ ์ด๋ฆ ์์๋ ๊ผญ $๋ฅผ ๋ถ์ฌ์ผ ํ๋ค.
scss
$primary : blue;
$w : 100px;
.btn{
background : $primary;
width : $w;
}
css
.btn{
background : blue;
width : 100px;
}
์ด๋ฐ์์ผ๋ก ํน์ ๊ฐ์ ๋ณ์๋ฅผ ์ ์ธํด์ ์ฌ์ฉํ ์ ์๋ค.
๋ค๋ง, ๋ณ์๋ ์ ์ธ๋ ๋ธ๋ก ๋ด์์๋ง ์ฌ์ฉ ๊ฐ๋ฅ.
.btn1{
$color : red;
background : $color;
} /* Okay */
.btn2{
background : $color;
} /* Error */
/* scss */
$family: unquote("Droid+Sans");
@import url("http://fonts.googleapis.com/css?family=#{$family}");
/* css */
@import url("http://fonts.googleapis.com/css?family=Droid+Sans");
css์์๋
.div {
...
}
.div .list{
...
}
.div .list li{
...
}
์ฒ๋ผ ์ผ์ผํ ๋๋ ์ ์ง์ ์์ฑํด์ผ ํ์ง๋ง
scss์์๋
.div{
...
.list{
...
li{
...
}
}
}
์ผ๋ก {์์๋ง ์ ์์ฑํด์ฃผ๋ฉด css๋ก ์ปดํ์ผ ํ๋ ๊ณผ์ ์์
์๋์ผ๋ก
.div {
...
}
.div .list{
...
}
.div .list li{
...
}
์ด๋ ๊ฒ ๋ณ๊ฒฝํด์ค๋ค.
scss
.btn{
&.active{
...
}
}
.btn{
...
&:hover{
...
}
}
css
.btn.active{
...
}
.btn{
...
}
.btn:hover{
...
}
@mixin ์ผ๋ก ์ ์ธํ๊ณ @include ๋ก ์ฌ์ฉ
/* mixin ์ ์ธ */
@mixin mine{
color : red;
&:hover{
color : blue;
}
}
/* include๋ก ์ฌ์ฉ */
.btn1{
@include mine;
}
.btn2{
@include mine;
}
@return์ ํตํด ๊ณ์ฐ๋ ํน์ ๋ฐํ ๊ฐ์ ์ฌ์ฉํจ
scss
$w : 100px;
@function myfunc($number){
@return $number*3;
}
.div{
width : $w + myfunc(10);
}
css
.div{
width : 130px;
}
ํน์ ์กฐ๊ฑด์์ ์ฐธ ๊ฑฐ์ง์ ๋ฐ๋ผ ๊ฐ์ ๋ฐํํ๋ ๊ธฐ๋ฅ
/* scss */
$w : 100px;
.btn{
width : if($w > 100px, $w, null);
}
/* css */
.btn{
width : 100px;
}
์กฐ๊ฑด์ ๋ฐ๋ฅธ ๋ถ๊ธฐ ์ฒ๋ฆฌ๊ฐ ๊ฐ๋ฅ
@mixin mine($text){
@if $text == 'no'{
color : red;
}@else if $text == 'yes'{
color : green;
}@else{
color : blue;
}
}
.text1{
@include mine('no');
}
.text2{
@include mine('yes');
}
Style์ ๋ฐ๋ณต์ ์ผ๋ก ์ถ๋ ฅํ๋ ๊ธฐ๋ฅ
from a through b | a ๋ถํฐ b ๊น์ง(b ํฌํจ) ๋ฐ๋ณต |
from a to b | a ๋ถํฐ b ์ ๊น์ง(b ๋ถํฌํจ) ๋ฐ๋ณต |
/* scss */
@for $i from 1 through 3 {
.through:nth-child(#{$i}) {
width: 20px * $i;
}
}
@for $i from 1 to 3 {
.to:nth-child(#{$i}) {
width: 20px * $i;
}
}
/* css */
.through:nth-child(1) {
width: 20px;
}
.through:nth-child(2) {
width: 40px;
}
.through:nth-child(3) {
width: 60px;
}
.to:nth-child(1) {
width: 20px;
}
.to:nth-child(2) {
width: 40px;
}
/* scss */
@each $color in red, blue, green {
.#{$color}-text{
color : #{$color};
}
}
@each $fruit, $color in (apple:red, banana:yellow) {
.#{$fruit}{
color : #{$color};
}
}
/* css */
.red-text{
color : red;
}
.blue-text{
color : blue;
}
.green-text{
color : green;
}
.apple{
color : red;
}
.banana{
color : yellow;
}