Image cover not stretch to fit

In this example below you will see how to do a Image cover not stretch to fit with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by spenceroreilly, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright spenceroreilly ©
  • HTML
  • CSS
  • JavaScript
    <img class="i1" src="https://upload.wikimedia.org/wikipedia/commons/5/54/Golden_Gate_Bridge_0002.jpg" />
<img class="i2" src="https://upload.wikimedia.org/wikipedia/commons/5/54/Golden_Gate_Bridge_0002.jpg" />
<img class="i3" src="https://upload.wikimedia.org/wikipedia/commons/5/54/Golden_Gate_Bridge_0002.jpg" />
<img class="i4" src="https://upload.wikimedia.org/wikipedia/commons/5/54/Golden_Gate_Bridge_0002.jpg" />
<img class="i5" src="https://upload.wikimedia.org/wikipedia/commons/5/54/Golden_Gate_Bridge_0002.jpg" />
<img class="i6" src="https://upload.wikimedia.org/wikipedia/commons/5/54/Golden_Gate_Bridge_0002.jpg" />
<img class="i7" src="https://upload.wikimedia.org/wikipedia/commons/5/54/Golden_Gate_Bridge_0002.jpg" />
<img class="i8" src="https://upload.wikimedia.org/wikipedia/commons/5/54/Golden_Gate_Bridge_0002.jpg" />
<img class="i9" src="https://upload.wikimedia.org/wikipedia/commons/5/54/Golden_Gate_Bridge_0002.jpg" />
<img class="i10" src="https://upload.wikimedia.org/wikipedia/commons/5/54/Golden_Gate_Bridge_0002.jpg" />
<img class="i11" src="https://upload.wikimedia.org/wikipedia/commons/5/54/Golden_Gate_Bridge_0002.jpg" />
<img class="i12" src="https://upload.wikimedia.org/wikipedia/commons/5/54/Golden_Gate_Bridge_0002.jpg" />
<img class="i13" src="https://upload.wikimedia.org/wikipedia/commons/5/54/Golden_Gate_Bridge_0002.jpg" />

/*Downloaded from https://www.codeseek.co/spenceroreilly/image-cover-not-stretch-to-fit-yYBLwL */
    .cover_wrapper_div {
  position: relative;
  display: inline-block;
  margin: 25px;
  25px;
}

.cover_div {
  opacity: 1;
  position: absolute;
  height: 100%;
  width: 100%;
  top: 0;
  left: 0;
  background-position: 50% 50%;
  background-size: cover;
}

.cover_img {
  display: none;
}

img {
  display: inline-block;
  margin: 25px 25px;
}

img.i1 {
  width: 200px;
  height: 200px;
}

img.i2 {
  width: 50px;
  height: 50px;
}

img.i3 {
  width: 150px;
  height: 300px;
}

img.i4 {
  width: 400px;
  height: 200px;
}

img.i5 {
  width: 1000px;
  height: 200px;
}

img.i6 {
  width: 500px;
  height: 400px;
}

img.i7 {
  width: 400px;
  height: 300px;
}

img.i8 {
  width: 800px;
  height: 100px;
}

img.i9 {
  width: 300px;
  height: 100px;
}

img.i10 {
  width: 100px;
  height: 300px;
}

img.i11 {
  width: 500px;
  height: 350px;
}

img.i12 {
  width: 350px;
  height: 150px;
}

img.i13 {
  width: 150px;
  height: 350px;
}
/*img::after
{
  content:'';
  width:100%;
  height:100%;
  opacity:1;
  background-img:url(attr(data-src));
}*/
/*Unfortunately url() does not accept attr() yet.
And img tags don't accept children tags.*/


/*Downloaded from https://www.codeseek.co/spenceroreilly/image-cover-not-stretch-to-fit-yYBLwL */
    var $cover_imgs = $("img");
$.each($cover_imgs, function(i, $this) {
  $this = $($this);
  var $cover_div = $("<div class='cover_div'></div>").css("background-image", "url(" + $this.prop('src') + ")");
  var $wrapper_div = $("<div class='cover_wrapper_div'></div>").css("width", $this.width()).css("height", $this.height()).appendTo($this.parent()).append($this, $cover_div);
  $this.addClass("cover_img");
});

Comments