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!
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);
<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;">
</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.
<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>
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
<!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_
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.
Source: Stackoverflow.com