Images are often the largest assets on modern websites, making optimization crucial for performance. Nuxt 3 provides powerful built-in tools for image optimization through the @nuxt/image
module. In this guide, we'll explore how to leverage these features for responsive, efficient images.
Setting Up Image Optimization
First, install the Nuxt Image module:
npm install @nuxt/image
Add it to your nuxt.config.ts
:
export default defineNuxtConfig({ modules: [ '@nuxt/image' ] })
NuxtPicture vs NuxtImg: When to Use What
Nuxt provides two main components for image optimization:
NuxtPicture - For Maximum Optimization
NuxtPicture
generates the <picture>
element with multiple formats and sizes:
<template> <NuxtPicture src="your-image.jpg" sizes="sm:100vw md:900px lg:1200px 2xl:1600px" :img-attrs="{ alt: 'Hero mountains at sunrise', class: 'hero' }" densities="1,2" :widths="[320,640,960,1200,1600]" format="avif,webp,jpeg" placeholder="blur" /> </template>
This generates:
- Multiple formats: AVIF (best compression), WebP (good support), JPEG (fallback)
- Responsive images: Different sizes for different screen widths
- Retina support: 1x and 2x pixel densitiesw
- Blur placeholder: Shows while loading
NuxtImg - For Simpler Use Cases
NuxtImg
is perfect when you need basic optimization:
<template> <NuxtImg src="your-image.jpg" sizes="sm:100vw md:800px lg:1200px" :modifiers="{ fit: 'cover', width: 1200, height: 800, quality: 80 }" alt="Sample image" placeholder="blur" class="responsive-img" /> </template>
Key Features Explained
1. Format Optimization
format="avif,webp,jpeg"
- AVIF: Next-gen format with 50% better compression than JPEG
- WebP: 25-35% smaller than JPEG with wide browser support
- JPEG: Universal fallback
2. Responsive Breakpoints
sizes="sm:100vw md:900px lg:1200px 2xl:1600px" :widths="[320,640,960,1200,1600]"
This creates optimized images for each screen size, reducing bandwidth on mobile devices.
3. Pixel Density Support
densities="1,2"
Serves high-DPI images for retina displays while keeping standard resolution for regular screens.
4. Smart Placeholders
placeholder="blur"
Shows a blurred version while the actual image loads, improving perceived performance.
Performance Benefits
Using Nuxt's image optimization provides:
- Faster Load Times: Smaller file sizes mean quicker downloads
- Better Core Web Vitals: Optimized images improve LCP (Largest Contentful Paint)
- Reduced Bandwidth: Especially important for mobile users
- Automatic Format Selection: Browsers automatically choose the best supported format
Best Practices
- Always specify alt text for accessibility
- Use appropriate sizes - don't serve desktop images to mobile
- Choose the right component:
-
NuxtPicture
for hero images and important visuals -
NuxtImg
for content images and simpler use cases
-
- Set explicit dimensions when possible to prevent layout shift
- Use placeholders for better user experience
Styling Your Images
.hero { display: block; width: 100%; height: auto; border-radius: 12px; } .responsive-img { max-width: 100%; height: auto; }
Conclusion
Nuxt 3's image optimization features make it easy to serve performant, responsive images without complex setup. By using NuxtPicture
for critical images and NuxtImg
for content, you can significantly improve your site's performance and user experience.
The automatic format selection, responsive breakpoints, and smart loading features help create fast, modern web applications that work well across all devices and connection speeds.
Top comments (0)