: CSS Preprocessor (CSS 전처리기)
main.scss
parcel-bundler 패키지를 설치해두면
sass 를 인지하여 자동으로 설치!
sass 는 scss -> css 로 바꿔준다.
$color : royalblue;
.container {
h1 {
color : $color;
}
}
.container {
ul{
li{
.name {
}
.age {
}
}
}
}
.btn {
position: absolute;
&.active {
color: red;
}
}
.fs {
&-small { font-size: 12px; }
&-medium { font-size: 14px; }
&-large { font-size: 16px; }
}
.box{
font: {
weight: bold;
size: 10px;
};
margin: {
top: 10px;
left: 20px;
};
padding: {
top: 10px;
bottom: 40px;
right: 30px;
};
}
$size: 200px;
변수 유효 범위를 활용할 수 있다.
필요한 부분 안쪽에서 생성하면 그 부분안쪽에서만 유효하다.
연산자를 사용할 수 있다.
단축 속성 사용방법과 겹치면 동작하지 않는다. 겹치는 경우 () 소괄호로 묶어주면 사용이 가능하다.
( 변수 할당받아 연산하거나 다른 연산자와 함께 사용하면 가능하다.)
단위가 동일해야한다.
calc() 사용. 다른 단위 연산 가능.
여러개의 속성을 설정하여 재활용할 수 있다.
인수를 넣어 사용할 수 있다.( 기본 설정도 가능 - 인수설정을 안해주면 기본으로 사용 )
// 설정.
@mixin center($size: 100px, $color:tomato) {
display: flex;
justify-content: center;
align-items: center;
width: $size;
background-color: $color;
}
.container {
// 활용
@include center(200px, red);
.item {
@include center($color:tomato);
}
}
1부터 연산한다.( not zero base )
보간 ( #{$i} ) js 의 ${} 과 같은 기능.
실제 값을 넣는 부분은 보간이 필요없다.
@for $i from 1 through 10 {
.box:nth-child(#{$i}) {
width: 100px * $i;
}
}
@function ratio($size, $ratio){
@return $size * $ratio
}
mix($color, red);
: 첫번째 인수 색상과 두번째 인수 색상을 섞는다.
lighten($color, 10%)
: 두번째 인수 만큼 더 밝게 해준다.
darken($color, 10%);
: 두번째 인수 만큼 더 어둡게 해준다.
saturate($color, 10%)
: 두번째 인수 만큼 채도를 올려준다.
desaturate($color, 10%)
: 두번째 인수 만큼 채도를 낮춰준다.
grayscale($color)
: 색상을 회색으로 바꿈
invert($color)
: 색상 반전
rgba($color, .5)
: 두번째 인수만큼 투명도를 준다.
@import url("./sub.scss");
@import "./sub","./sub2";
$list: orange, royalblue, yellow;
$map: ( o: orange, r:royalblue, y:yellow );
$list: orange, royalblue, yellow;
@each $c in $list {
.box {
color: $c;
}
}
.box {
color: orange;
}
.box {
color: royalblue;
}
.box {
color: yellow;
}
@mixin left-top {
position: absolute;
top: 0;
left: 0;
@content;
}
.container {
width: 100px;
height: 100px;
@include left-top {
margin: auto;
}
}
.contatine {
width: 100px;
height: 100px;
position: absolute;
top: 0;
left: 0;
margin: auto;
}