Search code examples
cssdirectorypathbackground-image

CSS background-image only appears when I have image in same folder as the project index.css file


Please forgive me if my jargon is a bit strange, as I am still a beginner with this stuff...

I am trying to open an image using CSS with a relative path. When I use the relative path to the image I want with "background-image: url()" the only way I can display the image is if the image is in the same folder as the main CSS file ("index.css"). If I move the image ANYWHERE else I cannot display the image.

EXAMPLE #1:

css(folder) -> index.css(main file), image.jpeg(image i want)

In the example above I can write "background-image: url(image.jpeg)" and the image will appear.

EXAMPLE #2:

HOWEVER, let's say I create another folder called "image_folder" inside the css folder that will hold the image:

css(folder) -> index.css(main file), image_folder(image folder that holds the image.jpeg)

In this example I write "background-image: url(image_folder\image.jpeg)" and the image doesn't appear.

Why does the image only appear if I have the image in the same folder as "index.css"? I can't seem to put the image anywhere else and have it appear.

Thanks

PICTURES:

DOES NOT WORK

DOES NOT WORK

DOES NOT WORK

THIS DOES WORK

THIS DOES WORK

THIS DOES WORK


Solution

  • Not sure exactly what I did differently this time, but as Debjeet mentioned, the forward slash in the directory path seemed to fix the problem.