I’m trying to learn how to build a website. I’ve built the index page and here is the PHP and HTML.
<?php
session_start();
$png = array('png1.jpg', 'png2.jpeg', 'png3.jpg', 'png4.jpg');
$random = rand(0,4);
$picture = "$png[$random]";
?>
<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" href="index.css">
</head>
<body>
<form action="login.php" method="post">
Name: <input type="text" name="name"><br>
Password: <input type="text" name="password"><br>
<input type="submit">
</form>
</body>
</html>
and here is the CSS where I link to the image file:
body{
background-image: url('png1.png');
}
I’m running the site on replit.com and here is the png1.jpg
:
The background image is not appearing. Only the form is showing.
the link of website on replit is
https://htmlphpcssjavascript-login-system.gepingyi.repl.co/
you can view the code with inspect
2
Answers
I am not sure if your image is not showing or if it is showing in the wrong place.
using the css below you will allow you to set your background image properly.
You appear to try generating a random background image using PHP but then do nothing with that, unless somehow you are hoping that it will exist within your stylesheet? As the stylesheet is a
.css
file any PHP code within will not be executed by the server so to have a dynamic style you could simply add an inline style tags to the page that sets the body backgroundThinking that perhaps the above, being untested, had an issue overlooked when writing the answer I have just put together a working example using images on my system &/or on the web.
The above, when saved with a
.php
extension will yield output like this: