[CSS-12] SCSS

Comely·2025년 6월 6일

CSS

목록 보기
12/12

🎯 Sass란?

Sass (Syntactically Awesome Style Sheets)는 CSS를 더 프로그래밍 언어처럼 작성할 수 있게 해주는 전처리언어(Preprocessor)입니다.

Sass의 장점

  • 변수 사용: 반복되는 값들을 변수로 관리
  • 중첩 구조: HTML 구조와 유사한 CSS 작성
  • 함수와 믹스인: 코드 재사용성 향상
  • 모듈화: 파일 분할로 관리 효율성 증대
  • 확장성: 큰 프로젝트에서도 체계적 관리 가능

🛠️ 개발환경 설정

브라우저는 CSS만 이해함

Sass 코드 (.scss) → 컴파일러 → CSS 코드 (.css) → 브라우저

VSCode 설정 (권장)

  1. Live Sass Compiler 확장 설치
  2. 설정 방법:
    • .scss 파일 생성
    • VSCode 하단 "Watch Sass" 클릭
    • 자동으로 .css 파일 생성됨

Sass vs SCSS 문법

/* SCSS (추천) - 중괄호 사용 */
.box {
  font-size: 16px;
  color: red;
}
/* Sass - 중괄호 생략 */
.box
  font-size: 16px
  color: red

권장: SCSS 문법 (.scss 파일) - 기존 CSS와 호환성 좋음

📦 Sass 문법 1: 변수 (Variables)

기본 변수 사용법

/* 변수 정의 */
$primary-color: #007bff;
$secondary-color: #6c757d;
$font-size-base: 16px;
$border-radius: 4px;

/* 변수 사용 */
.button {
  background-color: $primary-color;
  font-size: $font-size-base;
  border-radius: $border-radius;
}

.card {
  border: 1px solid $secondary-color;
  border-radius: $border-radius;
}

변수의 활용 예시

/* 컬러 팔레트 */
$white: #ffffff;
$black: #000000;
$gray-100: #f8f9fa;
$gray-500: #6c757d;
$gray-900: #212529;

/* 간격 시스템 */
$spacer: 1rem;
$spacers: (
  0: 0,
  1: $spacer * 0.25,
  2: $spacer * 0.5,
  3: $spacer,
  4: $spacer * 1.5,
  5: $spacer * 3
);

/* 폰트 크기 */
$font-size-sm: 0.875rem;
$font-size-base: 1rem;
$font-size-lg: 1.25rem;
$font-size-xl: 1.5rem;

사칙연산

$base-size: 16px;
$container-width: 1200px;

.element {
  font-size: $base-size + 2px;        /* 18px */
  width: ($container-width / 3);      /* 400px */
  padding: ($base-size * 2);          /* 32px */
  margin: $base-size - 4px;           /* 12px */
}

주의사항:

  • 덧셈/뺄셈: 같은 단위끼리만 가능
  • 곱셈/나눗셈: 괄호로 묶어서 사용
  • 단위가 다르면 calc() 함수 사용

🏗️ Sass 문법 2: 중첩 (Nesting)

기본 중첩 구조

/* SCSS */
.navbar {
  background: #333;
  padding: 1rem;
  
  ul {
    list-style: none;
    margin: 0;
    
    li {
      display: inline-block;
      margin-right: 1rem;
      
      a {
        color: white;
        text-decoration: none;
        
        &:hover {
          color: #007bff;
        }
      }
    }
  }
}

컴파일된 CSS 결과

.navbar {
  background: #333;
  padding: 1rem;
}

.navbar ul {
  list-style: none;
  margin: 0;
}

.navbar ul li {
  display: inline-block;
  margin-right: 1rem;
}

.navbar ul li a {
  color: white;
  text-decoration: none;
}

.navbar ul li a:hover {
  color: #007bff;
}

부모 선택자 참조 (&)

.button {
  background: #007bff;
  color: white;
  padding: 10px 20px;
  border: none;
  border-radius: 4px;
  
  /* 호버 상태 */
  &:hover {
    background: #0056b3;
  }
  
  /* 활성 상태 */
  &:active {
    transform: scale(0.98);
  }
  
  /* 수식어 클래스 */
  &.large {
    padding: 15px 30px;
    font-size: 1.2rem;
  }
  
  &.outline {
    background: transparent;
    border: 2px solid #007bff;
    color: #007bff;
  }
}

중첩 주의사항

/* 나쁜 예: 과도한 중첩 (3단계 이상) */
.header {
  .nav {
    .menu {
      .item {
        .link {
          color: blue; /* 너무 깊음 */
        }
      }
    }
  }
}

/* 좋은 예: 적절한 중첩 */
.header {
  background: white;
}

.nav-menu {
  display: flex;
  
  .nav-item {
    margin-right: 1rem;
  }
  
  .nav-link {
    color: #333;
    
    &:hover {
      color: #007bff;
    }
  }
}

🔄 Sass 문법 3: 확장 (@extend)

기본 사용법

/* 기본 버튼 스타일 */
.btn {
  display: inline-block;
  padding: 10px 20px;
  border: none;
  border-radius: 4px;
  font-size: 1rem;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s ease;
}

/* 버튼 변형들 */
.btn-primary {
  @extend .btn;
  background: #007bff;
  color: white;
  
  &:hover {
    background: #0056b3;
  }
}

.btn-secondary {
  @extend .btn;
  background: #6c757d;
  color: white;
  
  &:hover {
    background: #545b62;
  }
}

플레이스홀더 선택자 (%)

/* 플레이스홀더 - CSS로 컴파일되지 않음 */
%button-base {
  display: inline-block;
  padding: 10px 20px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s ease;
}

/* 실제 사용되는 클래스들 */
.btn-success {
  @extend %button-base;
  background: #28a745;
  color: white;
}

.btn-danger {
  @extend %button-base;
  background: #dc3545;
  color: white;
}

🎛️ Sass 문법 4: 믹스인 (@mixin)

기본 믹스인

/* 믹스인 정의 */
@mixin button-style {
  display: inline-block;
  padding: 10px 20px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s ease;
}

/* 믹스인 사용 */
.my-button {
  @include button-style;
  background: #007bff;
  color: white;
}

매개변수가 있는 믹스인

/* 매개변수 믹스인 */
@mixin button($bg-color, $text-color: white, $padding: 10px 20px) {
  display: inline-block;
  padding: $padding;
  background: $bg-color;
  color: $text-color;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s ease;
  
  &:hover {
    background: darken($bg-color, 10%);
  }
}

/* 사용 예시 */
.btn-custom {
  @include button(#ff6b6b);
}

.btn-large {
  @include button(#4ecdc4, white, 15px 30px);
}

.btn-outline {
  @include button(transparent, #007bff);
  border: 2px solid #007bff;
}

실용적인 믹스인 모음

/* 반응형 미디어 쿼리 */
@mixin mobile {
  @media (max-width: 768px) {
    @content;
  }
}

@mixin tablet {
  @media (min-width: 769px) and (max-width: 1024px) {
    @content;
  }
}

@mixin desktop {
  @media (min-width: 1025px) {
    @content;
  }
}

/* 사용 예시 */
.container {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  
  @include mobile {
    padding: 0 15px;
  }
  
  @include tablet {
    padding: 0 30px;
  }
}

/* 플렉스 중앙 정렬 */
@mixin flex-center($direction: row) {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: $direction;
}

.hero-section {
  @include flex-center(column);
  height: 100vh;
}

/* 그라디언트 */
@mixin gradient($start-color, $end-color, $direction: to right) {
  background: linear-gradient($direction, $start-color, $end-color);
}

.header {
  @include gradient(#667eea, #764ba2);
}

📁 Sass 문법 5: 모듈화 (@use, @import)

@use 사용법 (권장)

/* _variables.scss */
$primary-color: #007bff;
$secondary-color: #6c757d;
$font-size-base: 1rem;

/* _mixins.scss */
@mixin button($color) {
  background: $color;
  padding: 10px 20px;
  border: none;
  border-radius: 4px;
}

/* main.scss */
@use 'variables';
@use 'mixins';

.hero {
  color: variables.$primary-color;
  font-size: variables.$font-size-base;
}

.cta-button {
  @include mixins.button(variables.$primary-color);
}

네임스페이스 변경

@use 'variables' as vars;
@use 'mixins' as mix;

.element {
  color: vars.$primary-color;
  @include mix.button(vars.$secondary-color);
}

언더스코어 파일명

/* 파일 구조 */
styles/
├── _variables.scss    /* 컴파일되지 않음 */
├── _mixins.scss      /* 컴파일되지 않음 */
├── _components.scss  /* 컴파일되지 않음 */
└── main.scss         /* 메인 CSS로 컴파일됨 */

🎯 실전 예제: 완전한 컴포넌트 시스템

파일 구조

styles/
├── abstracts/
│   ├── _variables.scss
│   ├── _mixins.scss
│   └── _functions.scss
├── base/
│   ├── _reset.scss
│   └── _typography.scss
├── components/
│   ├── _buttons.scss
│   ├── _cards.scss
│   └── _alerts.scss
├── layout/
│   ├── _header.scss
│   ├── _footer.scss
│   └── _grid.scss
└── main.scss

_variables.scss

// 컬러 시스템
$white: #ffffff;
$gray-100: #f8f9fa;
$gray-200: #e9ecef;
$gray-300: #dee2e6;
$gray-400: #ced4da;
$gray-500: #adb5bd;
$gray-600: #6c757d;
$gray-700: #495057;
$gray-800: #343a40;
$gray-900: #212529;
$black: #000000;

// 테마 컬러
$primary: #007bff;
$secondary: #6c757d;
$success: #28a745;
$info: #17a2b8;
$warning: #ffc107;
$danger: #dc3545;
$light: $gray-100;
$dark: $gray-800;

// 타이포그래피
$font-family-base: 'Helvetica Neue', Arial, sans-serif;
$font-size-base: 1rem;
$font-weight-normal: 400;
$font-weight-bold: 700;
$line-height-base: 1.5;

// 간격
$spacer: 1rem;
$spacers: (
  0: 0,
  1: $spacer * 0.25,
  2: $spacer * 0.5,
  3: $spacer,
  4: $spacer * 1.5,
  5: $spacer * 3
);

// 브레이크포인트
$breakpoints: (
  xs: 0,
  sm: 576px,
  md: 768px,
  lg: 992px,
  xl: 1200px
);

_mixins.scss

@use 'variables' as *;

// 반응형 믹스인
@mixin media-breakpoint-up($name) {
  $min: map-get($breakpoints, $name);
  @if $min != 0 {
    @media (min-width: $min) {
      @content;
    }
  } @else {
    @content;
  }
}

// 버튼 믹스인
@mixin button-variant($background, $border: $background, $color: $white) {
  color: $color;
  background-color: $background;
  border-color: $border;
  
  &:hover {
    color: $color;
    background-color: darken($background, 7.5%);
    border-color: darken($border, 10%);
  }
  
  &:focus {
    box-shadow: 0 0 0 0.2rem rgba($background, 0.5);
  }
  
  &:active {
    background-color: darken($background, 10%);
    border-color: darken($border, 12.5%);
  }
}

// 카드 믹스인
@mixin card-variant($background, $border: rgba($black, 0.125)) {
  background-color: $background;
  border: 1px solid $border;
}

_buttons.scss

@use '../abstracts/variables' as *;
@use '../abstracts/mixins' as *;

.btn {
  display: inline-block;
  font-weight: $font-weight-normal;
  color: $dark;
  text-align: center;
  vertical-align: middle;
  user-select: none;
  background-color: transparent;
  border: 1px solid transparent;
  padding: 0.375rem 0.75rem;
  font-size: $font-size-base;
  line-height: $line-height-base;
  border-radius: 0.25rem;
  transition: color 0.15s ease-in-out, 
              background-color 0.15s ease-in-out, 
              border-color 0.15s ease-in-out, 
              box-shadow 0.15s ease-in-out;
  cursor: pointer;
  text-decoration: none;
  
  &:hover {
    text-decoration: none;
  }
  
  &:focus {
    outline: 0;
  }
  
  // 버튼 변형들
  &-primary {
    @include button-variant($primary);
  }
  
  &-secondary {
    @include button-variant($secondary);
  }
  
  &-success {
    @include button-variant($success);
  }
  
  &-danger {
    @include button-variant($danger);
  }
  
  // 크기 변형
  &-sm {
    padding: 0.25rem 0.5rem;
    font-size: 0.875rem;
    border-radius: 0.2rem;
  }
  
  &-lg {
    padding: 0.5rem 1rem;
    font-size: 1.25rem;
    border-radius: 0.3rem;
  }
}

main.scss

// 추상화 레이어
@use 'abstracts/variables';
@use 'abstracts/mixins';

// 베이스 스타일
@use 'base/reset';
@use 'base/typography';

// 컴포넌트
@use 'components/buttons';
@use 'components/cards';
@use 'components/alerts';

// 레이아웃
@use 'layout/header';
@use 'layout/footer';
@use 'layout/grid';

🏆 실습 과제 해답

1. 리스트 만들기

$primary-color: #007bff;
$gray-light: #f8f9fa;
$gray-dark: #343a40;

.menu-list {
  list-style: none;
  padding: 0;
  margin: 0;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  
  li {
    padding: 15px 20px;
    border-bottom: 1px solid #eee;
    cursor: pointer;
    transition: all 0.3s ease;
    
    &:last-child {
      border-bottom: none;
    }
    
    &:hover {
      background: $gray-light;
    }
    
    &.active {
      background: $primary-color;
      color: white;
    }
  }
}

2. Alert 박스 만들기

// 믹스인 방식
@mixin alert-variant($background, $border, $color) {
  background-color: $background;
  border: 1px solid $border;
  color: $color;
  border-radius: 4px;
  padding: 15px;
  margin-bottom: 20px;
  
  p {
    margin: 0;
    line-height: 1.5;
  }
}

.alert {
  &-success {
    @include alert-variant(#d4edda, #c3e6cb, #155724);
  }
  
  &-warning {
    @include alert-variant(#fff3cd, #ffeaa7, #856404);
  }
  
  &-danger {
    @include alert-variant(#f8d7da, #f5c6cb, #721c24);
  }
}

// 또는 Extend 방식
%alert-base {
  border-radius: 4px;
  padding: 15px;
  margin-bottom: 20px;
  border: 1px solid;
  
  p {
    margin: 0;
    line-height: 1.5;
  }
}

.alert-success {
  @extend %alert-base;
  background-color: #d4edda;
  border-color: #c3e6cb;
  color: #155724;
}

.alert-warning {
  @extend %alert-base;
  background-color: #fff3cd;
  border-color: #ffeaa7;
  color: #856404;
}

.alert-danger {
  @extend %alert-base;
  background-color: #f8d7da;
  border-color: #f5c6cb;
  color: #721c24;
}

3. Column 레이아웃 만들기

// 믹스인 방식
@mixin make-col($size) {
  flex: 0 0 percentage($size / 12);
  max-width: percentage($size / 12);
}

.row {
  display: flex;
  flex-wrap: wrap;
  margin-right: -15px;
  margin-left: -15px;
}

.col-1 { @include make-col(1); }
.col-2 { @include make-col(2); }
.col-3 { @include make-col(3); }
.col-4 { @include make-col(4); }
.col-5 { @include make-col(5); }
.col-6 { @include make-col(6); }
.col-7 { @include make-col(7); }
.col-8 { @include make-col(8); }
.col-9 { @include make-col(9); }
.col-10 { @include make-col(10); }
.col-11 { @include make-col(11); }
.col-12 { @include make-col(12); }

// 공통 컬럼 스타일
[class*="col-"] {
  position: relative;
  width: 100%;
  padding-right: 15px;
  padding-left: 15px;
}

💡 Sass 사용 팁

개발 워크플로우

  1. CSS 먼저 작성: 기능 구현
  2. 반복 패턴 찾기: 중복되는 스타일 확인
  3. 변수 추출: 색상, 크기 등을 변수로 정리
  4. 믹스인 생성: 재사용 가능한 스타일 그룹화
  5. 파일 분할: 기능별로 파일 나누기

성능 고려사항

// 좋은 예: 적절한 중첩
.nav {
  &-item {
    color: blue;
  }
}

// 나쁜 예: 과도한 중첩
.header {
  .nav {
    .item {
      .link {
        color: blue; // 너무 깊음
      }
    }
  }
}

네이밍 컨벤션

// BEM 방식
.card {
  &__header {
    font-weight: bold;
  }
  
  &__body {
    padding: 1rem;
  }
  
  &--featured {
    border: 2px solid gold;
  }
}

Sass를 체계적으로 학습하고 실무에서 효율적으로 활용할 수 있습니다

profile
App, Web Developer

0개의 댓글