0

I have no idea how to draw the structure of my project here in stackoverflow, so I will try to explain what my issues are as clear as possible.

I have server.js file in my root, using express.static middleware.

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

and my style.css file is included in public/css/style.css

Also, I am using ejs view engine, and I have two view files. (ejs view engine is looking for views folder)

First one is views/main/home.ejs, and second one is views/sub/intro/location.ejs

I`m using <% include %> code in each file,

// views/main/home.ejs
<% include ../header.ejs %>

// views/sub/intro/location.ejs
<% include ../../header.ejs %>

The problem is that, in header.ejs file, do I have to link same css file in two different href path? like,

// views/header.ejs

// for views/main/home.ejs
<link rel="stylesheet" href="css/style.css" type="text/css">

// for views/sub/intro/location.ejs
<link rel="stylesheet" href="../css/style.css" type="text/css">

Is that the only way I can do?

if header.ejs has many static image files, do I have to write

img src

twice for each image?

If there is more efficient way to solve this issue, it would be really appreciated to remind me of that. thank you.

1
  • <link rel="stylesheet" href="css/style.css" type="text/css"> this is enough, this will search in public directory Commented Feb 23, 2018 at 12:20

1 Answer 1

1

You don't need to write them twice since you are referring to the same file.

It has to be like this since you have configured your express static:

//for css file
<link rel="stylesheet" href="/css/style.css" type="text/css">
//for image.  
<img src="/imagefilepath">

The point is that there should be one, not two.

Hope this helps.

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

2 Comments

thank you. by the way, what is the difference between css/style.css and /css/style.css ? the first one works well in the views/main/home.ejs but not in the views/sub/intro/location.ejs
It depends on your folder structure. css/style.css means that css is a top-level folder and not inside any folder while /css/style.css means that css is inside another folder.

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.