JSFiddle - React, Tailwind, and code Playground
HTML
<div class="image-wrapper">
<img src="http://placehold.it/350x300">
</div>
<div class="image-wrapper">
<img src="http://placehold.it/500x350">
</div>
<div class="image-wrapper">
<img src="http://placehold.it/300x500">
</div>
<!-- Using background size cover -->
<div class="use-cover-background square" style="background-image: url(http://placehold.it/350x300)"></div>
<div class="use-cover-background square" style="background-image: url(http://placehold.it/500x300)"></div>
<div class="use-cover-background square" style="background-image: url(http://placehold.it/300x500)"></div>
CSS
.image-wrapper {
width: 300px;
height: 300px;
overflow: hidden;
position: relative;
background: rgba(0, 0, 0, 0.5);
margin: 10px 0;
}
.image-wrapper img {
width: 100%;
min-height: 100%;
position: absolute;
left: 0;
top: 50%;
transform: translateY(-50%);
}
.use-cover-background {
background-size: cover;
background-position: center;
}
.square {
margin: 10px 0;
width: 300px;
height: 300px;
}