Member-only story
Styling in Next.js — CSS, CSS Modules, and styled-components
Next.js is a powerful React framework that makes building web applications easier and faster. When it comes to styling in Next.js, you have multiple options, including traditional CSS, CSS Modules, and styled components. In this post, we’ll explore these methods and help you decide which one suits your project best.
1. Using Global CSS in Next.js
Next.js allows you to import global CSS files directly into your project. These styles apply to the entire application, just like traditional CSS.
How to use Global CSS:
- Create a CSS file (e.g.,
styles/global.css
). - Import the CSS file in
_app.js
or_app.tsx
:
import "../styles/global.css";
function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />;
}
export default MyApp;
3. Now, you can define styles in global.css
, and they will be applied globally.
Pros:
- Simple and easy to use.
- Works well for small projects.
Cons:
- This can cause style conflicts due to global scope.
- Not ideal for large applications with multiple components.