8

What is the correct syntax when setting the background-image in CSS? In visual studio, there seems to be no problem in the background since it appears. But in the browser like IE or FF, the background does not appear. is there something i missed here?

syntax i am using is below (which i think is correct...)

#headerArea
{
    height: 150px;
    background-image: url('/images/bgimage.jpg');
}

the above is correct right?

2
  • Can you give us an idea of the file structure? Where is your CSS relative to your image on your hard drive? Commented Mar 16, 2009 at 18:36
  • the CSS file is inside the "styles" folder in the root directory and the image file is inside the "images" folder in the root directory. so basically, they are separated. Commented Mar 17, 2009 at 0:35

8 Answers 8

8

Depending on your folder structure and where the CSS is located relative to the images it is using you will have to go up to the root level of the image directory and access it from there so you could maybe try something like

background-image: url('/../images/bgimage.jpg');

Sign up to request clarification or add additional context in comments.

1 Comment

gotcha! but i used: background-image: url('../images/bgimage.jpg'); without the first "/". so at least you gave me an idea. thanks!
7

i got mine working w/ ../images/bgimage.jpg

how? i did NOT use quotes - ex:

background-image: url(../images/bgimage.jpg);

1 Comment

worked for me but had to remove whitespace from file name
6

If you're testing on a local machine without using a web server (i.e. if the URL of your page in FF starts with "file://", that url wont work. You'll want to specify a relative path to the image because otherwise it'll be looking for that image at the root of your hard drive.

So if your files are like this:

/some/path/html/index.html
/some/path/html/images/bgimage.jpg

Your code will look like:

background-image: url('images/bgimage.jpg');

3 Comments

this is exactly the setup. it is relative.
Remember that the image path in the CSS is relative to the location of the CSS file, not to the HTML file. So you might need to do ../images/bgimage.jpg if your CSS file is in subfolder.
@jerbersoft: That is NOT the code you posted in your question. You used the absolute path url('/images/bgimage.jpg'). Note the leading slash. Were you mistaken in the question?
4

That's correct syntax. Have you checked whether the image is in the right location?

5 Comments

i have checked and in Visual Studio, there is no problem because the background image appears normally. the problem is when I run it, in FF and IE, the background does not appear.
Since I don't have enough reputation yet, I'll respond to your comment on Philippe's answer: yes, the background would disappear in Visual Studio. However, when you view the page in FF/IE, does it appear?
background-image: url('images/bgimage.jpg'); removing the first "/" does not work either.
If you're using an external css stylesheet, verify its url in the <link> tag in the <head> section of your html code.
the CSS file's url is verified correct since it can be used in VS.
3

And remember, relative path is relative to the CSS sheet.

Comments

2

If it is a relative path, remove the heading "/" in the url path?

1 Comment

when i try removing the "/", in Visual Studio, the background disappears so i think the "/" is required (or is it?). btw, i am using VS 2008 SP1.
1

Are you certain about the / at the beginning of the url? Aren't you trying to reach the image in the "images" subdirectory... which would imply url('images/bgimage.jpg')?

2 Comments

yes, I am reaching for the image file in the "images" subdirectory.
then change the url by removing the first / :)
1

My CSS file is in "Themes" folder. The images are saved in "Images" folder. Both "Themes" and "Images" are sub-folders under the root directory of my project.

In this case you should use

background-image: url(../Images/imageName.jpg);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.