Humpty11133
Humpty11133

Reputation: 11

How to make random image from array responsive?

I created an array with four different images which are randomly picked. Works like a charm.

My question is how to make those images responsive?

// place your images in this array
var random_images_array = ['1.jpg', '2.gif', '3.gif', '4.gif'];

function getRandomImage(imgAr, path) {
  path = path || 'random/'; // default path here
  
  var num = Math.floor(Math.random() * imgAr.length);
  var img = imgAr[num];
  var imgStr = '<img src="' + path + img + '" alt = "">';
  
  document.write(imgStr);
  document.close();
}

getRandomImage(random_images_array, 'random/')

Upvotes: 0

Views: 198

Answers (1)

jasonflaherty
jasonflaherty

Reputation: 1944

This is a great post

In short:

div img {  height: auto;  width: 100%; }

<div><img src="imgsrc" alt=""></div>

Is one way....

Upvotes: 3

Related Questions