본문 바로가기
seungchan.dev
뒤로

Sass 기본 문법: 변수, 중첩, 믹스인

Sass(SCSS)의 변수와 스코프, 선택자·속성 중첩, 믹스인 인자와 기본값, @content, 믹스인으로 만든 미디어 쿼리를 예제 코드로 정리했습니다.

1분 읽기#CSS

2024년에 쓴 글이라 지금과 다른 내용이 있을 수 있습니다.

1. 변수

css
 
$max-screen-width: 1200px;
$theme-color: #34a753;
 
.home {
	max-width: $max-screen-width;
    background-color: $theme-color;
}
 
변수 스코프
css
 
.home {
  $max-screen-width: 1200px;
  $theme-color: #34a753;
  max-width: $max-screen-width;
  background-color: $theme-color;
}
 
.article {
  color: $theme-color; /* Undefined variable $theme-color 컴파일 에러 */
}
 

2. 중첩

네이티브 CSS

css
.navbar {
  background-color: #333;
  color: #fff;
  padding: 10px;
}
 
.navbar a {
  color: #fff;
  text-decoration: none;
  padding: 5px 10px;
}
 
.navbar a:hover {
  background-color: #555;
}
 
.submenu {
  display: none;
}
 
.submenu-trigger:hover + .submenu {
  display: block;
  position: absolute;
  top: 100%;
  left: 0;
  background-color: #555;
}
 
.submenu a {
  display: block;
  padding: 5px 10px;
  color: #fff;
  text-decoration: none;
}
 
.submenu a:hover {
  background-color: #777;
}

SCSS

css
.navbar {
  background-color: #333;
  color: #fff;
  padding: 10px;
  
  a {
    color: #fff;
    text-decoration: none;
    padding: 5px 10px;
 
    &:hover {
      background-color: #555;
    }
  }
 
  .submenu {
    display: none;
 
    &:hover {
      display: block;
      position: absolute;
      top: 100%;
      left: 0;
      background-color: #555;
      
      a {
        display: block;
        padding: 5px 10px;
        color: #fff;
        text-decoration: none;
 
        &:hover {
          background-color: #777;
        }
      }
    }
  }
}
속성 중첩
css
/* css */
 
.hero {
    background-color: transparent;
    background-image: none;
    background-repeat: repeat;
    background-position: top left;
    background-attachment: scroll;
}
 
/* scss */
 
.hero {
   background {
    color: transparent;
    image: none;
    repeat: no-repeat;
    position: top left;
    attachment: scroll;
    }
}

3. 믹스인

선언

css
 
@mixin flex-center {
    display: flex;
    justify-content: center;
    align-items: center;
}

사용

css
 
.box {
 @include flex-center;
}
 
.box1 {
 @include flex-center;
}
믹스인 인자
css
@mixin flex($justify, $align) {
    display: flex;
    justify-content: $justify;
    align-items: $align;
}
 
.box {
  @include flex(space-between, center);
}
 
믹스인 인자 기본값 설정 (:)
css
@mixin flex($justify: center, $align: center) {
    display: flex;
    justify-content: $justify;
    align-items: $align;
}
 
.box {
  @include flex;
}
 
/*	컴파일 결과
.box {
   display: flex;
   justify-content: center;
   align-items: center;
}
 */
믹스인에 내용 추가하기 (@content)
css
@mixin flex($justify: center, $align: center) {
    display: flex;
    justify-content: $justify;
    align-items: $align;
    @content;
}
 
.box {
 @include flex {
   flex-flow: column wrap;
 }
}
 
/* 컴파일 결과
.box {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-flow: column wrap;
}
*/
 
믹스인으로 미디어 쿼리 만들기
css
$tablet: 968px;
$mobile: 568px;
 
 
 
@mixin md-screen {
  @media screen and (min-width: #{$tablet}) {
    @content;
  }
}
 
@mixin sm--screen {
  @media screen and (max-width: #{$mobile}) {
    @content;
  }
}
 
.section {
  padding: 0; 
  @include sm-screen {
    padding: 10
    /* 모바일 화면에서 적용할 스타일들 */
  }
  @include md-screen {
    padding: 15px;
    /* 태블릿 화면에서 적용할 스타일들 */
  }
 }