1

I am trying to add a background image to my ASP.NET Core application, but it's not showing.

Even a normal is not showing.

<div class="landing">
    <div class="text-center">
        <h2> app</h2>
        <h4>blah </h4>
        <button class="btn-success">See what we have</button>
    </div>
</div>
<style>
    .landing {
        background-image: url(..\..\wwwroot\images\bkimage.jpg);
        background-repeat: no-repeat;
        background-attachment: fixed;
        background-size: cover;
        height: auto;
    }
</style>

2 Answers 2

5

I'll assume the snippet above is from an MVC view. When you're using the wwwroot folder, that represents the static content root against which other things can reference (unless you've explicitly changed that). So, the 'wwwroot/images/bkimage.jpg' would just be the root-relative path '/images/bkimage.jpg' off that.

Try the following:

background-image: url('/images/bkimage.jpg'); 
Sign up to request clarification or add additional context in comments.

3 Comments

how about with base path? ~/ not work in url()
CSS is not modified by the Razor syntax like in views, so you must reference a valid relative or absolute URL. Either your image is in your wwwroot folder (the "base" path as it pertains to your hosted app) and you can just do /image.jpg, or you mean the base folder in your project, in which case the question is more involved ("How can I expose images from other places than the wwwroot folder?") and that should be asked separately.
/image not equals ~/image, in my case, the answer is Url.Content("~/")
0

Height attribute must be quantified. For example: height:400px.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.