Fitting an Image into a Container

Hi, I’m trying to fit an image into a flex div (and lightbox) but when I try to maximize its size within the containing div, it always overflows. I experimented with height and width constraints but nothing works. How can I maximize its size while keeping aspect ratio and everything contained in container? Thanks

Here is my site Read-Only: [LINK](Webflow - High Vibes)
(how to share your site Read-Only link)


To maximize the size of an image within a flex div while keeping its aspect ratio and containing everything within the container, you can use the following CSS:

Make sure to replace .flex-container and .img-container with your actual class names. This CSS will help you achieve a responsive image within a flex div while maintaining its aspect ratio.

If you need further assistance, feel free to ask!

Best regards,

Sofia Choudhry

You want the element wrapping the image to have a height, your lightbox is auto, meaning the image inside is gonna determine the size, so set the lightbox to 100% height to fix that, then the image to 100% width and height and then use Fit, I’d say contain, so nothing’s cut off.

Thanks for your help. I’m almost there! Everything looks good except for some reason all the images are positioned left of center when I want them to be centered. I’ve set all parent divs to center flex so I can’t figure out the issue. Note: I can’t set both height and width of the image to 100% because it stretches the image. I can only set height to 100% and width the auto.

@rolomathias best practise would be to wrap the image in the slide and have flex on the wrapper set to center and make sure the wrapper is 100% width and height. Or like is said 100% on the image, width and height and have object fit set to contain like I said above so it doesn’t stretch.

When you set an image’s object-fit property to contain , you’re instructing the browser to scale the image so that both its width and its height can fit inside the container without cropping the image. This means the entire image will be visible, but it might not cover the entire container if the image’s aspect ratio (the ratio of its width to its height) is different from the container’s aspect ratio.

The object-fit CSS property specifies how the content of a replaced element, such as an <img> or <video>, should be resized to fit its container. Here’s a brief overview of each object-fit option:

1. fill

  • Description: This is the default value. It stretches the content to fit the container’s height and width, potentially distorting the content’s original aspect ratio.
  • Use Case: When you need the content to completely cover the container, regardless of its original aspect ratio.

2. contain

  • Description: Scales the content to maintain its aspect ratio while fitting within the container’s dimensions. The entire object will be visible, but this can result in empty space in the container.
  • Use Case: Useful when preserving the original aspect ratio of the content is important, and you want to ensure that the entire object is visible without cropping.

3. cover

  • Description: Scales the content to maintain its aspect ratio while filling the entirety of the container. Parts of the content may be clipped to ensure no empty space in the container.
  • Use Case: Ideal for background images or when the content needs to cover the entire container, and cropping is acceptable to avoid empty spaces.

4. none

  • Description: The content will not be resized, displaying at its original size regardless of the container.
  • Use Case: When you need to display the content in its true size, without scaling, even if it doesn’t fit the container.

5. scale-down

  • Description: Compares the effects of none and contain to display the content at its original size unless it doesn’t fit inside the container, in which case it behaves like contain.
  • Use Case: Useful when you want to display the content at its original size but scale it down to fit the container if necessary, choosing the smaller of the two options.
1 Like