Utilizing ASP.NET Core for a Consistent Background Image Across Web Pages

I've been trying to set a background image for all the pages in my web application, but I've encountered some difficulties. I'm using an ASP.NET Core web app with razor pages and have bootstrap installed via NuGet, which provided me with a site.css file.

In my site.css file, I included the following code:

#HomepageBody{
    background-image: url("Images/cloud-1581.jpg");
    background-repeat: no-repeat;
    background-position: center;
}

Additionally, in my Shared/_Layout.cshtml file, this is what I have:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewData["Title"] - CustomerPageTest</title>
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
    <link rel="stylesheet" href="~/css/site.css" />
</head>
<body id="HomepageBody">
    <header>
        <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
            <div class="container">
                <a class="navbar-brand" asp-area="" asp-page="/Index">CD Insights</a>
                <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
                        aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
                </button>
                <div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse">
                    <ul class="navbar-nav flex-grow-1">
                        <li class="nav-item">
                            <a class="nav-link text-dark" asp-area="" asp-page="/Index">Home</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link text-dark" asp-area="" asp-page="/Customer/List">Add Assessment</a>
                        </li>
                    </ul>
                </div>
            </div>
        </nav>
    </header>

Although I've added the ID to the body, nothing seems to change when the web app is running. Any suggestions on how I can resolve this issue?

Answer №1

Your website's site.css file is referencing the image URL as:

background-image: url("Images/cloud-1581.jpg");
However, since the site.css file is located at ~/css/site.css, it is actually looking for the image at a relative URL like this: ~/css/Images/cloud-1581.jpg. Unfortunately, the image cannot be found at this location.

To solve this issue, change the reference to an absolute URL by adding a leading slash, such as:

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

Answer №2

For the project to run smoothly, it is essential to place the Images folder within the wwwroot directory.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

JavaScript / html Error: function body missing closing curly brace

I encountered an error that I'm struggling to resolve: "SyntaxError: missing } after function body". Despite not having a function named 'body', I have attempted changing every instance of 'body' in my script. However, ...

How to Condense an Element Using Position: Absolute

https://i.sstatic.net/GMUss.png I'm attempting to create functionality where the "Open Chat" button displays an Absolute positioned div, then hides it when clicked again. I experimented with using the react-collapse component, but encountered issues ...

Setting a variable equal to user input

When using a jQuery script, the elements for the details are dynamically created inside a jQuery function. However, there seems to be an issue with assigning the value from the variable "startLocation" to the input "detail_startLocation[]". Only the firs ...

What could be causing my background image to vanish or flicker in Bootstrap?

Something unusual is happening with my website. There's a section with a static background image, and as you scroll, a quote and button (which will eventually link to a store) appear on top of it. Previously, this setup was working fine. However, yest ...

Using Microsoft.Office.Interop.Word in ASP.NET, integrate HTML tags into a Word document

Is it possible to add HTML tags into a Word document using Microsoft.Office.Interop.Word in ASP.NET? Here is my code snippet: Microsoft.Office.Interop.Word.Paragraph oPara1; oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing); oPara1.Range.Text = "<b&g ...

Encountering the NETSDK1004 issue when launching a .NET 5 ASP.NET MVC Web Application

After successfully installing the .NET 5.0 SDK, I proceeded to create a new project (ASP.NET Core WEB APP). However, I am encountering an error when attempting to build the project. Despite extensive research, I have been unable to resolve this issue. ht ...

When implementing the Slick Carousel with autoplay within an Isotope grid, the height of the image slides may occasionally reduce to 1 pixel when applying filters

I've been struggling with this issue for more than a day now, and despite searching through similar posts and trying their solutions, I still haven't been able to resolve it. Here's an example of the problem: https://jsfiddle.net/Aorus/1c4x ...

Does the Isolated model in C# apply to console applications?

Recently, with the introduction of .NET 8 as the new standard for Azure Functions, I discovered the concept of the "isolated worker model" which is set to replace the older "in-process model". This transition involves making adjustments to existing functio ...

What is the reason for background images not loading when using `display: none`?

I'm really struggling to understand this puzzle: When the page loads, will mypic.jpg be downloaded by the browser? #test1 { display: none; } #test2 { background-image: url('http://www.dumpaday.com/wp-content/uploads/2017/01/random-pic ...

Having issues with the horizontal list layout

I am having trouble implementing Bootstrap horizontal lists on my website. How can I resolve this issue? Desired outcome: https://i.sstatic.net/zzBFi.png Current situation: https://i.sstatic.net/cfoZN.png Below is the snippet of CSS code in question ...

Retrieve the Scope ID value for labeling purposes

As a newcomer to the world of development, I am looking to display the last key id of an inserted row in a label. However, the code I currently have is not displaying the desired value in the label. Here is the code snippet I am working with: using (var ...

What is the best way to display multiple canvases on one page?

Is it possible to draw multiple canvases in a single page using this code? I have successfully drawn a single canvas using the provided code. For each canvas, I have different y data and I need to display 6 canvases per page. How can I achieve this on a ...

Turning JSON data into a DOM structure with the help of jQuery

I am seeking a way to display each 'item' from JSON data within a section or div, including the image and its link along with the name, id, and price. How can this be achieved using jQuery? The current jQuery code provided below is referencing th ...

Encountering difficulty with locating a button element within an HTML form using Selenium WebDriver in C#

Full HTML Codehttps://i.sstatic.net/5FsRt.png How do I access the button identified by the id(export_but)? The image shows the HTML code of a form tag that appears when a button is clicked. I have tried various methods such as XPath, CssSelector, and Id ...

Converting JSON data into composite models using IDictionary

I am working with a composite model public class Composite { public SubType1 Property1 {get;set} public SubType2 Property2 {get;set} public IDictionary<string, object> Property3 {get;set} } After WEB API processes it, Property3 contains ...

What is preventing Bootstrap properties from being applied to HTML elements appended to the DOM through JavaScript?

Currently, I am immersed in a project that utilizes Bootstrap to generate various nested components styled in accordance with Bootstrap's class system. I triumphantly crafted a card component using Bootstrap, but encountered a challenge when attemptin ...

The select tag is malfunctioning in the Firefox browser upon clicking with the mouse

One of the select tags in my application is causing issues in Firefox browser. Although all other select tags in different pages work fine, this particular page seems to be problematic. When attempting to change the option in the select tag, it fails to r ...

When I increase the height of my side bar to 100%, it extends beyond the borders of the container div in CSS

I am facing an issue where my sidebar overflows the container div's border when I set its height to 100%. Is there a way to adjust its height to be 100% minus a specific number of pixels? Below is the source code: <div id="main"> <br /& ...

Creating colorful containers using Bootstrap

Hey there, I am interested in creating small colored boxes to represent different meanings for each color. I came across this code snippet but I am unsure how to adjust the size: <div class="p-3 mb-2 bg-primary text-white">.bg-primary</d ...

What could be causing my carousel to not load on my webpage?

I'm struggling with getting my carousel to properly load on my webpage. Despite trying to adjust the CSS height and width for the carousel div, as well as setting heights for the image elements inside, I still can't seem to get it right. Any help ...