When working with images in Next.js, you may have noticed that large images can cause a flicker or empty space before they fully load. To improve the user experience, Next.js gives you a built-in option called blur placeholder. This feature shows a small blurred preview of the image until the full version loads.
In this post, we’ll go step by step on how you can implement it.
Why Blur Placeholder?
Blur placeholders help create a smoother visual experience. Instead of showing a blank space or loader, users see a preview that quickly fades into the full image once it’s loaded. This technique is widely used by platforms like Medium and Unsplash.
Using next/image with Blur
Next.js makes this simple using the <Image /> component. All you need is a blurDataURL, which is a base64 string that represents a small blurred version of your image.
Here’s a simple example:
1import Image from "next/image";
2
3export default function BlogImage() {
4return (
5<Image
6src="/my-photo.jpg"
7alt="My photo"
8width={800}
9height={600}
10placeholder="blur"
11blurDataURL="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD..."
12/>
13);
14}In the snippet above:
placeholder="blur"tells Next.js to use the blur effect.blurDataURLholds the small blurred version of the image in base64 format.
How to Generate Blur Data URL
You could generate the base64 manually with tools like sharp in Node.js, but the easiest way is to use blurred.dev.
Steps:
- Go to blurred.dev.
- Upload your image.
- Choose the image format (JPEG/PNG/WebP) and adjust the quality if you want.
- Copy the Data URL (base64) that is generated.
- Paste that string into your
blurDataURLproperty in your Next.js<Image />.
Here’s what the generated value looks like:
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/...
Final Example
1< Image
2src = "/cover.jpg"
3alt = "Cover image"
4width = {
5 1200
6}
7height = {
8 600
9}
10placeholder = "blur"
11blurDataURL = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD..." /
12 >And that’s it. Now your image will show a smooth blurred preview before loading in full quality.
Written by
Abhinav Yadav