Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

77
Views
How to style elements in an EJS file with a style.css file

I was just experimenting here and there with EJS, because I am new to it.

Then I thought, why not style elements with a conditional flow? Basically a particular element will look purple when the condition is true and it will look red when it is not. When I style the elements in the tag itself, it works, but it doesn't work when I link a style.css file with it.

<body>
    <%if(4>3){%>
    <h1 style="color: blue;">4 is more than 3</h1>
    <% }else{%>
    <h1 style="color: red;">Something is very wrong</h1>
    <% }%>
</body>

This worked properly.

But when I tried to style it in style.css, it didn't work. What is the reason?

EJS file:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="./style.css">
    <title>To Do List</title>
</head>

<body>
    <%if(4>3){%>
        <h1 id="one">4 is more than 3</h1>
    <% }else{%>
        <h1 id="two">Something is very wrong</h1>
    <% }%>
</body>
</html>

CSS file:

#one{
    color: purple;
}
#two{
    color: red;
}

The output:

7 months ago · Juan Pablo Isaza
1 answers
Answer question

0

Are you sure you have exposed the files properly? Such as in Express.js you need to serve the static folder where your file is kept on root domain for this to work.

To check this, try going to your website and opening /style.css and see if it opens.

7 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs