Contents
see List작성일 2025. 11. 30.
SCSS/SASS 기초와 활용
SCSS(Sassy CSS)는 CSS를 더 효율적으로 작성할 수 있게 해주는 전처리기입니다. 변수, 중첩, 믹스인, 함수 등을 제공하여 유지보수가 쉬운 스타일시트를 만들 수 있습니다.
언제 사용하나요?
- 대규모 프로젝트의 CSS 관리
- 반복되는 스타일 패턴 재사용
- 테마 시스템 구축
- 컴포넌트 기반 스타일링
설치 및 컴파일
# 설치
npm install sass --save-dev
# 컴파일
npx sass src/styles:dist/css --watch
# package.json 스크립트
"scripts": {
"sass": "sass src/scss:dist/css --watch"
}변수
// _variables.scss
$primary-color: #3498db;
$secondary-color: #2ecc71;
$font-stack: "Noto Sans KR", sans-serif;
$spacing-unit: 8px;
// 사용
.button {
background: $primary-color;
font-family: $font-stack;
padding: $spacing-unit * 2;
}중첩 (Nesting)
.card {
padding: 20px;
&__header {
font-size: 18px;
&--highlight {
color: $primary-color;
}
}
&__body {
margin-top: 10px;
}
// 가상 선택자
&:hover {
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}
// 미디어 쿼리 중첩
@media (max-width: 768px) {
padding: 10px;
}
}
// 컴파일 결과
// .card { ... }
// .card__header { ... }
// .card__header--highlight { ... }
// .card:hover { ... }믹스인 (Mixin)
// 정의
@mixin flex-center {
display: flex;
justify-content: center;
align-items: center;
}
@mixin button($bg-color, $text-color: white) {
background: $bg-color;
color: $text-color;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
&:hover {
background: darken($bg-color, 10%);
}
}
// 사용
.container {
@include flex-center;
}
.btn-primary {
@include button($primary-color);
}
.btn-secondary {
@include button($secondary-color, black);
}반복문과 조건문
// @each
$sizes: (sm: 12px, md: 16px, lg: 20px, xl: 24px);
@each $name, $size in $sizes {
.text-#{$name} {
font-size: $size;
}
}
// @for
@for $i from 1 through 5 {
.col-#{$i} {
width: 20% * $i;
}
}
// @if
@mixin theme($mode) {
@if $mode == dark {
background: #333;
color: #fff;
} @else {
background: #fff;
color: #333;
}
}함수
// 내장 함수
$color: #3498db;
lighten($color, 20%); // 밝게
darken($color, 20%); // 어둡게
saturate($color, 20%); // 채도 높게
rgba($color, 0.5); // 투명도
// 커스텀 함수
@function rem($px) {
@return #{$px / 16}rem;
}
.text {
font-size: rem(14); // 0.875rem
padding: rem(16); // 1rem
}파일 분리와 Import
// 파일 구조
styles/
├── main.scss
├── _variables.scss
├── _mixins.scss
├── _reset.scss
├── components/
│ ├── _button.scss
│ └── _card.scss
└── pages/
└── _home.scss
// main.scss
@use "variables";
@use "mixins";
@use "reset";
@use "components/button";
@use "components/card";
@use "pages/home";
// 네임스페이스로 접근
.element {
color: variables.$primary-color;
@include mixins.flex-center;
}확장 (@extend)
%button-base {
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
}
.btn-primary {
@extend %button-base;
background: $primary-color;
}
.btn-secondary {
@extend %button-base;
background: $secondary-color;
}유용한 믹스인 모음
// 반응형 브레이크포인트
@mixin mobile {
@media (max-width: 767px) { @content; }
}
@mixin tablet {
@media (max-width: 1023px) { @content; }
}
// 사용
.container {
width: 1200px;
@include tablet { width: 100%; }
@include mobile { padding: 10px; }
}
// 텍스트 말줄임
@mixin ellipsis($lines: 1) {
@if $lines == 1 {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
} @else {
display: -webkit-box;
-webkit-line-clamp: $lines;
-webkit-box-orient: vertical;
overflow: hidden;
}
}html
| No | 작성일 | Title |
|---|---|---|
| 3186 | 2026. 07. 05. | HTML Popover API로 자바스크립트 드롭다운 메뉴 줄이기 |
| 3144 | 2026. 06. 27. | CSS 컨테이너 쿼리 운영 가이드: 화면이 아니라 컴포넌트 폭에 맞춰 레이아웃 바꾸기 |
| 3088 | 2026. 06. 19. | 접근성 있는 모달 창 구현: dialog, focus, 닫기 동작을 함께 설계하기 |
| 2999 | 2026. 06. 11. | HTML 폼 검증 운영 가이드: 브라우저 기본 검증과 서버 검증을 함께 설계하기 |
| 2943 | 2026. 06. 03. | CSS 컨테이너 쿼리 실전 적용: 화면이 아니라 컴포넌트 너비에 맞춰 카드 레이아웃 바꾸기 |
| 2888 | 2026. 05. 26. | 반응형 이미지 운영 가이드: srcset, sizes, picture, lazy loading으로 LCP와 트래픽 줄이기 |
| 2824 | 2026. 05. 18. | 실무 문의 폼 접근성 점검: fieldset, autocomplete, Constraint Validation API 적용법 |
| 2626 | 2026. 04. 27. | CSS Anchor Positioning 완벽 가이드 — JavaScript 없이 툴팁·팝오버를 정밀하게 배치하는 새로운 CSS 표준 |
| 2547 | 2026. 04. 19. | HTML Invoker Commands API 완벽 가이드: JavaScript 없이 버튼으로 팝업·다이얼로그 제어하기 |
| 2494 | 2026. 04. 14. | 2025/2026 CSS 최신 기능 완벽 정리: View Transitions, Anchor Positioning, 스크롤 애니메이션 |