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>


html css

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Know Everything About HTML With HTML Experts

HTML Assignment Help Australia @30% OFF from Sample Assignment, with Our Best HTML assignment help experts. Get HTML homework help online at affordable price. 100% Plag free assignment solution.

HTML And CSS Tutorial For Beginners | Complete HTML & CSS Course In One Tutorial

In this video you will learn basics of HTML and CSS from beginning. This HTML and CSS course is best for Beginners to start learning web design and web development. In this HTML and CSS Beginners tutorial we will discuss all the important HTML tags, CSS properties to design the website with some example and explanation. We will also learn about animation using CSS.

Stay at Home Animation in HTML CSS - Covid-19 | SVG to HTML CSS |

Stay at home Animation in html css - COVID-19 . Adobe illustrator SVG To HTML CSS Animation . corona-virus. Adobe illustrator to HTML & CSS Animation.

Hire CSS Developer

Want to develop a website or re-design using CSS Development? We build a website and we implemented CSS successfully if you are planning to **[Hire CSS Developer](https://hourlydeveloper.io/hire-dedicated-css-developer/ "Hire CSS Developer")**...

Transparent Login Form Using HTML and CSS | Login Page in HTML CSS

Transparent Login Form Using HTML and CSS | Login Page in HTML CSS