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

I am encountering issues with the responsiveness of my layout as it is

My goal is to create a responsive layout utilizing code and examples from this website: www.responsivegridsystem.com To achieve this, I have nested a few containers to ensure a content area of 960px width centered within a 1000px container. My plan is th ...

Different ways to modify the underline and label color in Material-UI's <Select/> component

A while ago, I came across this useful demo that helped me change the colors of input fields. Here is the link: https://stackblitz.com/edit/material-ui-custom-outline-color Now, I'm on a quest to find a similar demo for customizing the color of selec ...

Maintain parent hover effect while child is being hovered over

After exploring various solutions to fix an issue, I've hit a roadblock. Adding CSS and jQuery code seemed promising at first, but upon refreshing the browser, a new problem emerged. The parent element retained its hover state background color, but th ...

What is the best way to change the background color for my photo gallery?

I'm currently working on a project to create a unique photo gallery where each image has a different colored background. I have six images in total, but right now all of them have a pink background. I've attempted adding another .popup class in t ...

Does Div have the capability for text-shadow?

Is there a way to create a shadow effect for DIVs similar to the text-shadow property? While text-shadow is great for adding shadows to individual pieces of text, I'm interested in applying a shadow effect to an entire DIV element. Does anyone have a ...

Position the second line of text to align in MUI

I am facing an issue with aligning the text on the second line. It should match up with the text on the first line. For reference, you can check out the Codesandbox by CLICKING HERE <Card sx={{ maxWidth: 200 }}> <CardMedia component="i ...

What are the steps to troubleshoot a running Express.js application in a Docker container that is not accessible?

I created an application utilizing Express.js with Prisma as its ORM. My goal is to deploy the application using Docker, and once it's exposed in the container, I aim to access it locally through docker run -p 3001:3002 klinikpintar/ttv:1.0. In the ...

Improving performance of nested collection queries in MongoDB

After retrieving all my registered items, I want to display a user rating section beneath each item on the page. The issue arises as the collection calls are asynchronous and I am unable to send an express response when the calls are completed. Currently ...

PHP dropdown menu is a dynamic feature that provides users

Can anyone help me identify the issue with this code? I am struggling to display the sub menu... I suspect the problem lies in the section where I retrieve the sub menu from the database. However, upon inspection, everything seems correct... It's po ...

No testing detected, ending with code zero - NPM and YARN

After installing node js and yarn, I attempted to run an application/script developed by someone else. However, when running the test, I encountered the following message: No tests found, exiting with code 0 Watch Usage › Press f to run only failed tes ...

What is the best way to implement a scrollbar in a specific div rather than the entire window?

Hey everyone! So, I have this window in electronJS with a div where I'm dynamically adding elements using Javascript and the function --> document.createElement('section'). Here's the loop in Javascript to add these elements: for (var ...

Is it considered poor form to develop an npm module that relies on an external application or program for its functionality

Is it advisable for me to release an npm module that depends on an external program not available on npm (like python), and installs it using a postinstall or preinstall hook? ...

What are the steps to incorporate an iframe containing uniquely designed XML content?

I'm currently working on a website project for a client who specifically wants to include news from the BBC. Despite spending 3 hours searching online, I haven't been able to successfully insert an XML file into an iframe and style it to the clie ...

Stop mongoose from inserting template schema into the database

Currently, I am in the process of developing a demonstration gaming web application using express and mongoose. Within this app, there are various game types available, including quizzes and puzzles. My objective is to establish a primary Schema named Gam ...

Performing Numerous Validations in Mysql Using ExpressJS and NodeJS

Hello, I am just starting out with NodeJS and currently working on an ExpressJS API to use with angular2. I am facing a problem where I need to perform database checks (MySQL) before inserting data and handle any errors that may occur. However, my code se ...

The @sheet/core package was nowhere to be found in the npm repository

I am facing an issue with a package dependency called '@sheet/core' in my package.json file. Despite being listed in the file, npm is unable to find this dependency in the repository. Every time I try to install it, npm throws a 404 not found err ...

Removing items from redis with the use of Node.js

I have been struggling to delete keys in redis with the code below. While the console output works perfectly, it seems that the keys are not getting deleted in redis. Could someone please assist me in figuring out what I might be missing? import { RedisCli ...

Ways to showcase my Node.js CLI module across various environments

As I develop a cli using nodejs, named xygcli, everything appears to be running smoothly. However, once it was published, I received feedback from some users indicating that they were unable to locate the command "xygcli not found (zsh)" Even though they ...

Navigating the world of updating problems with Express.js

Currently, I am working on a MEAN stack project and facing challenges with the routing for updating. Saving operations are functioning correctly. In my Angular controller, I have defined the following scope method in which staff represents the object subm ...

Encountering issue with npm twigjs while compiling template containing twig features

Recently, I created a boilerplate for utilizing twig with webpack and nodejs. If you're interested, you can access it here: My Twig boilerplate While the boilerplate works fine for basic tasks like iteration and if statements, I encountered an issue ...