Scale image by centering while maintaining aspect ratio using CSS

I want to create an image gallery like a mobile phone gallery. each image should be in a square box inside the main container.

The image should not stretch and not crop or clip. now i am stretching image with aspect ratio using this code,

img {
  max-width : 150px;
  max-height : 150px;
}

      

it gives me a reduced thumbnail of each image. but I want to do everything inside a square square with a clipped or cropped clip.

here is the example i want, enter image description here

+3


source to share


1 answer


Here is the solution how I achieved it ... it preserves the aspect ratio and brings the image to the center as if I want ...



.scale-image {
  object-fit: cover;
  width: 150px;
  height: 150px;
}

      

+1


source







All Articles