๐Ÿค ์€์ง€log ๐Ÿค

[NextJS] NextJS_CSS ๋ณธ๋ฌธ

๐ŸŒท Next JS

[NextJS] NextJS_CSS

Eun_zii 2022. 10. 6. 14:29
 

Next JS

์„ค์น˜ npm create-next-app . / npm create-next-app —typescript . โœ๏ธ * name can no longer contain capital letters ๋ผ๋Š” ์—๋Ÿฌ๊ฐ€ ๋œจ๋ฉด ํด๋”๋ช…์— " ๋Œ€๋ฌธ์ž " ๊ฐ€ ์žˆ์–ด์„œ ๋œจ๋Š”๊ฒƒ์ด๋ฏ€๋กœ " ์†Œ๋ฌธ์ž " ๋กœ ๋ณ€๊ฒฝ Next..

eun-jii.tistory.com

 

.module.css ํŒŒ์ผ ์ƒ์„ฑ

CSS ๋ฅผ ์ ์šฉํ•  ํŒŒ์ผ๊ณผ ๊ฐ™์€ ํด๋”์— ํŒŒ์ผ๋ช….module.css ํŒŒ์ผ ์ƒ์„ฑ
ex) NavBar.js
      NavBar.module.css

 

.active {
  color: tomato;
}
.link {
  text-decoration: none;
}

์ฝ”๋“œ๋ฅผ css์— ์ ๊ณ 

 

import styles from "./NavBar.module.css"; ๐Ÿ“import ํ•ด์ฃผ๊ธฐ

				<a
          className={`${styles.link} ${ 
            router.pathname === "/" ? styles.active : ""
          }`} ๐Ÿ“ ๋ฐฑํ‹ฑ ์‚ฌ์šฉ
        >
          Home
        </a>

				<a
	          className={[ ๐Ÿ“ ๋ฐฐ์—ด ์‚ฌ์šฉ
            styles.link,
            router.pathname === "/about" ? styles.active : "",
          ].join(" ")} ๐Ÿ“ join์€ ๋ฐฐ์—ด๋‚ด์— ๊ฒƒ๋“ค์„ ํ•ฉ์ณ์คŒ
        >
          About
        </a>

๐Ÿ“Œ ๋ฐฑํ‹ฑ์œผ๋กœ ์—ฌ๋Ÿฌ๊ฐœ์˜ ์Šคํƒ€์ผ์„ ์ฃผ๋Š”๋ฐฉ๋ฒ•๊ณผ ๋ฐฐ์—ด๋กœ ์ฃผ๋Š” ๋ฐฉ๋ฒ• 2๊ฐ€์ง€๊ฐ€ ์กด์žฌ.

.์ปดํฌ๋„ŒํŠธ ์ด๋ฆ„ {CSS ์ž‘์„ฑ} ์ ์šฉํ•  ํŒŒ์ผ์— ์—ฐ๊ฒฐ

 

< style jsx>< /style>

module.css ์™€ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ css๋ฅผ ์—ฐ๋™ํ•˜๋Š”๋ฐฉ๋ฒ•

 

<nav>
      <Link href="/">
        <a className={router.pathname === "/" ? "active" : ""}>Home</a>
      </Link>
      <Link href="/about">
        <a className={router.pathname === "/about" ? "active" : ""}>About</a>
      </Link>
      <style jsx>{`
        nav {
          background-color: tomato;
        }
        a {
          text-decoration: none;
        }
        .active {
          color: white;
        }
      `}</style>
    </nav>

 

  • ์‚ฌ์šฉํ•  ์ปดํฌ๋„ŒํŠธ ์•ˆ์—์„œ style jsx๋ฅผ ์—ด์–ด์ฃผ๊ณ  css๋ฅผ ์ ๊ธฐ๋งŒํ•˜๋ฉด ๋๋‚œ๋‹ค.
    โžž import ํ•˜์ง€ ์•Š์•„๋„๋˜๋Š” ์žฅ์ ๊ณผ ํด๋ž˜์Šค๋„ค์ž„์„ ์ผ์ผํžˆ ๊ธฐ์–ตํ•  ํ•„์š”๊ฐ€ ์—†๋Š” ์žฅ์ ์ด ์žˆ์Œ.

 

  • module.css์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋‹ค๋ฅธ ์ปดํฌ๋„ŒํŠธ์—์„œ ๊ฐ™์€ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•ด๋„ ๊ฒน์น˜์ง€ ์•Š๋Š”๋‹ค.
    โžž ๋ฌด์ž‘์œ„๋กœ ์ถ”๊ฐ€์ ์ธ ํด๋ž˜์Šค๋„ค์ž„์ด ์ƒ์„ฑ๋˜๊ธฐ๋•Œ๋ฌธ → ๋ณ€์ˆ˜์ด๋ฆ„์ง“๋Š”๋ฐ ์ž์œ ๋กœ์›Œ์ง€๋ฉฐ ์ถฉ๋Œ์„ ํ”ผํ•  ์ˆ˜ ์žˆ๋Š” ์žฅ์ ์ด์žˆ๋‹ค.

 

Globals Style ์™€ Custom App

 

NextJS์—์„œ globalsStyle์„ ์ ์šฉํ•˜๋ ค๋ฉด appComponent๋ฅผ customํ•ด์ค˜์•ผ ํ•œ๋‹ค.

โญ๏ธ _app.js ํŒŒ์ผ์„ ์ˆ˜์ •ํ•˜๋ฉด ์„œ๋ฒ„๋ฅผ ์žฌ์‹œ์ž‘ ํ•ด์•ผํ•จ โญ๏ธ

 

๊ธฐ๋ณธ์ ์œผ๋กœ ์ˆจ์–ด์ ธ์žˆ๋Š” app์„ ์ˆ˜์ •ํ•˜๋ ค๋ฉด

 

1. pages์— _app.js๋ฅผ ๋งŒ๋“ค์–ด์•ผํ•œ๋‹ค.

 

 

2. _app.js์—์„œ App component๋ฅผ ์ƒ์„ฑํ•˜๊ณ , Component์™€ pageProps๋ฅผ parameter๋กœ ๋ฐ›์€ ํ›„ ๋ถˆ๋Ÿฌ์™€์ค€๋‹ค.

export default function App({ Component, pageProps }) {
  return (
    <>
      <Component {...pageProps} />
    </>
  );
}

 

3. ๊ทธํ›„ stylesํด๋”์•ˆ์˜ globals.css ๋ฅผ ๋ถˆ๋Ÿฌ์˜จ๋‹ค. (module์ด ์•„๋‹Œ css๋Š” ์ปค์Šคํ…€app์—์„œ๋งŒ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ์Œ)

import "../styles/globals.css"; ๐Ÿ“Œ
export default function App({ Component, pageProps }) {
  return (
    <>
      <Component {...pageProps} />
    </>
  );
}

4. ์—ฌ๋Ÿฌ ํŽ˜์ด์ง€์— ๊ณตํ†ต์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” navBar๋‚˜ footer๋ฅผ ์ž„ํฌํŠธํ•ด์ค€๋‹ค.

import NavBar from "../components/NavBar";
import "../styles/globals.css";
export default function App({ Component, pageProps }) {
  return (
    <>
      <NavBar />
      <Component {...pageProps} />
    </>
  );
}

5. style jsx global ์„ ํ†ตํ•ด ์ถ”๊ฐ€๋กœ ๊ธ€๋กœ๋ฒŒ์Šคํƒ€์ผ์„ ์ฃผ๊ฑฐ๋‚˜, globals.css๋ฅผ ์ˆ˜์ •ํ•˜์—ฌ ๊ธ€๋กœ๋ฒŒ ์Šคํƒ€์ผ์„ ์ค„ ์ˆ˜ ์žˆ๋‹ค.

import NavBar from "../components/NavBar";
import "../styles/globals.css";
export default function App({ Component, pageProps }) {
  return (
    <>
      <NavBar />
      <Component {...pageProps} />
      <style jsx global>
        {`
          * {
            font-size: 18px;
          }
        `}
      </style>
    </>
  );
}

 

728x90