1. 셀렉터 중첩 (Nesting)
부모 셀렉터가 후손 셀렉터를 감싸고 있는 형식
Scss
nav {
ul {
margin: 0;
padding: 0;
list-style: none;
}
li { display: inline-block; }
a {
display: block;
padding: 6px 12px;
text-decoration: none;
}
}
CSS
nav ul {
margin: 0;
padding: 0;
list-style: none;
}
nav li {
display: inline-block;
}
nav a {
display: block;
padding: 6px 12px;
text-decoration: none;
}
2. 속성 중첩
아래 코드에 있는 .box 셀렉터는 속성값으로 font-weight, font-size, font-family를 가진다. 이 속성값들은 font-라는 네임스페이스를 가진다. 네임 스페이스란 이름을 통해 구분 가능한 범위를 만들어내는 것을 말한다. 속성이 가지고 있는 이름 부분의 영역 (font-)가 같으므로 네임 스페이스가 같다고 말한다. 같은 네임스페이스인 속성을 중첩시킬 수 있다.
Scss
.box {
font: {
weight: bold;
size: 10px;
family: 'Open Sans';
};
margin: {
top: 10px;
left: 20px;
};
}
CSS
.box {
font-weight: bold;
font-size: 10px;
font-family: 'Open Sans';
margin-top: 10px;
margin-left: 20px;
}
3. 상위 선택자 참조 (&)
& 기호는 상위 선택자를 참조한다. 즉 & 기호는 부모 선택자로 치환이된다.
Scss
.fs {
&-small { font-size: 12px; }
&-medium { font-size: 15px; }
&-large { font-size: 18px; }
}
CSS
.fs-small {
font-size: 12px;
}
.fs-medium {
font-size: 15px;
}
.fs-large {
font-size: 18px;
}
4. 변수 ($variable)
- 자주 사용하는 스타일코드를 변수로 지정해 같은 UI에 동일한 스타일을 유지할 수 있다.
- 변수로 지정하면 좋은 요소: 색상, 사이즈
변수선언하기
$변수명: 값;
변수를 중괄호 밖에서 선언하면 전역적으로 사용할 수 있고 변수를 중괄호 안에서 선언하면 해당하는 중괄호 안에서만 지역적으로 사용할 수 있다.
4.1 변수를 전역적으로 사용하기
Scss
$size: 100px;
.container {
position: fixed;
top: $size;
.item {
width: $size;
height: $size;
transform: translateX($size);
}
}
CSS
.container {
position: fixed;
top: 100px;
}
.container .item {
width: 100px;
height: 100px;
transform: translateX(100px);
}
4.2 변수를 지역적으로 사용하기
Scss
.container {
/* 변수 size 선언 */
$size: 200px;
position: fixed;
top: $size;
.item {
/* 변수 size 재선언: 아래에 있는 모든 변수 size는 재선언된 값을 가지고 있다 */
$size: 100px;
width: $size;
height: $size;
transform: translateY($size);
}
/* 재선언된 값을 가지고 있는 변수 size */
left: $size;
}
CSS
.container {
position: fixed;
top: 200px;
left: 100px;
}
.container .item {
width: 100px;
height: 100px;
transform: translateX(100px);
}
5. 산술연산 (+, -, *, /, %)
Scss도 더하기, 빼기, 곱하기, 나누기, 나머지 기호를 사용해 산술연산을 할 수 있다. 다만, / 기호는 CSS에서 단축속성으로 사용하기 때문에 Scss에서 나누기 연산을 하려면 소괄호로 나눗셈 연산을 묶어줘야한다.
Scss
div {
width: 20px + 20px;
height: 40px - 10px;
font-size: 10px * 2;
margin: (10px / 2);
padding: 20px % 7;
}
CSS
div {
width: 40px;
height: 30px;
font-size: 20px;
margin: 5px;
padding: 6px;
}
6. 코드재활용하기 (@mixin)
자주쓰이는 스타일코드를 묶어 @mixin 이름으로 선언한 후 사용하고 싶을때마다 @include 이름으로 해당 스타일 코드를 적용해준다.
Scss
@mixin center {
display: flex;
justify-content: center;
align-items: center;
}
.container {
@include center;
.item {
@include center;
}
}
.box {
@include center;
}
CSS
.container {
display: flex;
justify-content: center;
align-items: center;
}
.container .item {
display: flex;
justify-content: center;
align-items: center;
}
.box {
display: flex;
justify-content: center;
align-items: center;
}
6.1 mixin으로 인수 받기
@mixin을 하나의 함수처럼 사용하는 방법이다. @mixin을 사용해 반복적으로 사용할 스타일 코드를 정의한다. 이때 @mixin으로 정의한 스타일코든느 함수처럼 매개변수(parameter) 값을 받을 수 있고 해당 스타일을 @include로 호출할때도 인자(argument)를 전달해줄 수 있다. 단, 여기서 주의할 점은 함수를 호출할때는 함수의 매개변수와 함수의 인자의 순서가 일치되어야 한다.
Scss
@mixin box($size) {
width: $size;
height: $size;
background-color: tomato;
}
.container {
@include box(200px);
.item {
@include box(100px);
}
}
.box {
@include box(100px)
}
CSS
.container {
width: 200px;
height: 200px;
background-color: tomato;
}
.container .item {
width: 100px;
height: 100px;
background-color: tomato;
}
.box {
width: 100px;
height: 100px;
background-color: tomato;
}
6.2 mixin로 만든 함수에 기본값 설정해주기
@mixin으로 만든 함수의 기본 매개변수값을 지정해줘 인자 없이 함수를 부를때 기본 매개변수 값이 들어가게 한다.
Scss
@mixin box($size: 100px, $color: tomato) {
width: $size;
height: $size;
background-color: $color;
}
.container {
@include box(200px, royalblue);
.item {
@include box($color: green);
}
}
.box {
@include box;
}
CSS
.container {
width: 200px;
height: 200px;
background-color: royalblue;
}
.container .item {
width: 100px;
height: 100px;
background-color: green;
}
.box {
width: 100px;
height: 100px;
background-color: tomato;
}
6.3 @mixin에 @content 키워드 넣어주기
@mixin에 정의된 스타일을 추가하거나 변경이 가능하게 하려면 @mixin에 @content 키워드를 넣어준다. @content를 사용하면 @include로 @mixin에 정의된 스타일 코드를 수정해 사용할 수 있다.
Scss
@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: auto;
}
}
CSS
.container {
width: 100px;
height: 100px;
position: absolute;
top: 0;
left: 0;
}
.box {
width: 200px;
height: 300px;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
margin: auto;
}
7. 함수 (@function)
@mixin과 @function의 차이점
@mixin은 자주 사용하는 CSS 코드를 재사용할 수 있게 해주는 반면 연산할 수 없다.
- mixin: CSS 코드의 모음
- function: 받아온 값을 처리하는 연산
Scss
@mixin center {
display: flex;
justify-content: center;
align-items: center;
}
@function ratio($size, $ratio) {
@return $size * $ratio
}
.box {
$width: 160px;
width: $width;
height: ratio($width, 9/16);
@include center;
}
CSS
.box {
width: 160px;
height: 90px;
display: flex;
justify-content: center;
align-items: center;
}
8. 색상 내장 함수
1) mix()
mix 함수 안에 있는 색상을 모두 섞은 새로운 색을 리턴한다.
color: mix(색상1, 색상2...);
2) lighten()
색상을 해당 % 만큼 밝아진 새로운 색을 리턴한다.
color: lighten(색상, %);
3) darken()
색상을 해당 % 만큼 어두워진 새로운 색을 리턴한다.
color: darken(색상, %);
4. saturate()
색상을 해당 % 만큼 채도를 높인 색을 리턴한다.
color: saturate(색상, %);
5) desaturate()
색상을 해당 % 만큼 채도를 낮춘 색을 리턴한다.
color: saturate(색상, %);
6) grayscale()
색상을 그레이스케일로 변환한 색을 리턴한다.
color: grayscale(색상);
7) invert()
색상을 반전시킨 색을 리턴한다.
color: invert(색상);
8) rgba()
색상에 해당 투명도 적용한 색을 리턴한다. 투명도에는 % 또는 소수점이 올 수 있다.
color: rgba(색상, 투명도);
Scss
.box {
$color: royalblue;
width: 200px;
height: 100px;
margin: 20px;
border-radius: 10px;
background-color: $color;
&:hover {
background-color: lighten($color, 10%);
}
}
CSS
.box {
width: 200px;
height: 100px;
margin: 20px;
border-radius: 10px;
background-color: royalblue;
}
.box:hover {
background-color: #6d8ce8;
}
9. 반복문 (@for)
자바스크립트 for문처럼 Scss에서도 반복문을 사용할 수 있다. 반복할 숫자를 코드 상에서 사용할때는 #{$변수명}으로 부른다.
Scss
@for $i from 1 through 5 {
.box:nth-child(#{$i}) {
background-image: url("./img/profile-#{$i}.png");
}
}
CSS
.box:nth-child(1) {
background-image: url("./img/profile-1.png");
}
.box:nth-child(2) {
background-image: url("./img/profile-2.png");
}
.box:nth-child(3) {
background-image: url("./img/profile-3.png");
}
.box:nth-child(4) {
background-image: url("./img/profile-4.png");
}
.box:nth-child(5) {
background-image: url("./img/profile-5.png");
}
10. @each 키워드를 사용한 반복문 (@each)
@each 키워드를 사용하면 list, map에 대한 반복문을 사용 할 수 있다.
10.1 list 데이터를 @each 반복문으로 돌려주기
Scss에서 list 데이터는 자바스크립트 배열처럼 데이터를 여러개 담는 타입이다. 아래의 코드는 $list 변수 안에 있는 데이터를 하나씩 꺼내 c라는 변수에 담아 중괄호에서 처리한다.
Scss
$list: red, orange, yellow;
@each $c in $list {
.box {
color: $c;
}
}
CSS
.box {
color: red;
}
.box {
color: orange;
}
.box {
color: yellow;
}
10.2 map 데이터를 @each 반복문으로 돌려주기
Scss에서 map 데이터는 자바스크립트에서의 객체처럼 key와 value로 이루어져 있다. 아래의 코드는 $map 변수 안에 있는 key, value데이터를 하나씩 꺼내 $k, $v라는 변수에 담아 중괄호에서 처리한다.
Scss
$map: (
r: red,
o: orange,
y: yellow
);
@each $k, $v in $map {
.box-#{$k} {
color: $v;
}
}
CSS
.box-r {
color: red;
}
.box-o {
color: orange;
}
.box-y {
color: yellow;
}'Sass · Scss' 카테고리의 다른 글
| [Sass/Scss] 주석처리하기 (0) | 2022.04.20 |
|---|---|
| [Sass/Scss] 사스의 데이터 타입 7가지 (0) | 2022.04.20 |
| [Sass/Scss] 색상 변수에 opacity값 넣기 (0) | 2022.04.17 |
| [Sass/Scss] Live Sass Compiler를 사용해 컴파일된 css파일 경로 자동으로 바꾸기 (0) | 2022.03.16 |
| [Sass/Scss] Scss 프로젝트 시작하기 (0) | 2022.03.16 |
댓글