Add css background image in node.js using express

10,219

The path to your image should be set inside url("...").

Based on your project structure, it would be:

body{
    background-image: url("a.jpg");
}

Reference

Share:
10,219

Related videos on Youtube

user5640752
Author by

user5640752

Updated on June 04, 2022

Comments

  • user5640752
    user5640752 almost 2 years

    I am experimenting with Node.js and am a complete new bee.. Haven't been able to find an answer. My directory structure is

    app --> public --> a.jpg & style.css

    I provide access to these files in express as follows:

    var express = require("express");
    var app = express();
    var PORT = 3000;
    app.use(express.static("./app/public"));
    

    The css works, but when i call the background-image property in the style sheet it is unable to find the image and apply it. I have tried the following combinations with no luck:

    body{
        background-image: "../app/public/a.jpg";
        background-image: "./app/public/a.jpg";
        background-image: "/app/public/a.jpg";
        background-image: "/public/a.jpg";
        background-image: "./public/a.jpg";
        background-image: "/a.jpg";
        background-image: "a.jpg";
    }
    

    can someone point me in the right direction.

    • Imnotapotato
      Imnotapotato almost 6 years
      Usually backgrounds need sizes or at least content that will stretch the tag with the background applied to. Do you have any content in the body tag?
    • Dhaval Jardosh
      Dhaval Jardosh almost 6 years
      Try this ./a.jpg. Will take you to public and from there to a.jpg
    • user5640752
      user5640752 almost 6 years
      thanks just tried it but still no go :-/
  • user5640752
    user5640752 almost 6 years
    wow.. lol i can't believe i missed that.. thank you so much. it works now