CSS를 이용해 웹 사이트를 스타일링 하다보면 선택자가 엄청 길어지고 반복해서 써야하는 낭비가 심하다는 것을 느낀다. 나중에 다시 보면 더 헷갈리기도 하고 코드 줄도 어마무시하게 늘어난다. 그래서 CSS 쓰기가 싫어질 정도인데 훨씬 간편하고 여러 기능이 있는 SCSS를 쓴다면 이 불편함을 해결할 수 있다!
오늘은 SCSS 문법에 대해 기록해보려 한다. ㅎㅎ
사실 무언가를 배운다는게 부담되는 것은 사실이다. 안그래도 정말 배울게 많은데 CSS 관련해서도 공부하라니, 처음엔 그냥 CSS 쓰지 뭐 했던 것이 사실이다. 그.러.나!!
SCSS를 쓰면 절대 CSS를 쓰지 못한다는 말을 듣고 얼마나 편하길래..?
하면서 SCSS를 학습해 봤는데,....
정말 신세계다. 모두 SCSS 쓰세요.
CSS 대체하는 다양한 툴들이 있는데 SASS, SCSS를 가장 많이 쓰이는 것 같다.
SASS, SCSS는 같은 계열이긴 한데 문법은 조금 다르다고 알고 있다.
내가 생각하는 SCSS 특장점은
이정도 되는 것 같다.
프론트 개발자라면 꼭 한 번 공부해보는 것을 추천한다!!
.container {
ul {
li {
.name {
color: royalblue;
}
.age {
color: orange;
}
}
}
}
.container ul li .name {
color: royalblue;
}
.container ul li .age {
color: orange;
}
.btn {
position: absolute;
&.active {
color: red;
}
}
.list {
li {
&:last-child {
margin-right: 0;
}
}
}
.btn {
position: absolute;
}
.btn.active {
color: red;
}
.list li:last-child {
margin-right: 0;
}
.fs {
&-small {font-size: 12px}
&-meditum {font-size: 14px}
&-large {font-size: 16px}
}
.fs-small {font-size: 12px}
.fs-meditum {font-size: 14px}
.fs-large {font-size: 16px}
.box {
font: {
weight: bold;
size: 10px;
family: sans-serif;
};
margin: {
top: 10px;
left: 10px;
};
padding: {
top: 10px
bottom: 10px;
};
}
.box {
font-weight: bold;
font-size: 10px;
font-family: sans-serif;
margin-top: 10px;
margin-left: 10px;
padding-top: bottom;
}
$size: 100px;
.container {
position: fixed;
top: $size;
.item {
width: 100px;
height: 100px;
transform: translateX($size);
}
}
div {
width: 20px + 20px;
height: 40px - 10px;
margin: 30px / 2;
padding: 10px % 7;
}
span
SCSS에서 나누기 연산하기
- () 괄호로 묶어주기
- 변수를 이용해서 나눠주기
- 다른 산술연산과 같이 있는 경우
- calc()
- 단위가 맞아야 연산이 가능하지만 calc()를 쓴다면 단위가 맞지 않아도됨
.box {
background-color: royalblue;
width: calc(100% - 200px);
height: 100px;
}
// 수직수평 가운데 정렬하는 코드
@mixin center {
display: flex;
justify-content: center;
aling-items: center;
}
.container {
@include center;
.time {
@include center;
}
}
@mixin box ($size: 100px, $color: tomato) {
width: $size;
height: $size;
background-color: $color;
}
.container {
@include box(200px, red);
.item {
@include box($color: green);
}
}
.box {
@include box;
}
@for $i from 1 through 10 {
.box:nth-child(#{$i}) {
width: 100px * $i;
}
}
@mixin center {
display: flex;
justify-content: center;
align-items: center;
}
@function ratio{$size, $ratio) {
@return $size * $ratio
}
.box {
$width: 100px;
width: $width;
height: ratio($width, 9/16); //영상 비율
@include center;
}
색상 관련
- mix(기존컬러, 새로운색상) ⇒ 색상을 섞어줌
- lighten($color, 10%) ⇒ 색상을 밝게 만들어줌
- darken($color, 10%) ⇒ 색상을 어둡게 만들어줌
- saturate($color, 10%) ⇒ 기존의 채도 올려줌
- desaturate($color, 10%) ⇒ 기존의 채도를 낮춰줌
- grayscale($color) ⇒ 색상을 회색으로 만들어줌
- invert($color) ⇒ 기존 색상을 반전시킴
- rgba($color, .5) ⇒ 투명도를 조절할 수 있음
.box {
$color: royalblue;
width: 200px;
height: 100px;
margin: 20px;
border-radius: 10px;
background-color: $color;
&:hover {
background-color: darken($color, 10%);
}
&.built-in {
background-color: mix($color, blue);
}
}
@import './sub', './sub2';
@each $c in $list {
.box {
color: $c;
}
}
@each $key, $value in $map {
.box-#{$key} {
color: $value;
}
}
@mixin left-top {
position: absolute;
top: 0;
left: 0;
@content;
}
.container {
width: 100px;
height: 100px;
@include left-top;
}
.box {
width: 200px;
height: 300px;
@include left-top {
bottom: 0;
right: 0;
margin: 0;
}
}
패스트캠퍼스 프론트엔드 초격차 패키지 - SCSS
아직 사용하기 익숙하지는 않지만, 잘 쓰면 정말 편하게 쓸 수 있는 것 같다. 내장함수들도 활용한다면 빠르고 쉽게 스타일링 적용이 가능하다. 앞으로 프로젝트 한다면 무조건 SCSS를 애용할 것 같다!