결과 화면

 


1. node-sass 설치하기

node-sass 패키지를 설치한다.

설치가 완료되면 packae.json 파일에서 확인할 수 있다.

yarn add node-sass
또는
npm install node-sass

 

2. SCSS 작성하기

  • src/assets/scss/components/header.scss
header {
  width: 100%;

  .inner {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 80px;

    h1 {
      a {
        font-family: 'Cherry Bomb One', cursive;
      }
    }

    nav {
      ul {
        display: flex;
        justify-content: space-between;
        align-items: center;

        li {
          padding-left: 20px;
        }
      }
    }
  }
}

 

  • src/components/layouts/Heaser.tsx

작성한 scss 파일을 상단에 import 시킨다.

import '../../assets/scss/components/header.scss'

const Header = () => {
  return (
    <>
      <header>
        <div className="inner">
          <h1><a href="/">BOB</a></h1>

          <nav>
            <ul>
              <li><a href="/">menu01</a></li>
              <li><a href="/">menu02</a></li>
              <li><a href="/">menu03</a></li>
            </ul>
          </nav>
        </div>
      </header>
    </>
  )
}

export default Header

 

  • src/assets/scss/common.scss

reset.scss, font.scss 등 공통적으로 필요한 scss 파일을 페이지마다 import 하면 번거롭고, 코드도 길어지기 때문에 common.scss를 만들어 한 개의 scss 파일에 작성되도록 한다.

// base
@import './base/reset';
@import './base/font';

// layout
@import './layout/layout';

 

  • src/App.tsx

최종적으로 common.scss를 App 컴포넌트에 import 한다.

import React from 'react'
import Layout from './components/layouts/Layout'
import './assets/scss/common.scss'

const App = () => {
  return (
    <Layout>
      <h1>내용을 작성해 주세요.</h1>
    </Layout>
  )
}

export default App

버전에 맞춰서 강의와 다른 코드가 있습니다.

강의와 별개로 추가한 내용입니다.

 

결과 화면

 


scss를 사용하기 위해 터미널에서 yarn add node-sass 또는 npm install node-sass 명령어를 실행한다.

 

파일 구조

  • src/scss/layout/_layout.scss
@import url('https://fonts.googleapis.com/css2?family=Noto+Sans:wght@400;700&display=swap');
@import url('https://fonts.googleapis.com/css2?family=Noto+Sans+KR:wght@400;700&display=swap');

body {
    background-color: burlywood;
    font-family: 'Noto Sans', 'Noto Sans KR', sans-serif;

    .layout {
        position: relative;
        top: 0;
        left: 0;
        bottom: 0;
        right: 0;
        max-width: 500px;
        width: 90%;
        margin: 50px auto;
        padding: 30px;
        background-color: bisque;

        button {
            min-height: 30px;
            padding: 0 10px;
            background-color: #fff;
        }
    }
}

 

  • src/routes/home.scss
.home {
    h1 {
        margin-bottom: 30px;
        text-align: center;
    }

    form {
        display: flex;
        justify-content: space-between;
        gap: 10px;
        margin-bottom: 10px;

        input {
            width: 100%;
            height: 30px;
            padding: 10px;
        }
    }
}

 

  • src/components/todo.scss
ul {
    li {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 10px;

        &:last-child {
            margin-bottom: 0;
        }
    }
}

 

  • src/routes/detail.scss
.detail {
    h1 {
        margin-bottom: 30px;
        text-align: center;
    }

    .date {
        margin-bottom: 10px;
        text-align: right;
    }
}

+ Recent posts