Next.js Discord

Discord Forum

How can I make an image responsive and fill the remaining space in a container

Unanswered
codecret posted this in #help-forum
Open in Discord
I'm working on a Next.js component and want to ensure that an image fills the remaining space in a container while maintaining responsiveness. I would also like to leave some space below the image. Here's my current setup:

i guess it can't be responsive if i putted fixed values right?


import Image from "next/image";

export default function Home() {
  return (
    <div>
      <section className="max-h-screen overflow-hidden">
        <Navbar />
        <Filters />
        <div className="relative aspect-[886/1372]">
          <Image src={"/cover1.webp"} alt="cover" fill objectFit="cover" />
        </div>
      </section>
    </div>
  );

15 Replies

is layout deprected?
Yes, it is
@codecret what you do is make a div, do all your styling on that
Like leaving the space
Fixed values
Breakpoints
And then put your image inside it with the fill prop.
To help with your image optimizations, you can also use the sizes prop to detail what breakpoints and diff sizes the image will take
@Arinji And then put your image inside it with the fill prop.
so if i want to let the image take the whole height should i measure the height betewen the header and the bottom of the page and crop the image?
will that even be responsive
i thought that fixed values for images are not good
@codecret so if i want to let the image take the whole height should i measure the height betewen the header and the bottom of the page and crop the image?
Uh, yes.. this shld be like normal css. Like it's upto you, not nextjs specific


Also don't crop the image, use object-cover on the image. It will like extend the image while maintaining aspect ratio
@codecret did it work?