0

I'm trying to display an image on the background of a site through CSS. I want the image to be kept in a separate folder than the CSS/HTML file. The image only displays, if I place it in the same folder as the CSS/HTML file, and reference its name only.

The code is in C:\Users\User\Desktop\project\templates and the image is in C:\Users\User\Desktop\project\pictures. If I try using the absolute path it won't display. It will only display if I place the picture in the templates folder.

<!--This code works if I place the picture in the template file-->

<!DOCTYPE html>
<html>
<head>
<style>
body {
  background-image: url("picture.png");
}
</style>
</head>
</html>

<!--This code doesn't work-->

<!DOCTYPE html>
<html>
<head>
<style>
body {
  background-image: url("C:\Users\User\Desktop\project\pictures\picture.png");
}
</style>
</head>
</html>
Constantin Groß
  • 10,719
  • 4
  • 24
  • 50
Alec McKay
  • 135
  • 7

2 Answers2

0

Use this css

Body{

background-image: url("../pictures/picture.png")
}
PRADEEP GORULE
  • 159
  • 1
  • 11
0

Based on your folder structure, you might need to do a ../pictures/picture.png

background-image: url("../pcitures/picture.png");

To find the image when it's in the pictures folder. The first '.' means the current directory(.css file), while the second '../' means the parent directory (projects folder). From there, you navigate to pictures folder and then directly link with the picture image. Use this link to learn about file paths. https://en.wikipedia.org/wiki/Path_(computing)

Avoid absolute file path. Imagine transferring your files to a new server, you'll have to rewrite the file path again.

 background-image: url("C:\Users\User\Desktop\project\pictures\picture.png");