[html] Changing image sizes proportionally using CSS?

I have been trying for a couple of days now to configure my thumbnail gallery so all the images appear the same height and width. However when I change the Css code to,

max-height: 150px;
max-width: 200px;
width: 120px;
height: 120px;

I get images that are all the same size but the aspect ratio is stretched ruining the images. is there not a way to resize the image container and not the image instead? allowing me to keep the aspect ratio. but resize the image still. (I dont mind if i cut off some of the image.)

Thanks in advance!

This question is related to html css gallery lightbox

The answer is


You need to fix one side ( eg height ) and set the other to auto.

Eg

 height: 120px;
 width: auto;

That would scale the image based on one side only. If you find cropping the image acceptable, you can just set

overflow: hidden; 

to the parent element, which would crop out anything that would otherwise exceed its size.


transform: scale(0.5);

Example:

<div>Normal</div>
<div class="scaled">Scaled</div>
div {
  width: 80px;
  height: 80px;
  background-color: skyblue;
}

.scaled {
  transform: scale(0.7); /* Equal to scaleX(0.7) scaleY(0.7) */
  background-color: pink;
}

see: https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/scale


Put it as a background on your holder e.g.

<div style="background:url(path/to/image/myimage.jpg) center center; width:120px; height:120px;">
&nbsp;
</div>

This will center your image inside a 120x120 div chopping off any excess of the image


To make images adjustable/flexible you could use this:

/* fit images to container */
.container img {
    max-width: 100%;
    height: auto;
}

if you know the spect ratio you can use padding-bottom with percentage to set the hight depending on with of the diff.

<div>
   <div style="padding-bottom: 33%;">
      i have 33% height of my parents width
   </div>
</div>

This help me to make the image 150% with ease.

.img-popup img {
  transform: scale(1.5);
}

If you don't want to stretch the image, fit it into div container without overflow and center it by adjusting it's margin if needed.

  1. The image will not get cropped
  2. The aspect ratio will also remain the same.

HTML:

<div id="app">
    <div id="container">
      <img src="#" alt="something">
    </div>
    <div id="container">
      <img src="#" alt="something">
    </div>
    <div id="container">
      <img src="#" alt="something">
    </div>
</div>

CSS:

div#container {
    height: 200px;
    width: 200px;
    border: 1px solid black;
    margin: 4px;
}
img {
    max-width: 100%;
    max-height: 100%;
    display: block;
    margin: 0 auto;
}

You can use object-fit css3 property, something like

_x000D_
_x000D_
<!doctype html>_x000D_
<html>_x000D_
_x000D_
<head>_x000D_
  <meta charset='utf-8'>_x000D_
  <style>_x000D_
    .holder {_x000D_
      display: inline;_x000D_
    }_x000D_
    .holder img {_x000D_
      max-height: 200px;_x000D_
      max-width: 200px;_x000D_
      object-fit: cover;_x000D_
    }_x000D_
  </style>_x000D_
</head>_x000D_
_x000D_
<body>_x000D_
  <div class='holder'>_x000D_
    <img src='meld.png'>_x000D_
  </div>_x000D_
  <div class='holder'>_x000D_
    <img src='twiddla.png'>_x000D_
  </div>_x000D_
  <div class='holder'>_x000D_
    <img src='meld.png'>_x000D_
  </div>_x000D_
</body>_x000D_
_x000D_
</html>
_x000D_
_x000D_
_x000D_

It is not exactly your answer, though, because of it doesn't stretch the container, but it behaves like the gallery and you can keep styling the img itself.

Another drawback of this solution is still a poor support of the css3 property. More details are available here: http://www.steveworkman.com/html5-2/javascript/2012/css3-object-fit-polyfill/. jQuery solution can be found there as well.


Examples related to html

Embed ruby within URL : Middleman Blog Please help me convert this script to a simple image slider Generating a list of pages (not posts) without the index file Why there is this "clear" class before footer? Is it possible to change the content HTML5 alert messages? Getting all files in directory with ajax DevTools failed to load SourceMap: Could not load content for chrome-extension How to set width of mat-table column in angular? How to open a link in new tab using angular? ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment

Examples related to css

need to add a class to an element Using Lato fonts in my css (@font-face) Please help me convert this script to a simple image slider Why there is this "clear" class before footer? How to set width of mat-table column in angular? Center content vertically on Vuetify bootstrap 4 file input doesn't show the file name Bootstrap 4: responsive sidebar menu to top navbar Stylesheet not loaded because of MIME-type Force flex item to span full row width How to add an image to the emulator gallery in android studio? Changing image sizes proportionally using CSS? Mosaic Grid gallery with dynamic sized images Android get image from gallery into ImageView android - save image into gallery android pick images from gallery How can I make a horizontal ListView in Android? Android: Bitmaps loaded from gallery are rotated in ImageView Horizontal ListView in Android? Image, saved to sdcard, doesn't appear in Android's Gallery app Changing image sizes proportionally using CSS? TypeError: $(...).on is not a function Prevent body scrolling but allow overlay scrolling Using JQuery to open a popup window and print Using jQuery Fancybox or Lightbox to display a contact form Lightbox to show videos from Youtube and Vimeo?