1

I believe this is the correct way to link the css to the ejs file, however it's still not working. You can see how all my folders are arranged in the pictures provided.pic1 pic2 enter image description here

2
  • 1
    Please don't post code in images. Post code as text. There are at least a dozen good reasons for posting text (search indexing, copy/paste into answers, access by visually impaired, etc...). Commented Feb 16, 2022 at 7:16
  • I think the problem is ../public. The .. means to go one directory up so essentialy outside your app folder. If your server.js is in for example /blah/myapp/server.js then ../public points to /blah/public. So use public without the ../ and it should be ok. Commented Feb 16, 2022 at 7:16

2 Answers 2

2

your problem is not with the link tag in your Ejs file. the problem is with defining the path for your file inside the server.js file

the solution is to use static method inside the express 'use' middleware as the following:

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

this means that you are telling express where to find and serve static files.

for linking the css file, all you have to do inside your Ejs file is to add a link tag as follows:

<link href="/css/styles.css" rel="stylesheet" type="text/css">
Sign up to request clarification or add additional context in comments.

Comments

2

In your server.js file, use this line instead of the one you have:

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

The line above tells express where to find public assets. With this, evrytime you write / in a ejs file, you are already in the public folder.

You will for example link your css file like so (I assume your css styles are inside public/css/styles.css):

<link href="/css/styles.css" rel="stylesheet">

Comments

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.