1

I have this:

body {
    background-image: url("Images/Background.jpg");
}

But the url it generates is incorrect no matter what variation I use.

When I run my website, the url is localhost/MYSITE as it's plonking it on a local IIS server (not iisexpress).

The image is stored in a folder called Images on the root of the application.

if I inspect the element this is what I get:

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

gives

url("http://localhost/MYSITE/Styles/Images/Background.jpg")

I don't know where the Styles comes from.

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

gives

url("/Images/Background.jpg")

Which is wrong since it needs to be MYSITE/Images.

background-image: url("~/Images/Background.jpg");

gives

url("http://localhost/MYSITE/Styles/~/Images/Background.jpg")

Styles is the location of the LESS file so I guess that is sorta why?

background-image: url("~/../Images/Background.jpg");

gives

url("http://localhost/MYSITE/Styles/Images/Background.jpg")

What is going on?! Why does it keep doing these weird things?

When I deploy to live it will no longer be IP/MYSITE it will be mysite.mydomain.com so I have to get the relative pathing correct.

1
  • Resource paths tend to get tricky using MVC3 and Less. To keep things simple, try keeping resource URL's away from includes and relative to the .less file you're using them in. Several less implementations also seem to have problems with relative paths in includes in general. (see github.com/madskristensen/WebEssentials2013/issues/134 ) for instance Commented Mar 11, 2014 at 11:47

1 Answer 1

2

If you give url of image in css it path start right from the folder where your css is so if your css file is inside Styles folder and you give background-image url as

url("/Images/Background.jpg");

localhost/MYSITE/Styles/Images/Background.jpg

because your folder structure is like this

Root
  Styles
  Images

so if you want to point some image in the Images folder from your css file in Styles folder you should write like this

url("../Images/Background.jpg");

It means go to the parent folder that is Root and then Images and then Background.jpg

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

1 Comment

I see, so when using url() in CSS you should avoid "~/blahblah" ?

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.