I’m making a random image generator in JavaScript following this online guide. I’m looking to generate multiple random images as shown in example 2 of the guide but am unsure how to make these images display horizontally instead of vertically. Any help is appreciated!
Here’s the code in question below:
function getRandomImage() {
var randomImage = new Array();
randomImage[0] = "https://wi.wallpapertip.com/wsimgs/15-155208_desktop-puppy-wallpaper-hd.jpg";
randomImage[1] = "http://www.petsworld.in/blog/wp-content/uploads/2014/09/running-cute-puppies.jpg";
randomImage[2] = "https://wi.wallpapertip.com/wsimgs/156-1564365_golden-retriever-puppy-desktop-wallpaper-desktop-wallpaper-puppy.jpg";
randomImage[3] = "https://wi.wallpapertip.com/wsimgs/156-1564140_free-puppy-wallpapers-for-computer-wallpaper-cave-cute.jpg";
randomImage[4] = "https://wi.wallpapertip.com/wsimgs/156-1565522_puppies-desktop-wallpaper-desktop-background-puppies.jpg";
randomImage[5] = "https://wi.wallpapertip.com/wsimgs/156-1566650_cute-puppies-desktop-wallpaper-cute-puppies.jpg";
for (let i = 0; i < 5; i++) {
var number = Math.floor(Math.random() * randomImage.length);
document.getElementById("result").innerHTML += '<img src="' + randomImage[number] + '" style="width:150px" />';
}
}
getRandomImage()
<div id="result"></div>
4
Answers
If you want all images in the same row, add the following CSS to make it work.
I don’t know what you mean, however please replace div tag with this
I think this meets your requirement.
The randomImage array can be declared like this (optional):
In addition to some improvements on how to render a random sequence of distinct images, the OP needs to make use of syntax and properties of the CSS flex box layout.