What is the optimal method for increasing the height of an image up to its maximum height?

I have a unique container designed to showcase images with a fixed width of 100%. Here are my specific requirements:

  • The image must maintain its original aspect ratio.
  • If the image is wider, the width should be 100% and the height less than the designated max-height.
  • If the image is taller, the height should be at the max-height limit while the width is less than 100%.

In my scenario, the first two images display correctly. However, the third one poses an issue as either the width does not reach 100% or the height exceeds the max height limit.

How can I rectify this situation?

.container {
  border: 1px solid black;
  height: 200px;
  width: 100%;
}

img {
  max-width: 100%;
  max-height: 200px;
  width: auto;
  display: block;
  margin: auto;
}
<div class="container">
  <img src="https://i.sstatic.net/kZPXw.png" />
</div>

<div class="container">
  <img src="https://i.sstatic.net/IfTIL.png" />
</div>

<div class="container">
  <img src="https://www.gravatar.com/avatar/62345e20906dbccbf7e18f11ab4bd047?s=64&d=identicon&r=PG&f=1" />
</div>

Feel free to experiment further here: https://jsfiddle.net/d425c1bh/2/

Answer №1

For a solution, try adding the CSS property object-fit and change the width from auto to 100%

.container {
  border: 1px solid black;
  height: 200px;
  width: 100%;
}

img {
  max-height: 200px;
  width: 100%;
  max-width: 100%;
  object-fit: contain;
  display: block;
  margin: auto;
}

To see it in action, check out this JSFiddle link

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Adjusting ArrowHelper Width in Three.jsLooking to change the size of an Arrow

I have been attempting to adjust the width of pink lines on this jsfiddle link: animation of object In my code, I am defining local basis vectors as follows: // Defining local vector axis of plane var thetadir = new THREE.Vector3(1, 0, 0); var phidir = ...

What is the best way to keep a dropdown list menu next to the selected dropdown button as the user scrolls?

I recently encountered a problem similar to the one on this page within my App. Upon selecting the drop-down list, the corresponding menu opens as expected. However, when the user scrolls down the page, the drop-down menu remains in its original position. ...

Using JSON data to populate the jQuery UI Datepicker

Is it possible to populate this jQuery UI datepicker calendar with data from a JSON file containing all non-working days for the years 2017 and 2018? P.S. return [!(month == 8 && day == 27), 'highlight', highlight]; - This example demons ...

creating tabulations and charts across various while loops

My goal is to create Tabs using information from the first while loop, and then populate a table within each Tab using data from the second while loop. The data is fetched date-wise from my 'treatment' table to generate the Tabs. The line items ...

Bind ng-model with button click in AngularJS

I am encountering a frustrating issue with data binding utilizing ng-model and button. The functionality of my website is as follows: The HTML site showcases a list of projects (imported from an external .json file). Each row contains an Edit button tha ...

Commence {{@index}} at 1 instead of 0 in Handlebars, without any template specified

Currently, I am utilizing the @each loop to go through items but would like to start with an index of 1 instead of 0. I attempted to resolve this by including the following code snippet: var Handlebars = require('handlebars'); Handlebars.regist ...

What is the best way to make Bootstrap 3 move to a new line when there is not enough space?

I am facing an issue with a lengthy text inside a div with a class of "col-md-3". The text is so long that it appears to be overlapping with the content of another div. Adding spaces is not an option as it is an email address, and it displays fine on some ...

Styling Issues with Angular Code within App-Root Element

I am encountering an issue with my Angular 7 application during initialization. I have a class named "testing" that simply changes the text color to red. I tried placing the class in the index.html file within a style tag, as well as in the styles.scss fil ...

Bespoke HTML helper dropdown selection box

Here's a snippet I have: @Html.DropDownList("consultations", Model.ConsultationsTruncated, new { @class = "form-control", @id = "consultations" }) The displayed texts are truncated: For instance string s = "Hello everybody" ==> Text ="Hel..." ...

What is the best way to use two distinct CSS styles for mat-form-field across multiple pages?

On one of my pages, I have a mat-form-field: <mat-form-field class="form-control-full-width"> <input type="text" matInput placeholder="First Name" formControlName="firstNameFC" required> <mat-error *ngIf="hasNewUserErro ...

Working with jQuery: Retrieving the ID of an element that is generated dynamically

Looking for some guidance: I'm working on dynamically creating a table based on the response from an AJAX call. The table includes headers and rows, with new rows added using the "after" function. However, I'm facing an issue with accessing the ...

Display a DIV element using jQuery when two input values are identical

I have been trying to display a DIV if two input values are the same, following a similar question on Stack Overflow: How to compare two inputs and show a div. Even though I have copied the code snippet exactly, it doesn't seem to work when I test it ...

How can JavaScript be used to toggle the visibility of text on a webpage

Can anyone help me with a problem I'm having toggling text? I have a truncated text and I want to be able to switch back and forth between the truncated text and the original text on button click. Here is a link to the pen. var myButton = documen ...

Could really use a hand getting this card grid to be more responsive

Recently, I delved into using HTML & CSS for work. However, I encountered a major hurdle: creating responsive Grid card elements. I have four card elements in a column, each with text that increases opacity when hovering over the card. When viewed on a t ...

Unable to display images on mobile devices using HTML

I recently created a website using HTML and CSS. I have successfully added images to some of the web pages, and they display properly on laptops and desktops. However, I am facing an issue where the images do not appear on small screens, even though all im ...

The viewport dimensions and media query parameters do not align

Currently working on an HTML/CSS/JS project. Within my <head> section, there's a viewport <meta> tag: <meta name="viewport" content="width=device-width, initial-scale=1" /> Encountering unexpected behavior w ...

Issue with Table Element's Full Width Within Parent Element

Here is a demonstration of the scenario in JSFiddle I have made updates to the JSFiddle demonstration here: http://jsfiddle.net/x11joex11/r5spu85z/8/. This version provides more detailed insight into how the sticky footer functions well, albeit with a hei ...

Establishing a secure HTML5 websocket connection in a webworker on Chrome version 61

Currently, I am utilizing code to establish a secure websocket connection from a web worker. Strangely, it is functioning perfectly in Chrome 60 but encountering issues in Chrome 61. On the contrary, it works seamlessly in other browsers such as IE, Firefo ...

Ways to showcase every resource from an API in React JS

I need help with adding code to display products along with their images from an API in my existing code. Can someone assist me with this? import React, {useState, useEffect} from 'react' import "bootstrap/dist/css/bootstrap.min.css" im ...

I am unable to apply CSS to style my <div> element

I've run into a snag with my coding project, specifically when attempting to style my div. Here is the code I have so far: All CSS rules are applying correctly except for the .chat rule. Can someone help me figure out what I'm doing wrong? var ...