I have 3 images: 1- 320 x 420 2- 600 x 450 3- 695 x 355
How can I with css and html to have for all three images the dimension 260 x 260? I don't want cut the images, I want keep their original proportions. Thanks for your help
I have 3 images: 1- 320 x 420 2- 600 x 450 3- 695 x 355
How can I with css and html to have for all three images the dimension 260 x 260? I don't want cut the images, I want keep their original proportions. Thanks for your help
object-fit
is made for this, it will keep proportion and can also clip your image the way you want:
The
object-fit
CSS property sets how the content of a replaced element, such as an<img>
or<video>
, should be resized to fit its container.syntax values :
fill | contain | cover | none | scale-down
You can alter the alignment of the replaced element's content object within the element's box using the
object-position
property. ex :
img[class] {
width:260px;
height:260px;
object-fit:cover;
}
img:nth-child(1) {
object-position: center top;
}
img:nth-child(3) {
object-position: right top;
}
<img class src=https://placeimg.com/320/420/animals>
<img class src=https://placeimg.com/600/450/animals>
<img class src=https://placeimg.com/695/355/animals>
<p>Original size</p>
<img src=https://placeimg.com/320/420/animals>
<img src=https://placeimg.com/600/450/animals>
<img src=https://placeimg.com/695/355/animals>
If your images are only parts of the design, then background-image,background-size,background-position
can do similar visual.
Is this what you're looking for? It uses background-size:contain
to automatically make sure the whole image fits, then background-position:center
to center it in the div.
#image {
border: 2px solid black;
height:260px;
width:260px;
background: url("https://wallpapertag.com/wallpaper/full/0/6/3/49612-mountains-wallpaper-1920x1080-for-android-40.jpg");
background-repeat: no-repeat;
background-position: center;
background-size: contain;
<div id="image">