티스토리 뷰

NextJS

[NextJS] CSS Modules

BIBLIYA 2022. 10. 14. 12:10

NextJS에서 CSS를 효과적으로 사용하는 방법

ex) NavBar.js 만든 곳에 NavBar.module.css 파일을 만들어 준다.

 

NavBar.module.css

.nav {
  display: flex;
  justify-content: space-between;
  background-color: tomato;
}

NavBar.js

import Link from "next/link";
import styles from "./NavBar.module.css";

export default function NavBar() {

  return (
    <nav className={styles.nav}>
      <Link href="/">
        <a>Home</a>
      </Link>
      <Link href="/about">
        <a>About</a>
      </Link>
    </nav>
  );
}

 

Router와 함께 사용 / class 두개 줄 때

import Link from "next/link";
import { useRouter } from "next/router";
import styles from "./NavBar.module.css";

export default function NavBar() {
  const router = useRouter();
  return (
    <nav>
      <Link href="/">
        <a className={`${styles.link} ${router.pathname === "/" ? styles.active : ""}`}>Home</a>
      </Link>
      <Link href="/about">
        <a className={[styles.link, router.pathname === "/about" ? styles.active : ""].join(" ")}>
          About
        </a>
      </Link>
    </nav>
  );
}
.link {
  text-decoration: none;
}

.active {
  color: tomato;
}

'NextJS' 카테고리의 다른 글

[NextJS] _app  (0) 2022.10.14
[NextJS] Styles JSX  (0) 2022.10.14
[NextJS] Routing  (0) 2022.10.14
[NextJS] Pages / Static Pre Rendering  (0) 2022.10.14
[NextJS] 라이브러리(Library) / 프레임워크(framework)  (0) 2022.10.14
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2026/04   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30
글 보관함