background-image relative url doesnt work in CSS s

2019-06-13 16:54发布

问题:

I am keeping my both html and css stylesheet files in the same folder and images in a "img" folder.

Relative links to images works on html:

<img src="img\Philippines.png">

but it doesn't work in css when i write:

#Philippines {
background-image:url("../img/UK.png");

it does work when i put full url:

#Philippines {
background-image:url("file:///C:/Users/User/Dropbox/+HOROS%20code/img/Philippines.png");

I want it to work with relative urls, any help?

回答1:

url() in CSS is relative to the location of your CSS file.

If I understand you correctly, this is your folder structure:

-+-img
 |  +--images
 +html_file
 +css_file

If so, try this:

#Philippines {
    background-image:url("img/UK.png");
}