tags:

views:

698

answers:

6

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?

+3  A: 

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

Bryson
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.
jerbersoft
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?
Bryson
background-image: url('images/bgimage.jpg'); removing the first "/" does not work either.
jerbersoft
If you're using an external css stylesheet, verify its url in the <link> tag in the <head> section of your html code.
Julian Aubourg
the CSS file's url is verified correct since it can be used in VS.
jerbersoft
+2  A: 

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');
Tyson
this is exactly the setup. it is relative.
jerbersoft
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.
Vegard Larsen
@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?
Chuck
A: 

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')?

Julian Aubourg
yes, I am reaching for the image file in the "images" subdirectory.
jerbersoft
then change the url by removing the first / :)
Julian Aubourg
unfortunately, it does not work. i'm getting confused here.
jerbersoft
+1  A: 

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

Philippe
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.
jerbersoft
+1  A: 

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

Igor Pavelek
+1  A: 

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');

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