How to keep image the same size (and crop the excess) inside a flexible div?

css crop image top and bottom
crop image css
css image size scale to fit
crop image css responsive
resize image css
object-fit
css crop image to square
crop image to div

I have an image inside a flexible div and I want the image to stay the same size no matter if the div is being resized, the image should just be hidden (centering it would also be a great add-on). How to achieve this?

Below is my current code, this causes the image to shrink when the div is being resized. Thank you!

.banner-img {
  max-width: 30%;
  float: left;
  overflow: hidden;
  border: 1px solid red;
}

.banner-img img {
  width: 250px;
  height: 100px;
}
<div class="banner-img"><img src="http://via.placeholder.com/250x100" alt="" /></div>

Flex is a good solution to center your image

.banner-img {
  max-width: 30%;
  height: auto;
  overflow: hidden;
  display: flex;
  justify-content: center;
  border: 1px solid red;
}

.banner-img img {
  width: 250px;
  height: auto;
}
<div class="banner-img"><img src="http://via.placeholder.com/250x100" alt="" /></div>

Resize image proportionally with CSS, How do I resize an image to fit in a div?

You can achive this with making an image as background and align the background position to center:

.banner-img {
  width: 250px;
  max-width: 30%;
  height: 100px;
  background: url("http://via.placeholder.com/250x100") center no-repeat;
}
<div class="banner-img"></div>

Changing image sizes proportionally using CSS?, How do you make all images the same size in HTML?

In .banner-img img, you can add object-fit: cover and change the width: 250px from pixels to percentage, like this:

.banner-img {
    max-width:30%;
    float:left;
    overflow:hidden;
}

.banner-img img {
    width: 100%;
    height: 100px;
    object-fit: cover;
}
<div class="banner-img"><img src="http://via.placeholder.com/250x100" alt="" /></div>

Cropping Images in CSS With object-fit ← Alligator.io, How do I reduce the size of an image in HTML?

Simple Responsive Images With CSS Background Images , If the container has a specified dimension (in my example the width), when telling You need to fix one side ( eg height ) and set the other to auto . which would crop out anything that would otherwise exceed its size. This will center your image inside a 120x120 div chopping off any excess of the image.

Hiding and revealing portions of images, The object-fit property is a welcome addition that allows to crop images so but while keeping its aspect ratio and avoid having a squished image? The image is not resized at all, and the original image size fills the given area. Now we have a problem if we need the image to be the same height, but fit in half the width.

Fully Responsive Images, Editor's Note: This article features just one of the many, suboptimal solutions On an iPhone, a 290 × 183 pixel image that is 18 KB in size looks identical to This excess is not just academic; it has a substantial effect on the website's load time. Place an image in the empty <div> or <span> by setting its 

Comments
  • Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking as the code you have provided does not demonstrate the issue.
  • You want your banner-img to have a particular height?
  • OK, i think I got it. I have a max-width:100% rule in my css which is getting in the way, so applying also a max-width:250px to the image solves the issue.
  • If you feel this solved the problem, please mark the answer as correct. Thank you.