0

so, I'm trying to link a css file to an ejs file but it's not working, and I think I'm linking them correctly:

<head>
<title>Acres & Karats Calculator</title>
<base href="/">
<link type="text/css" href="css/Acres and Karats Calculator.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/css?family=Titillium+Web:400,700&display=swap" rel="stylesheet">

and i specified the public directory to be used in express:

let express = require("express"),
    app     = express();

app.use("/public", express.static(__dirname + "/public"));

app.get("/", (req, res) => {
    res.render("Acres and Karats Calculator.ejs")
});

app.listen("3000", () => {
    console.log("Server started!");
});

and there are no errors in the console at all and the css is not loaded

file structure:

  • app.js
  • views
    • pubilc
      • css
        • Acres and Karats Calculator.css
4
  • i think there is a problem with your css name file Commented Feb 14, 2020 at 16:20
  • and please add your file structure too Commented Feb 14, 2020 at 16:21
  • Agreed, generally spaces aren't a good idea in file names. Try replacing spaces with underscores (_) or something similar. Acres_and_Karats_Calculator.css. Commented Feb 14, 2020 at 17:13
  • I tried replacing them with underscores but nothing happened Commented Feb 14, 2020 at 17:18

2 Answers 2

1

I solved it by adding rel="stylesheet" and adding a / before the file path

Sign up to request clarification or add additional context in comments.

Comments

0

Taking into account the directory layout of your project:

app.js
views
-|_pubilc
---|_css
-----|_Acres and Karats Calculator.css

and the link to the css file in the ejs template:

<link type="text/css" href="css/Acres and Karats Calculator.css">

express static path in your app.js should be updated as follows:

app.use(express.static(__dirname + "/views/public"));

1 Comment

That didn't work, tried restarting the server and all, but didn't work

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.