JSFiddle - React, Tailwind, and code Playground
HTML
<div id="background">
<div id="box">
<div class="title">TITLE OF WEBSITE</div>
<div class="text" id="text-box">Text corresponding to the specfic background shown.</div>
<div class="button">
<button type="button" id="my-button">Randomizing button</button>
</div>
</div>
</div>
CSS
html, body {
height: 100%;
}
#background {
height: 100%;
}
#box {
background: #fff;
width: 40%;
min-width: 400px;
height: 200px;
margin-left: auto;
margin-right: auto;
top: 40%;
position: relative;
}
.title {
width: 100%;
height: 50px;
float: left;
background: #e1e1e1;
text-align: center;
font-size: 24px;
line-height:50px;
}
.text {
background: #c8c8c8;
width: 100%;
height: 100px;
float: left;
text-align: center;
font-size: 18px;
position: relative;
padding-top: 20px;
}
.button {
width: 100%;
height: 50px;
float: left;
text-align: center;
background: #afafaf;
}
JavaScript
var myData = {
1: {
imageUrl: "http://lorempixel.com/800/800/cats/1",
text: "This is the text for image 1"
},
2: {
imageUrl: "http://lorempixel.com/800/800/cats/2",
text: "This is the text for image 2"
},
3: {
imageUrl: "http://lorempixel.com/800/800/cats/3",
text: "This is the text for image 3"
},
4: {
imageUrl: "http://lorempixel.com/800/800/cats/4",
text: "This is the text for image 4"
},
5: {
imageUrl: "http://lorempixel.com/800/800/cats/5",
text: "This is the text for image 5"
},
6: {
imageUrl: "http://lorempixel.com/800/800/cats/6",
text: "This is the text for image 6"
},
7: {
imageUrl: "http://lorempixel.com/800/800/cats/7",
text: "This is the text for image 7"
},
8: {
imageUrl: "http://lorempixel.com/800/800/cats/8",
text: "This is the text for image 8"
},
9: {
imageUrl: "http://lorempixel.com/800/800/cats/9",
text: "This is the text for image 9"
},
10: {
imageUrl: "http://lorempixel.com/800/800/cats/10",
text: "This is the text for image 10"
}
};
function changeImage(){
var randomNumber = Math.floor((Math.random() * 10) + 1);
document.getElementById("background").style.background = "url('"+myData[randomNumber].imageUrl+"')";
document.getElementById("text-box").innerHTML = myData[randomNumber].text;
}
document.getElementById("my-button").addEventListener("click",changeImage);