Using object-fit: cover
will achieve the desired effect.
However, it may not be compatible with IE/Edge. To ensure cross-browser functionality, you can implement the following pure CSS solution.
To center the image within the container, I utilized the following CSS properties: position: absolute, top: 50%, left: 50%, and transform: translate(-50%, -50%).
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
After centering the image, apply the following styles:
// For vertical images (height > width)
height: 100%;
width: auto;
// For horizontal images (width > height)
height: auto;
width: 100%;
These styles replicate the behavior of Object-fit: cover.
See the above technique in action.
https://jsfiddle.net/furqan_694/s3xLe1gp/
This method is compatible with all major browsers.
Original Image
https://i.sstatic.net/YkuCP.png
Vertically Cropped
https://i.sstatic.net/7wVxn.png
Horizontally Cropped
https://i.sstatic.net/WdJTE.png
Square Container
https://i.sstatic.net/Cr8zu.jpg