Skip to content

Next.js Image Component: Full Height Responsive Images

Learn how to make Next.js Image components fill container heights while maintaining aspect ratios.

Problem: Next.js Image Height Limitations

The Next.js Image component requires explicit dimensions for optimal performance, but this creates challenges when you need images to fill container heights responsively. When using layout="fill", developers often encounter the error:

bash
Error: Image with src "/deco.svg" must use "width" and "height" properties or "unsized" property.

This occurs because while layout="fill" is designed to fill parent containers, Next.js still needs intrinsic dimensions for proper aspect ratio calculation and optimization.

Modern Solutions (Next.js 13+)

Method 1: Using the fill Property with Parent Container

The most reliable approach uses the fill property with a positioned parent container:

jsx
<div className="relative h-full w-full">
  <Image
    src="/deco.svg"
    alt="Decoration"
    fill
    className="object-cover"
  />
</div>

Key requirements:

  • Parent container must have position: relative
  • Parent must have defined dimensions (height/width)
  • Use object-fit CSS property to control image scaling
css
.container {
  position: relative;
  width: 100%;
  height: 300px;
}
jsx
<div className="container">
  <Image
    src="/deco.svg"
    alt="Decoration"
    fill
    style={{ objectFit: 'cover' }}
  />
</div>

Method 2: Using Width/Height with Responsive Sizing

For more control over responsive behavior:

jsx
<Image
  src="/deco.svg"
  alt="Decoration"
  width={0}
  height={0}
  sizes="100vw"
  style={{ width: '100%', height: 'auto' }}
/>

This approach:

  • Sets intrinsic dimensions to zero (bypassing requirements)
  • Uses sizes="100vw" for responsive width scaling
  • Applies custom styles for final dimensions

WARNING

While setting width={0} and height={0} works, it may not provide optimal performance benefits since Next.js cannot precalculate aspect ratios.

Method 3: Tailwind CSS Integration

Combine Next.js Image with Tailwind CSS for streamlined styling:

jsx
<div className="relative size-64">
  <Image
    src="/deco.svg"
    alt="Decoration"
    fill
    className="object-contain"
  />
</div>

Common object-fit values:

  • object-contain: Scale to fit while preserving aspect ratio
  • object-cover: Fill container while preserving aspect ratio (may crop)
  • object-fill: Stretch to fill container (may distort)

Legacy Approaches (Next.js 12 and earlier)

For older Next.js versions, different properties were available:

jsx
// Next.js 12 and earlier
<Image
  src="/deco.svg"
  alt="Decoration"
  layout="fill"
  objectFit="cover"
/>

Deprecated Features

The layout and objectFit props are deprecated in Next.js 13+. Migrate to the newer fill property and CSS-based solutions.

Advanced Techniques

Responsive Images with Aspect Ratios

Maintain aspect ratios while being responsive:

jsx
<div style={{ 
  position: 'relative', 
  width: '100%', 
  paddingBottom: '56.25%' /* 16:9 aspect ratio */
}}>
  <Image
    src="/deco.svg"
    alt="Decoration"
    fill
    style={{ objectFit: 'cover' }}
  />
</div>

External Images

When using images from external sources:

jsx
<div className="relative h-64 w-full">
  <Image
    src="https://example.com/image.jpg"
    alt="External image"
    fill
    className="object-cover"
  />
</div>

WARNING

For external images, ensure you've configured the next.config.js to allow the domain:

js
module.exports = {
  images: {
    domains: ['example.com'],
  },
}

Common Issues and Solutions

1. Image Not Filling Container

Problem: Image doesn't expand to fill parent Solution: Ensure parent has position: relative and defined dimensions

2. Aspect Ratio Distortion

Problem: Image appears stretched or squashed Solution: Use appropriate object-fit value (contain, cover, etc.)

3. Layout Shift

Problem: Content jumps as images load Solution: Always provide dimensions or use fill with sized containers

4. Console Warnings

Problem: Browser warnings about image dimensions Solution: Provide meaningful width/height values when not using fill

Performance Considerations

  • Use the fill property for responsive images that adapt to container sizes
  • Prefer local images over external URLs when possible
  • Utilize the sizes attribute for responsive breakpoints
  • Consider using the priority prop for above-the-fold images

Conclusion

The Next.js Image component provides powerful optimization features but requires careful dimension management. For full-height images:

  1. Use fill with positioned parent containers for most responsive scenarios
  2. Apply appropriate object-fit CSS to control image scaling behavior
  3. Always provide container dimensions to prevent layout shifts
  4. Migrate from deprecated layout prop to modern fill approach

By following these patterns, you can create responsive, optimized images that perfectly fill their containers while maintaining performance benefits.