🎨 Styling Components in Next.js v15 Using CSS Modules (Step-by-Step)
As a former 3D Animator with more than 12 years of experience, I have always been fascinated by the intersection of technology and creativity. That's why I recently shifted my career towards MERN stack development and software engineering, where I have been serving since 2021.
With my background in 3D animation, I bring a unique perspective to software development, combining creativity and technical expertise to build innovative and visually engaging applications. I have a passion for learning and staying up-to-date with the latest technologies and best practices, and I enjoy collaborating with cross-functional teams to solve complex problems and create seamless user experiences.
In my current role as a MERN stack developer, I have been responsible for developing and implementing web applications using MongoDB, Express, React, and Node.js. I have also gained experience in Agile development methodologies, version control with Git, and cloud-based deployment using platforms like Heroku and AWS.
I am committed to delivering high-quality work that meets the needs of both clients and end-users, and I am always seeking new challenges and opportunities to grow both personally and professionally.
You’ve built your layout. You’ve created a reusable MainHeader component. But right now... it probably looks a little ugly 😅
Let’s fix that using CSS Modules — a built-in styling method in Next.js v15 that keeps your styles scoped to each component.
🧩 Your Styling Options in Next.js
Next.js gives you multiple ways to style your app:
| Method | Description |
| Global CSS | Styles applied app-wide from globals.css |
| Tailwind CSS | Utility-first CSS framework (popular, but not used in this blog series) |
| CSS Modules | Scoped CSS files for individual components (our focus) |
✅ In this project, we'll use CSS Modules so you can focus on learning Next.js — not memorizing Tailwind classes.
✅ What Are CSS Modules?
CSS written in normal CSS syntax
Filename ends with
.module.cssThe styles are scoped only to the component that imports them
Prevents global style conflicts
Perfect for reusable components like headers, cards, buttons, etc.
🛠️ Step-by-Step: Style Your MainHeader Component
🔹 Step 1: Create the CSS Module File
Inside your /components folder (or wherever your header lives), create:
components/
├── main-header.js
├── main-header.module.css ← ✅ New file
🔹 Step 2: Add Your CSS
Here’s an example of what your main-header.module.css might include:
.header {
background-color: #333;
color: white;
padding: 1rem 2rem;
display: flex;
align-items: center;
justify-content: space-between;
}
.logo {
display: flex;
align-items: center;
gap: 0.5rem;
font-size: 1.25rem;
font-weight: bold;
text-decoration: none;
color: white;
}
.nav {
display: flex;
gap: 1.5rem;
}
.nav a {
text-decoration: none;
color: white;
}
.nav a:hover {
text-decoration: underline;
}
🔹 Step 3: Import and Use the Styles
In your main-header.js file:
'use client';
import Link from 'next/link';
import Image from 'next/image';
import logo from '@/assets/logo.png';
import classes from './main-header.module.css'; // ✅ Importing the CSS module
export default function MainHeader() {
return (
<header className={classes.header}>
<Link href="/" className={classes.logo}>
<Image src={logo.src} alt="A plate with food" width={50} height={50} />
<span>Next Level Food</span>
</Link>
<nav className={classes.nav}>
<Link href="/meals">Browse Meals</Link>
<Link href="/community">Foodies Community</Link>
</nav>
</header>
);
}
🎯 Key Benefits of CSS Modules
| Feature | Why It’s Awesome |
| Scoped styles | No accidental clashes with other CSS |
| Easy to maintain | One file per component |
| Works out-of-the-box | No setup needed in Next.js |
| Simple syntax | Just write regular CSS |
✅ Bonus: You can even use SCSS modules (
.module.scss) if you enable Sass support.
🧠 Global vs CSS Module Styling
| Type | File Example | Scope | Use When... |
| Global CSS | globals.css | App-wide | For resets, fonts, general rules |
| CSS Modules | main-header.module.css | Component only | For specific, reusable components |
🚀 What’s Next?
Now that your header looks great, you're ready to:
Style the rest of your components using CSS modules
Create reusable UI building blocks
Explore responsive styling (media queries, etc.)
📌 Final Thoughts
✅ CSS Modules help you keep styles clean, modular, and bug-free.
✅ They work perfectly in Next.js v15 without any extra setup.
✅ This is a great balance between plain CSS and more advanced frameworks like Tailwind.