1

I have created a folder inside wwwroot, called Media. I'm trying to set a background image with css like this:

<body style='background-image: url("~/Media/background.png")'>

But the background does not appear.

However, if I try to put is as a image it works:

<img src="~/Media/background.png" />

So, how do I handle background images?

3
  • Is the server does not send the imag?! Inspect the code from the browser and see if it received from the server or not ? Commented Mar 13, 2021 at 11:17
  • 3
    ASP.NET's Tag-Helpers for resolving ~/ do not work inside style="" attributes, but you can use @Url.Content(). Commented Mar 14, 2021 at 4:34
  • Bryan: a quick reminder about spelling. Would you please use a spell-checker? Commented Apr 22, 2021 at 22:58

2 Answers 2

1

Try the following:

<body style="background-image: url(../Media/background.png)">
Sign up to request clarification or add additional context in comments.

Comments

0

Maybe: try to use url of image instead of relative path.

In That case I suppose `https://yourdomain/media/background.png' could do the trick.

Comments

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.