CSS object-fit: contain; is keeping original image width in layout

CSS object-fit: contain; is keeping original image width in layout

I'm trying to make my images responsive inside some flexbox containers using object-fit: contain, and while the images do resize, the layout seems to be keeping the original image size, causing a scrollbar to appear.

I'm trying to make my images responsive inside some flexbox containers using object-fit: contain, and while the images do resize, the layout seems to be keeping the original image size, causing a scrollbar to appear.

Checking the width of the image using Chrome Dev Tools shows that the width is still 1024 (however the height has been reduced appropriately.)

Am I missing some additional CSS attributes?

JSFiddle: https://jsfiddle.net/w6hgqf18/1/

html,
body {
  margin: 0;
  height: 100%;
}

.page { height: 100%; display: flex; }

.main-container { flex: 1 1 0; display: flex; flex-direction: column; }

.half-containers { flex: 0 1 50%; overflow: auto; box-sizing: border-box; border: 0.5px solid red; display: flex; }

.page-header { flex: 0 0 auto; background-color: #dcdcdc; }

.page-footer { flex: 0 0 auto; background-color: #dcdcdc; }

img { object-fit: contain; } <div class="page"> <div class="main-container"> <div class="page-header"> This is a header </div> <div class="half-containers"> <img src='https://i.imgur.com/tqQvuFr.jpg' /> </div> <div class="half-containers"> <img src='https://i.imgur.com/tqQvuFr.jpg' /> </div> <div class="page-footer"> This is a footer </div> </div> </div>


Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

The Ultimate Guide to Web Development with HTML and CSS

The Ultimate Guide to Web Development with HTML and CSS. Learn Web Development by creating a Blog with HTML and CSS. Make a web applications using HTML5 and CSS3. Create a blog from Scratch.

Animate Any SVG icons with CSS Only | SVG Stroke Animation With Html CSS

Animate Any SVG icons with CSS Only | SVG Stroke Animation With Html CSS

HTML 5 Tutorial: How to create a table using HTML tags and Stylesheet (CSS)

In this article, we want to show you how to create a table using HTML tags and Stylesheet (CSS). HTML table may vary depends on data and style requirements. Sometimes, in the real application, we use an HTML table as a layout of the Email template in HTML format.