Assets & Styling 🎨
Next.js treats styling as a buffet—choose the approach that fits each component while leaning on framework optimizations.
CSS Modules 🧩
- Create
Component.module.cssalongside your component.
import styles from './Card.module.css';
export function Card() {
return <div className={styles.card}>Hello</div>;
}- Class names are locally scoped, preventing collisions.
Global CSS 🌍
- Import once in
app/layout.tsx(App Router) or_app.tsx(Pages Router).
import './globals.css';- Use for resets, typography, or utilities.
Tailwind CSS 🌬️
- Install via
npx tailwindcss init -p. - Configure
contentpaths to includeapp/**/*.{ts,tsx}. - Utility-first classes speed up prototyping; pair with
clsxfor conditional styling.
Sass / SCSS 🧶
- Supported out of the box:
import styles from './Card.module.scss';. - Enjoy nesting, variables, mixins.
Styled-components / Emotion 💅
- Supported via SWC transforms. Update
next.config.js:
const nextConfig = {
compiler: { styledComponents: true },
};
module.exports = nextConfig;- Great for theme-based design systems.
Fonts (next/font) 🔤
import { Inter } from 'next/font/google';
const inter = Inter({ subsets: ['latin'], variable: '--font-inter' });
export default function Layout({ children }) {
return <html className={inter.variable}>{children}</html>;
}- Self-hosts fonts automatically, no layout shift.
- Supports local fonts via
next/font/local.
Images (next/image) 🖼️
import Image from 'next/image';
<Image src="/hero.png" alt="Hero" width={1200} height={600} priority />- Automatic responsive sizing, lazy loading, AVIF/WebP.
- Configure remote domains in
next.config.js.
Static Assets (/public) 📁
- Place files in
public/and reference via/logo.svg. - Ideal for favicons, robots.txt, downloadable files.
Styling Tips ✅
- Mix and match: global CSS for tokens, modules for components, Tailwind for utilities.
- Co-locate styles with components to improve maintenance.
- Use CSS variables for theming (works across CSS Modules + Tailwind).
- Keep static assets optimized; rely on
next/imagefor responsive variants.
Analogy: Next.js styling is a wardrobe with labeled drawers—pick the outfit (approach) that suits the occasion while keeping everything organized.
Invest a few minutes setting conventions early—it pays dividends as the app grows. 💅
Last updated on