The background image refuses to load

I've been working on a website project using nodejs, expressjs, and pug as my template engine. I designed the home page in pure pug but ran into an issue when trying to add a background image to a specific section of the site. Despite double-checking the image path, developer tools keep reporting a 404 error for not finding the image. Can anyone lend a hand?

My file structure in vs code can be viewed here.

Here's my dance.js:

const express = require("express");
const path = require("path");

const app = express();

app.use("/static", express.static("static"));
app.use(express.urlencoded());
app.set("view engine", "pug");
app.set("views", path.join(__dirname, "render"));

app.get("/", (req,res)=>{
    res.status(200).render("dance");
});

const port = 80;
const hostname = "127.0.0.1"
app.listen(port, hostname, ()=>{
    console.log(`Application is accessible on http://${hostname}:${port}`);
});

Here's dance.pug:

doctype html
html
    head
        title My dance academy 
        style 
            include ../static/style.css
    body
        div.navbar 
            ul#navigation
                li #[a(href="/") Home]
                li #[a(href="/") Services]
                li #[a(href="/") Our courses]
                li #[a(href="/") Contact us]
                li #[a(href="/") About us]
        div#sectionForimage
            h1 Express your emotions through your moves
            h3 
        div#section2
            p There
        div#section3
            p Our Footer

And here's style.css:

@import url('https://fonts.googleapis.com/css2?family=Combo&display=swap');/*font-family: 'Combo', cursive;*/
*{
    margin: 0px;
    padding: 0px;
    box-sizing: border-box;
}

.navbar ul li a{
    text-decoration: none;
    color: white;
}

.navbar ul li a:hover{
    font-weight: bold;
    /* font-style: italic; */
    color:rgb(0, 216, 249);
}

.navbar ul li{
    list-style-type: none;
    padding: 1rem;
    font-size: 1.7rem;
    font-family: 'Combo', cursive;
    margin:10px;
}

#navigation{
    display: flex;
    justify-content: center;
    background-color: tomato;
}

#sectionForimage{
    /* position: absolute; */
    height: 50vh;
    background-image: url("img.jpg");
}

#section2{
    height:50vh;
    background-color: yellow;
}

#section3{
    height:50vh;
    background-color: red;
}

Your assistance would be greatly appreciated! Thank you!

Answer №1

It is recommended to utilize the root path within your style.css file in the following manner:

#sectionForimage{
    /* position: absolute; */
    height: 50vh;
    background-image: url(/static/img.jpg);
}

Keep in mind that using quotes or double quotes may not be necessary, unless dealing with special characters as outlined in the W3 guidelines.

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

The image being displayed is significantly wider than the corresponding HTML element

I'm attempting to display this webpage in PNG format using npm's wkhtmltox: <!doctype html> <html> <head> <meta charset="utf-8"> <style> html, body { padding: 0; width: 340px; } ...

Error handling middleware for Node.js to catch and handle CORS issues in

Here is my code snippet for checking the origin header and allowing access by using express along with npm cors plugin: const app = express(); const cors = require('cors') var corsOptions = { origin: function (origin: any, callback: any) { ...

Ways to standardize the input email address?

While using express-validator, I came across an issue where I used normalize email for validation of email during sign up and stored the normalized email on the server. Validation code: router.post( "/signup", [ check("name").n ...

Tips for positioning a sticky div underneath a stationary header

I'm currently utilizing Bootstrap 4 for my project, and I am encountering an issue with maintaining a div that has the class "sticky-top" just below a fixed navbar. Despite attempting to use JavaScript to replace the CSS while scrolling, it hasn' ...

If the overflow-x property is set to hidden for the html and body elements, the links in the footer will become unclickable as they will

Situation: Consider the following scenario with a simplified HTML example: position the footer behind the content and make it stick to the bottom when reaching the end of the page, the footer should slide up from behind the content I achieved this layo ...

Node handling multi-client long polling

I am working on developing a node server that can alert long-polling clients whenever there is an update to a file on the server. However, I'm facing challenges in writing the code that will allow the server to detect changes in the file and notify al ...

My date function in Node JS is throwing an error, can someone please help me troubleshoot?

I encountered an error with new date(); while working with node js and express npm plugin. I built a variable date but faced some compilation errors. This is my code .js var update_time = new Date(); update_time.formatDate("y/m/d"); When I run ...

Tips for maximizing page layout efficiency without compromising on element visibility

What is occurring https://i.stack.imgur.com/Agjw6.gif The use of .hide() and .fadeIn(200) is resulting in a jittery effect that I would like to avoid. Desired Outcome When the user hovers over the menu icon, the menu icon should vanish the text "Pr ...

Issue with Bootstrap 4 column width when using Angular 2 *ngFor

I have a container that displays search results. The layout is set up using Bootstrap columns, but there seems to be excessive padding or margin causing the results to appear very narrow. Interestingly, if I manually input each result instead of using *ngF ...

Troubleshooting Error Message in Reactjs + Express Twitter API: "Authentication Failed"

I am attempting to develop a straightforward app that enables me to post tweets. Currently, I have React running on port 3000 and an express server.js running on port 5000. My server.js file includes the following code: app.post("/twitter/message", async( ...

Unable to install npm without symlinks option

I have successfully set up a development environment on Windows 8 with Ubuntu as a virtual machine using VirtualBox. I was able to create a shared folder within VirtualBox as well. Within this shared folder, I attempted to start a project using ember-gene ...

Node.js API failing to display specific product information for a given product ID

I am in the process of developing a basic Node.js API without using any frameworks. Below is my folder structure: controllers productController.js models productModels.js data products.js node_modules package.json package-lock.json server.js ...

What is the best way to hide the div when scrolling to the top?

Is there a way to hide the circle once it's scrolled to the top? Currently, I can scroll the circle to the top but it remains visible. Upon further exploration, I found that clicking on the circle scrolls it to the top and then on a second click, it f ...

The hovering effect on the image link is causing the surrounding div to stretch too far

Whenever I hover over specific points below an image link wrapped in a div, it triggers the hovering effect for the CSS-created image link. Despite trying various solutions like overflow:hidden and display:inline-block, nothing seems to resolve this issue. ...

Modify form layout upon selection of a specific radio button

Hey there! I'm a student currently diving into the world of JavaScript, CSS, and HTML. However, I've encountered a little problem while working on an exercise that involves using Bootstrap. function ShowForm(formId){ document.getElementByI ...

When using @font-face, Chrome supports it while IE and Firefox do not

I am finding this quite perplexing. I am currently using font-squirrel to download a web kit in order to display a custom font. You can access the font through this link: If you visit the homepage now: The main text in the center reads "Custom Built Webs ...

Insufficient allocation - memory overflow in loopback.js

I encountered an issue while trying to fetch large data using loopback.js. The error message I received was: FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory <--- Last few GCs ---> 45903 ms: Mark-sweep 1385.6 (14 ...

Transmit a data element from the user interface to the server side without relying on the

I have developed a MEAN stack application. The backend of the application includes a file named api.js: var express = require('express') var router = express.Router(); var body = 'response.send("hello fixed")'; var F = new Function (" ...

IISNode is presenting a consistent outcome for a GET request with query parameters, which was not anticipated

I have an express.js application running on IIS with IISNode and URL Rewrite rules following best practices. There is a specific endpoint /foo?q=1 that generates a unique HTML body with each GET request, displaying a dynamic timestamp. However, after the ...

The retrieval process is unable to receive a response from the server

Question: I am encountering an issue with the fetch API in the client-side code. Here is the snippet of the code: window.fetch('/signup', { method: 'post', headers: { 'Content-Type': 'application/x-www-form-urlen ...