Sass (Syntactically Awesome Style Sheets)는 CSS를 더 프로그래밍 언어처럼 작성할 수 있게 해주는 전처리언어(Preprocessor)입니다.
Sass 코드 (.scss) → 컴파일러 → CSS 코드 (.css) → 브라우저
.scss 파일 생성.css 파일 생성됨/* SCSS (추천) - 중괄호 사용 */
.box {
font-size: 16px;
color: red;
}
/* Sass - 중괄호 생략 */
.box
font-size: 16px
color: red
권장: SCSS 문법 (.scss 파일) - 기존 CSS와 호환성 좋음
/* 변수 정의 */
$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 */
}
주의사항:
/* 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;
}
}
}
}
}
.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;
}
}
}
/* 기본 버튼 스타일 */
.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;
}
/* 믹스인 정의 */
@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);
}
/* _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
// 컬러 시스템
$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
);
@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;
}
@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;
}
}
// 추상화 레이어
@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';
$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;
}
}
}
// 믹스인 방식
@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;
}
// 믹스인 방식
@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;
}
// 좋은 예: 적절한 중첩
.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를 체계적으로 학습하고 실무에서 효율적으로 활용할 수 있습니다