# <Image> (Advanced)

> Fluid responsive images using the fill prop.

Subject: Next.js | Lesson 49 of 65 | 3 min read
Interactive lesson: https://codewithanimation.in/nextjs/image-advanced
Previous: [<Image> (Remote)](https://codewithanimation.in/nextjs/image-remote.md) | Next: [<Font>](https://codewithanimation.in/nextjs/font-optimization.md)

## The Fill Prop

If you don't know the dimensions of an image (e.g., in a fluid, responsive CSS Grid), you can bypass the `width` and `height` requirements by using the `fill` prop.

When `fill` is true, Next.js injects CSS to make the image automatically expand to fill its parent container.

## The Absolute Positioning Trap

The `fill` prop works by applying `position: absolute` and `height: 100%, width: 100%` to the image.

In CSS, absolute elements expand until they hit a relative parent.

If you forget to add `position: relative` to the parent `<div>`, the image will expand past your layout and cover the entire browser window.

## Implementation

1. Add `position: relative` to the parent.

2. Add `<Image fill src="..." />`.

3. Add `className="object-cover"` so the image maintains its aspect ratio instead of squishing and distort.

4. Add the `sizes` prop (e.g. `sizes="(max-width: 768px) 100vw, 50vw"`) to prevent Next.js from downloading unnecessarily massive images on mobile devices.

## Common mistake: The Missing Sizes Warning

If you use `fill` without providing a `sizes` prop, Next.js will issue a loud warning in development.

Because the image is fluid, Next.js doesn't know what sizes to generate, so it defaults to serving the largest possible resolution (which ruins performance on mobile).

## Try it: Fill vs Explicit Dimensions

Examine the syntax difference between a hardcoded, explicitly dimensioned image and a fluid, responsive image using the fill prop.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/nextjs/image-advanced

## Check yourself

### Question 1

What CSS property is absolutely required on the parent container when using the Next.js `<Image fill />` prop?

- A. display: flex
- B. overflow: hidden
- C. position: relative
- D. object-fit: cover

Correct answer: C

The `fill` prop applies `position: absolute` to the image. For it to correctly fill its parent (instead of the whole screen), the parent MUST have `position: relative`.

- Why not A: Layout modes don't constrain absolute positioning.
- Why not B: While helpful, it doesn't constrain the element's actual bounds.
- Why not D: This goes on the image itself, not the parent.

## Remember this

- Use `fill` when you don't know the exact dimensions.
- The parent container MUST have `position: relative`.
- Apply `object-cover` to prevent the image from distorting.
- Always provide a `sizes` prop when using `fill` to optimize mobile downloads.
