This project is read-only.

"background-image: url('img/line.gif')" in the Masterpage

Topics: Developer Forum
Feb 4, 2009 at 8:30 AM

I faced problem:
At the MasterPage I used html relative path to the background image:
<td style="background-image: url('img/line.gif'); background-repeat: repeat-y">
At first hieracy level it is ok, ex:
But at the next level (when ) using this link is not leading to the gif file anymore.

Currently I sovled it from code-behind for every backgroud imge (((
<td style="background-image: url('<% = line %>'); background-repeat: repeat-y">
public partial class MasterPage : System.Web.UI.MasterPage
    public string fol1;
    public string fol2;
    public string line;
    protected override void OnInit(EventArgs e)
    private void FixImageUrls()
        string[] s = this.Request.RawUrl.Split("/".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
        StringBuilder sb = new StringBuilder();
        for (int i = 1; i < s.Length; i++)
            sb = sb.Append("../");
        line = sb.ToString() + "img/line.gif";
        fol1 = sb.ToString() + "img/fol1.gif";
        fol2 = sb.ToString() + "img/fol2.gif";

Sure that there is much better way.
Does anyone have ideas?

I am aware that using asp:Image I can specify path from project root like "~/img/line.gif", but asp:image is not possible in the backgroud...
Feb 4, 2009 at 8:38 AM
It's not a problem.
Just write "background-image: url('/img/line.gif');"
This mean that everytime the page will load the image will be loaded from root directory img. Slash before image path force browser to look your path from root.
Feb 4, 2009 at 8:53 AM
Thank you very much
I am new to html ((