Despite being correctly linked in EJS and without any errors, the CSS is still not loading

Hey there, I'm facing an issue with linking a CSS file to an EJS file. I believe that I am connecting 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">

I have also set the public directory to be used in Express like this:

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!");
});

Despite no errors in the console, the CSS is still not loading.

File structure:

  • app.js
  • views
    • pubilc
      • css
        • Acres and Karats Calculator.css

Answer №1

My solution involved including the rel="stylesheet" attribute and inserting a / before the file path

Answer №2

Considering the file structure of your project:

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

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

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

To update the express static path in your app.js:

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

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

What is the best way to switch the direction of the arrows based on the sorting order?

Is there a way to dynamically change the direction of arrows based on sorting, similar to the example shown here? sortingPipe.ts: import { SprBitType } from '../spr-bit-type/sprBitType'; import { Pipe, PipeTransform } from '@angular/core& ...

Tips for preventing the need to open numerous chrome windows when running multiple URLs with Selenium WebDriverJS

Is there a way to prevent multiple instances of the browser from opening when attempting to parse multiple URLs? I would like to have just one browser open and running all the URLs within it. Any advice or suggestions would be greatly appreciated! I' ...

How should one correctly utilize npm within the root environment?

As I set up a Parse Server with an AWS EC2 instance, I utilized Nginx and Elastic IP to handle SSL certificates. Following various guides, I came across the recommendation to place the node app at /var/www/parse-server. However, I encountered an issue when ...

Tips for adjusting image dimensions with url() method in css

I have successfully incorporated collapsible functionality on my page. I would like to display a down arrow image instead of the default '+' symbol on the right side of the collapsible section. To achieve this, I can use the following CSS code s ...

Node.js middleware designed for handling unique requests

My request is for middleware to be applied specifically to certain routes: POST /tickets PUT /tickets/:id DELETE /tickets/:id etc... Currently, the middleware I have implemented runs for all HTTP requests made on /tickets and similar routes: app.use(&ap ...

The error thrown by Mongoose, stating "TypeError: Cannot read property 'catch' of undefined," is not caught after the data is saved

After updating to Mongoose version 5.0.15, I encountered an error that says TypeError: Cannot read property 'catch' of undefined when trying to save my object with errors found, even though everything was working fine on Mongoose version 4.13.11. ...

Unable to initiate the PDF download using express.js

I am currently working with express.js and pdfkit to create PDFs and prompt users to download them in a web browser. Although I have set up a static location for the PDF files, I am facing an issue where nothing happens when trying to read this location a ...

What is the optimal method for storing images on a website built with expressjs and mongodb?

I am currently diving into express, nodejs, and mongodb for a project I'm working on - creating a website for a clothing store. The client will be uploading new images every 2 days, which need to be displayed quickly as thumbnails on the website. User ...

The jQuery fade speed effect has a limitation where it can only be utilized

When a specific link is clicked, I want a div to display with a fadeIn effect and then fadeOut when the link is clicked again. However, after the first click following an HTTP request, the fadeIn works but the fadeOut does not (the div closes though). More ...

Troubleshooting a problem with a CSS dropdown menu

For the past couple of hours, I've been trying to troubleshoot the issue with the fly-out menu on the homepage of this website. When hovering over the capabilities link, the fly-out works correctly but the main background doesn't stretch to fit ...

React: Import default export as a string

Help needed with JSON data import import dataOption1 from './Option1.json' import dataOption2 from './Option2.json' async setParamsByDomain(optionUser) { await this.setState({ jsonName: "data"+ optionUser}); console.log(t ...

Update NPM automatically in Bash/GitBash only when a newer version is detected

Is there a way to automatically check for the availability of a new stable version of NPM online without manually opening a browser? If a newer version is found and it's greater than what I currently have installed globally, then I want to run the com ...

typescript module throwing error when trying to create pg-pool object

I've been working on a node project that involves using the pg-pool library. Here are the dependencies I have included: "@types/pg-pool": "0.0.3", "pg": "^7.3.0", "pg-format": "^1.0.4", "pg-pool": "^2.0.3", Within one of my files, I attempted to cre ...

Guide on using webpack to import jQuery

When using webpack, is it necessary to install the jquery file from npm, or can I simply require the downloaded file from the jquery website? directory app/ ./assets/javascripts/article/create/base.js ./assets/javascripts/lib/jquery-1.11.1.min.js webpack ...

Last item in Material UI Grid container not filling up entire space

I designed a Grid container with 2 Grid columns, each containing 3 elements. However, I am facing an issue where the last element of each column is not appearing at the bottom as intended. What could be causing this problem? For reference, you can view th ...

Mongoose virtual population allows you to fetch related fields

When attempting to utilize virtual populate between two models that I've created, the goal is to retrieve all reviews with the tour id and display them alongside the corresponding tour. This is achieved by using query findById() to specifically show o ...

Is it possible to monitor and keep a record of every modification made to an HTML element?

Can anyone suggest an effective method to monitor changes made to an HTML element? I attempted to utilize JavaScript with jQuery but was unsuccessful. $('div.formSubmitButton input[type="submit"]').change(function(event){ alert( ...

What are some ways to fix the error message "Uncaught TypeError: iterable.hasOwnProperty is not a function"?

I recently integrated the react-leaflet-markerclusters package into my project. However, upon installation and running npm start, an error has surfaced: loadMessages.js?4a62:4 Uncaught TypeError: iterable.hasOwnProperty is not a function at traverse (l ...

What are the steps to enable data encryption at rest for MongoDB Community Edition?

After thoroughly reviewing the documentation on encryption in MongoDB available at MongoDB docs, it appears that this feature is only available in MongoDB Enterprise. Is there a way to implement data-at-rest encryption in MongoDB Community Edition version ...

The Mean.js platform seems to be experiencing issues as it is unable to establish a connection

SCENARIO: I have downloaded the repository from this link: https://github.com/meanjs/mean After following and executing all instructions, I ran the command $ npm start and encountered the following error: ERROR: Could not connect to MongoDB! { Mon ...