#how do i import pure css files into nextjs 13 app
1 messages · Page 1 of 1 (latest)
In Next.js, you can import CSS files into your application in a few different ways. Here are some methods based on the information provided in the extracts:
-
Importing CSS files from a JavaScript file: Next.js allows you to import CSS files from a JavaScript file. This is possible because Next.js extends the concept of
importbeyond JavaScript. source (https://nextjs.org/docs/pages/building-your-application/styling/css-modules) -
Using CSS Modules: Next.js has built-in support for CSS Modules using the
.module.cssextension. CSS Modules locally scope CSS by automatically creating a unique class name. This allows you to use the same class name in different files without worrying about collisions. CSS Modules can be imported into any file inside theappdirectory. source (https://nextjs.org/docs/app/building-your-application/styling/css-modules)
Example:
import styles from './styles.module.css'
export default function DashboardLayout({
children,
}: {
children: React.ReactNode
}) {
return <section className={styles.dashboard}>{children}</section>
}
- Importing styles from
node_modules: Since Next.js 9.5.4, importing a CSS file fromnode_modulesis permitted anywhere in your application. For global stylesheets, likebootstrapornprogress, you should import the file insidepages/_app.js. source (https://nextjs.org/docs/pages/building-your-application/styling/css-modules)
Example:
import 'bootstrap/dist/css/bootstrap.css'
export default function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />
}
- Adding a global stylesheet: To add a stylesheet to your application, import the CSS file within
pages/_app.js. These styles will apply to all pages and components in your application. Due to the global nature of stylesheets, and to avoid conflicts, you may only import them insidepages/_app.js. source (https://nextjs.org/docs/pages/building-your-application/styling/css-modules)
Example:
import '../styles.css'
export default function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />
}