Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

249
Vistas
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:

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

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.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda