Kritim Yantra
Aug 25, 2025
You know that sinking feeling when you’re super excited about deploying your shiny new project, only to find that a broken link throws users straight into the ugly default 404 error page? 😬
I still remember my first Next.js project—everything was smooth until a client clicked a wrong link and was greeted with that plain, lifeless “Page Not Found” message. Their first reaction? “Is something broken?”
That’s when I realized: a custom 404 page isn’t just about fixing an error, it’s about keeping users engaged even when they wander off the right path.
So today, let’s learn how to create a clean, branded, and user-friendly custom 404 page in Next.js—step by step. Trust me, it’s easier than you think! 🚀
Before we jump into code, let’s quickly address the “why.”
A custom 404 page:
Think of it like putting up a friendly “Detour” sign instead of just leaving people stranded on a dead-end road.
404.js
FileIn Next.js, adding a custom 404 page is as simple as creating a file named 404.js
inside the pages
folder.
👉 File structure:
my-next-app/
┣ pages/
┃ ┣ index.js
┃ ┣ about.js
┃ ┗ 404.js 👈 our custom page
Here’s the simplest version you can start with:
// pages/404.js
import Link from "next/link";
export default function Custom404() {
return (
<div style={{ textAlign: "center", marginTop: "50px" }}>
<h1>404 - Page Not Found 😢</h1>
<p>Oops! The page you’re looking for doesn’t exist.</p>
<Link href="/">
<a style={{ color: "blue", textDecoration: "underline" }}>Go Back Home</a>
</Link>
</div>
);
}
What’s happening here?
Let’s make our 404 page look like it belongs to your app. You can add some CSS or Tailwind styles.
Example with Tailwind CSS:
// pages/404.js
import Link from "next/link";
export default function Custom404() {
return (
<div className="flex flex-col items-center justify-center h-screen text-center">
<h1 className="text-6xl font-bold text-red-600">404</h1>
<p className="mt-4 text-lg">Oops! Page not found.</p>
<Link href="/">
<a className="mt-6 px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700">
Back to Home
</a>
</Link>
</div>
);
}
Now instead of a boring white screen, you’ve got a polished, branded error page.
Want to make your 404 page fun? Here are a few ideas:
Remember, even errors can create a positive experience.
If you want a catch-all error page (for things like server errors too), you can create a _error.js
file in the pages
directory. But for most use cases, a custom 404.js
is enough.
We just built a custom 404 page in Next.js by:
404.js
file inside the pages
folder.Q1: Do I always need a 404.js file in Next.js?
👉 Not really. Next.js provides a default 404 page. But adding your own makes your site feel more professional.
Q2: Can I add animations or GIFs to my 404 page?
👉 Absolutely! You can use CSS animations, Lottie files, or even a meme GIF to make it more fun.
Q3: What’s the difference between 404.js
and _error.js
?
👉 404.js
is just for “Page Not Found” errors, while _error.js
handles all errors (like 500 server errors).
Creating a custom 404 page in Next.js takes just a few minutes, but it leaves a lasting impression on your users. Instead of making them feel lost, you’re guiding them back with style.
So go ahead—build one today, add a personal touch, and make sure your visitors never feel abandoned again.
Have you ever seen a really cool 404 page that stuck in your memory? Drop it in the comments—I’d love to check it out!
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google