0

I have ejs layout like this on my views/layout.ejs

<html>
<head>
<title>title</title>
</head>
<body>
<%- body %> 
</body>
</html>

Then, on my views/login/login.ejs I have.

<h1 class="login">Login</h1>

I'm trying to link css file with my login.ejs file, but how I do that? I've already read this, It says that we should use conditional statement in layout.ejs but I don't really understand since it's wrote in asp language.

Roby Cigar
  • 766
  • 2
  • 14
  • 28

1 Answers1

0

To conditionally add CSS, you simple use plain JavaScript within EJS tags:

<head>
   <title>title</title>
   <% if (condition) { %>
     <link rel="stylesheet" href="/path/to/main.css">
   <% } %>
</head>

/path/to/main.css is resolved by your server, not EJS. condition can be anything JavaScript can do. For example:

<% if (path === '/login') %>
Phil
  • 879
  • 2
  • 10
  • 23
  • Why a downvote? External CSS can only be imported in the HTML document meta-data (header). – Phil Jan 04 '21 at 14:51