I am trying to randomize my hero image on the front page of my website but struggling to make it work. Here is my code. I was testing to see if I got an image to display but no image is displayed.
If I do this then it works and an image is displayed.
.hero-image {
background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)),url("images/traffic.jpg");
}
But I am trying to randomize the image and found some code on the web which I was trying to make it work on my site but it doesn't seem to work. I see no image:
<?php
$bg = array('traffic.jpg', 'traffic.jpg', 'traffic.jpg', 'traffic.jpg', 'traffic.jpg','traffic.jpg', 'traffic.jpg' ); // array of filenames
$i = rand(0, count($bg)-1); // generate random number size of the array
$selectedBg = "$bg[$i]"; // set variable equal to which random filename was chosen
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>TheWall</title>
<link rel="stylesheet" type="text/css" href="style.css">
<link href="https://fonts.googleapis.com/css?family=Sawarabi+Gothic" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Montserrat:100" rel="stylesheet">
<style type="text/css">
<!--
.hero-image{
background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), url(images/<?php echo$selectedBg; ?>);
}
In my body, my div looks like this:
<div class="hero-image">
<div class="hero-text">
</div>
</div>