1

I am using the background image in my application in CSS . I am using the following code to get the background image file

background-image: url("Image\GREEN.GIF");

but I am not able to get the Image .. I know that it is path problem .. I can able to get the absolute path in java using

path=new File("web/CSS/myimage.gif").getAbsolutePath();

but in CSS how to get the absolute path..

please help me in getting the absolute path in CSS

Thanks in Advance

Raj

Raveline
  • 2,660
  • 1
  • 24
  • 28
Raj
  • 2,463
  • 10
  • 36
  • 52

4 Answers4

5
url("Image\GREEN.GIF")

should be

url("Image/GREEN.GIF")

Relative URLs should work perfectly well in CSS, so you don't need absolute URLs. Just be aware that some URLs are relative to the stylesheet, not the HTML that loads it.

Community
  • 1
  • 1
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
2

I'm not sure there is a way to get the absolute path via CSS, but can you not use relative paths here?

Say you have the following:

-root
-----css
-----img

In order to then reference your images from you css file, you would simply put a path of

background-image: url("../img/myfile.gif");
dougajmcdonald
  • 19,231
  • 12
  • 56
  • 89
  • Thanks .. it is working .. But my doubt is if I deploy the war file in tomcat webapps folder , is it work? – Raj Sep 09 '11 at 09:57
  • You should be ok because the path to your image will be relative to the folder you css resides in. Assuming you keep the folder structure the same in your deployment environment and you're not using some form of URL rewriting which interferes, you should be ok. – dougajmcdonald Sep 09 '11 at 10:02
0

Also remember that the item your giving the background image to must have content in it, or set dimensions! Otherwise nothing will show!

Graeme Leighfield
  • 2,825
  • 3
  • 23
  • 38
0

You don't need absolute URLs.Normally,the urls in stylesheet are relative to the stylesheet,but if you define the mouse cursor's image,URLs relative to the stylesheet are not supported by IE,you should set the path relative to root path or use absolute path.