CSS Image size, how to fill, but not stretch?

You can use the css property object-fit. (“sets how the content of a replaced element, such as an <img> or <video>, should be resized to fit its container.”)

.cover {
  object-fit: cover;
  width: 50px;
  height: 100px;
}
<img src="http://i.stack.imgur.com/2OrtT.jpg" class="cover" width="242" height="363" />

See example here

There’s a polyfill for IE: https://github.com/anselmh/object-fit

Related: object-position (specifies the alignment of an element’s contents within its box.)

Leave a Comment