: 웹 프론트엔드 개발의 가장 기본적인 언어
이러한 CSS를 더 효율적으로 작성할 수 있도록 도와주는 CSS 전처리기(Preprocessor) 중 하나인 [ SASS (SCSS) ]
에 대하여 알아보자_!
터미널에 ...
npm i sass
: 설치하기
App.scss
OR index.scss
: scss 확장자 파일 쓰기!
.button
background: cornflowerblue
border-radius: 5px
padding: 10px 20px
& :hover
cursor: pointer
&:disabled
cursor: default
background: grey
pointer-events: none
.button{
background: cornflowerblue;
border-radius: 5px;
padding: 10px 20px;
& :hover{
cursor: pointer;
}
&:disabled{
cursor: default;
background: grey;
pointer-events: none;
}
}
element1 {
background-color: rgb(240, 240, 240);
color: rgb(100, 100, 100);
}
element2 {
background-color: rgb(240, 240, 240);
color: rgb(100, 100, 100);
}
$light-gray: rgb(240, 240, 240)
$dark-gray: rgb(100, 100, 100)
element1 {
background-color: $light-gray;
color: $dark-gray;
}
element2 {
background-color: $light-gray;
color: $dark-gray;
}
background-color: rgb(240, 240, 240);
을 $light-gray
라는 변수로 지정해준 후에 element {
color: white;
}
element:hover{
background-color: lightgray;
}
element div {
color: gray;
}
element div span {
color: blue;
}
element {
&:hover{
background-color: lightgray;
}
color: white;
div {
color: gray;
span {
color: blue;
}
}
}
css
코드에서 중복되는 element
라는 element를 겹쳐서 해당 element 안에 코드를 중첩해준다&
으로 자기 자신을 선택할 수 있음 !!: 반복되는 코드를 재사용할 수 있게 해주는 틀
틀 만들기
@mixin 믹스인이름 {
재사용할 코드
}
틀 사용하기
@include 믹스인이름;
element1 {
display: flex;
flex-direction: column;
align-items: center;
}
element2 {
display: flex;
flex-direction: column;
align-items: center;
}
element3 {
display: flex;
flex-direction: column;
align-items: center;
}
@mixin flex-column {
display: flex;
flex-direction: column;
align-items: center;
}
element1 {
@include flex-column;
}
element2 {
@include flex-column;
}
element3 {
@include flex-column;
}
@mixin flex-column {}
를 이용하여 flex-column 이라는 mixin을 만들어주고 @include flex-column;
를 사용하여 일괄 적용: 속성은 같지만, 값이 다를 때 ...
element1 {
display: flex;
flex-direction: column;
align-items: center;
gap: 20px
}
element2 {
display: flex;
flex-direction: row-reverse;
align-items: flex-start;
gap: 10px
}
element3 {
display: flex;
flex-direction: row;
align-items: center;
gap: 10px
}
$gap-small: 10px;
@mixin flex ( $direction, $align: center , $gap: 20px) {
display: flex;
flex-direction: $direction;
align-items: $align;
gap: $gap
}
element1 {
@include flex(column);
}
element2 {
@include flex(row-reverse, flex-start, $gap-small);
}
element3 {
@include flex($direction: row, $gap: 10px);
}
@mixin flex ( $direction, $align: center , $gap: 20px) {}
여기서 $gap: 20px 이렇게 $gap의 기본값을 20px로 지정해줄 수 있음
element1
: [flex mixin] 변수 중 맨 앞에 있는 $direction; 변수만 특정 지정
element2
: [flex mixin] 변수 에서 $direction;, $align; 두 변수는 특정값 지정,
$gap 변수에는 맨 위에 지정한 $gap-small: 10px;
변수 사용
element3
: [flex mixin] 변수 중 가운데 변수인 $align 변수는 기본값 지정이기 때문에, 해당 속성 직접 언급 지정
.base-button, .primary-button, .secondary-button {
padding: 10px;
border-radius: 5px;
font-size: 14px;
}
.primary-button {
background-color: blue;
color: white;
}
.secondary-button {
background-color: gray;
color: black;
}
.base-button {
padding: 10px;
border-radius: 5px;
font-size: 14px;
}
.primary-button {
@extend .base-button;
background-color: blue;
color: white;
}
.secondary-button {
@extend .base-button;
background-color: gray;
color: black;
}
@extend
를 사용하면 기존 스타일을 다른 클래스에서 재사용할 수 있습니다.SCSS를 적극적으로 활용하여 CSS를 더욱 깔끔하고 체계적으로 관리할 수 있다 !