.dashboard {
padding: 24px;
}
import styles from './styles.module.css';
export default function DashboardLayout({ children }: {
children: React.ReactNode
}) {
return <section className={styles.dashboard}>{children}</section>;
}
import 'bootstrap/dist/css/bootstrap.css';
export default function RootLayout({ children }: {
children: React.ReactNode;
}) {
return (
<html lang="en">
<body className="container">{children}</body>
</html>
);
}
_app.js에서만 import 가능한
pages 디렉터리와는 다르게 app 디렉터리에서는 필요한곳 어디에서는 글로벌 css를 가져올 수 있음body {
padding: 20px 20px 60px;
max-width: 680px;
margin: 0 auto;
}
import './global.css';
export default function RootLayout({ children }: {
children: React.ReactNode;
}) {
return (
<html lang="en">
<body>{children}</body>
</html>
);
}
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p
/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
"./app/**/*.{js,ts,jsx,tsx,mdx}", // app 디렉터리 추가
"./pages/**/*.{js,ts,jsx,tsx,mdx}",
"./components/**/*.{js,ts,jsx,tsx,mdx}",
// src 디렉토리 사용시
"./src/**/*.{js,ts,jsx,tsx,mdx}",
],
theme: {
extend: {},
},
plugins: [],
}
@tailwind base;
@tailwind components;
@tailwind utilities;
export default function Page() {
return (
<h1 className="text-3xl font-bold underline">
Hello, Next.js!
</h1>
)
}
// app/registry.tsx
'use client';
import React, { useState } from 'react';
import { useServerInsertedHTML } from 'next/navigation';
import { StyleRegistry, createStyleRegistry } from 'styled-jsx';
export default function StyledJsxRegistry({
children,
}: {
children: React.ReactNode;
}) {
const [jsxStyleRegistry] = useState(() => createStyleRegistry());
useServerInsertedHTML(() => {
const styles = jsxStyleRegistry.styles();
jsxStyleRegistry.flush();
return <>{styles}</>;
});
return <StyleRegistry registry={jsxStyleRegistry}>{children}</StyleRegistry>;
}
// app/layout.tsx
import StyledJsxRegistry from './registry';
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html>
<body>
<StyledJsxRegistry>{children}</StyledJsxRegistry>
</body>
</html>
);
}
styled-components
// app/registry.tsx
'use client';
import React, { useState } from 'react';
import { useServerInsertedHTML } from 'next/navigation';
import { ServerStyleSheet, StyleSheetManager } from 'styled-components';
export default function StyledComponentsRegistry({
children,
}: {
children: React.ReactNode;
}) {
// Only create stylesheet once with lazy initial state
// x-ref: https://reactjs.org/docs/hooks-reference.html#lazy-initial-state
const [styledComponentsStyleSheet] = useState(() => new ServerStyleSheet());
useServerInsertedHTML(() => {
const styles = styledComponentsStyleSheet.getStyleElement();
styledComponentsStyleSheet.instance.clearTag();
return <>{styles}</>;
});
if (typeof window !== 'undefined') return <>{children}</>;
return (
<StyleSheetManager sheet={styledComponentsStyleSheet.instance}>
{children}
</StyleSheetManager>
);
}
// app/layout.tsx
import StyledComponentsRegistry from './lib/registry';
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html>
<body>
<StyledComponentsRegistry>{children}</StyledComponentsRegistry>
</body>
</html>
);
}
설치 커맨드
npm install --save-dev sass
Sass 컴파일러 설정이 필요하면 next.config.js의 sassOptions 옵션 사용
const path = require('path');
module.exports = {
sassOptions: {
includePaths: [path.join(__dirname, 'styles')],
},
};
ex)
$primary-color: #64ff00;
:export {
primaryColor: $primary-color;
}
import variables from './variables.module.scss';
export default function Page() {
return <h1 style={{ color: variables.primaryColor }}>Hello, Next.js!</h1>;
}
출처:
https://beta.nextjs.org/docs/styling/css-modules
https://beta.nextjs.org/docs/styling/tailwind-css
https://beta.nextjs.org/docs/styling/global-styles
https://beta.nextjs.org/docs/styling/css-in-js
https://beta.nextjs.org/docs/styling/external-stylesheets
https://beta.nextjs.org/docs/styling/sass