I am making a random image generator for a school assignment, how do I center the image? I am new to java script and would like to know how to center the images made by the generator. I have tried a few different ways of centering the code but haven't found any way of doing it. If there are workarounds to get it centered that is alright as I just need to have it centered. Here is the code;
<html>
<head>
<title>Display random images</title>
<style>
body {
margin-top: 30px;
}
</style>
</head>
<script>
function displayRandomImages()
{
//array of images with image location, height, and width
var imageArray = [
{
//address URL of the image
src: "https://i.insider.com/5d8a4d172e22af08c029c929?width=750&format=jpeg&auto=webp",
//size for the image to be display on webpage
width: "650",
height: "500",
},
{
src: "https://static.stacker.com/s3fs-public/styles/slide_desktop/s3/editedWWIIMonopolySPYSCAPE2000xpngv1587097885.PNG",
width: "650",
height: "500",
},
{
src: "https://images-na.ssl-images-amazon.com/images/I/71IbpNx-3oL._SL1000_.jpg",
width: "650",
height: "500",
},
{
src: "https://cdn.vox-cdn.com/thumbor/wj2qaxojsQT3DrUbP78SVW1fpMs=/0x0:1500x1500/1200x800/filters:focal(630x630:870x870)/cdn.vox-cdn.com/uploads/chorus_image/image/65772083/81C985A22GL._SL1500_.0.jpg",
width: "650",
height: "500",
} ];
//find the length of the array of images
var arrayLength = imageArray.length;
var newArray = [];
for (var i = 0; i < arrayLength; i++) {
newArray[i] = new Image();
newArray[i].src = imageArray[i].src;
newArray[i].width = imageArray[i].width;
newArray[i].height = imageArray[i].height;
}
// create random image number
function getRandomNum(min, max)
{
// generate and return a random number for the image to be displayed
imgNo = Math.floor(Math.random() * (max - min + 1)) + min;
return newArray[imgNo];
}
// 0 is first image and (preBuffer.length - 1) is last image of the array
var newImage = getRandomNum(0, newArray.length - 1);
// remove the previous images
var images = document.getElementsByTagName('img');
var l = images.length;
for (var p = 0; p < l; p++) {
images[0].parentNode.removeChild(images[0]);
}
// display the new random image
document.body.appendChild(newImage);
}
</script>
<body>
<div>
<center>
<h2 style="color:green"> Random Image Generator </h2>
<h4> Press the button to display and change the image </h4>
<button onclick="displayRandomImages();"> Display Images </button>
</center>
</div>
</body>
</html>