Search code examples
asp.nethtmlvisual-web-developer-2010

How to set a background image in html/ASP.net that will work even when the site is moved?


I'm trying to set an image as a background image. From what I've read – I need to have a tilde (~) before the address or the site won't function if put inside a folder. But though that works when I do the following (which doesn't get the result I want because it's not a background image):

<asp:Image ID="MyImage" runat="server" ImageUrl="~/images/Image1.png"/>

It doesn’t work in where I need it to work:

<body style="background-image: url(~/images/Image1.png) ; background-position: left top; background-repeat: no-repeat">

Adding that tilde there causes the image to disappear.

So how do I add the tilde to a background image? (And why doesn't it work there?)

EDIT: I tried having just:

background-image: url(images/topleft.png)

(without the first forwardslash) but then any page inheriting from this page (which is a master page) didn't "find" the image.


Solution

  • Things are easier if you move all your styles into css, that way you don't have any problems resolving URLs because they're all relative to the location of your stylesheet.