Skip to content Skip to sidebar Skip to footer

Automatic Image Resizing To Fit Screen

I have a varying number of images that are display in a row. I want those images to automatically rescale to fit the screen. Example: If I have 1 image it should automatically resc

Solution 1:

Please see this fiddle http://jsfiddle.net/3dR3u/

document.addEventListener("DOMContentLoaded", calculate, false);
window.addEventListener("resize", calculate, false);

functioncalculate() {
  var imgs = document.querySelectorAll(".col-md-3 img");
  var count = imgs.length;
  var wid = Math.floor(window.innerWidth / count);
  for (i = 1; i <= imgs.length; i++) {
    document.getElementById('img' + i).style.width = wid + 'px';
    if (document.getElementById('img' + i).clientHeight > window.innerHeight) {
      document.getElementById('img' + i).style.height = window.innerHeight + 'px';
    }
  }
}
.col-md-3img {
  float: left;
}

body {
  margin: 0px;
}
<body><divclass="row"id="images"><divclass="col-md-3"><imgid="img1"src="http://farm4.staticflickr.com/3680/13191485343_88d5da5123.jpg"><imgid="img2"src="http://farm4.staticflickr.com/3680/13191485343_88d5da5123.jpg"><imgid="img3"src="http://farm4.staticflickr.com/3680/13191485343_88d5da5123.jpg"><imgid="img4"src="http://farm4.staticflickr.com/3680/13191485343_88d5da5123.jpg"></div></div></body>

Solution 2:

Well a possibility, using jQuery, would be:

$('.col-md-3 img').css('width', 100/$('.col-md-3 img').length+"%");

Post a Comment for "Automatic Image Resizing To Fit Screen"