How to include CSS files in EJS View Engine?
Last Updated :
28 Apr, 2025
Including CSS files is important for styling web pages in web development with Node.js, Express, and EJS. In this article, we will explore the process of including CSS in such projects to enhance the visual appeal and user experience.
Approach to include CSS file in EJS:
- We're building a Node.js web application using Express and EJS.
- Our project includes a basic webpage styled with CSS.
- We've set up routes to render EJS templates, ensuring the text content is centered within a styled container.
Steps to Include CSS files in EJS:
Step 1: let's create a new Node.js project. Open your terminal and run the following commands:
mkdir nodejs-express-ejs
cd nodejs-express-ejs
npm init -y
Step 2: Install dependencies for our project using the following command in the terminal.
- Express is a web application framework for Node.js
- EJS is a simple templating language that lets you generate HTML markup with plain JavaScript.
npm install express ejs
Step 3: After setting up the Node environment on your system, we can start by creating a server.js file and create two directories by the names of Views in which we will write our EJS fil,es and Public in which we will write the CSS files.
Project Structure:
Project StructureThe updated dependencies in the package.json file will look like this.
"dependencies": {
"ejs": "^3.1.9",
"express": "^4.18.2"
}
Example: Here, we will demonstrate how to include CSS files using Node.js, Express, and EJS
JavaScript
//server.js
const express = require('express');
const app = express();
const path = require('path');
// Set EJS as the view engine
app.set('view engine', 'ejs');
// Serve static files from the 'public' directory
app.use(express.static(path.join(__dirname, 'public')));
// Route for rendering the index.ejs template
app.get('/', (req, res) => {
res.render('index');
});
// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(
`Server is running on
http://localhost:${PORT}`
);
});
HTML
<!-- views/index.ejs -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width,
initial-scale=1.0">
<title>
Node.js Express EJS Example
</title>
<link rel="stylesheet"
type="text/css"
href="/css/style.css">
</head>
<body>
<div class="container">
<h1>
Welcome to Geeks
for Geeks
</h1>
<p>
This is a basic example of
including css files to ejs
</p>
</div>
</body>
</html>
CSS
/* public/css/styles.css */
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
margin: 0;
padding: 0;
}
.container {
max-width: 800px;
margin: 50px auto;
padding: 20px;
background-color: #222221;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
text-align: center;
display: flex;
flex-direction: column;
justify-content: center;
}
h1 {
color: #2f8d46;
}
p {
color: white;
}
Output:
Including CSS files to EJS
Similar Reads
How to Include CSS Files in Vue2 ? VueJS is one of the greatest frameworks for JavaScript similar to ReactJS. The user interface layer is designed with VueJS. When working with Vue2, styling your components is an essential aspect of creating a visually appealing and user-friendly application. In Vue2 you can include CSS files in vari
4 min read
How to include css files using NodeJS, Express, and EJS? CSS stands for âCascading Style Sheetâ. It is used to style web pages. CSS simplifies the process of making web pages presentable. It describes how web pages should look it prescribes colors, fonts, spacing, and much more. Including CSS files is important for styling web pages in web development wit
2 min read
How to Configure multiple View Engines in Express.js ? View engines present in web application framework are basically the template engines that allow us to embed dynamic content into the web pages, render them on the server, and send them to the client. With the help of these, we can serve dynamic data, and utilise the template inheritance properties t
3 min read
How to Setup Handlebars View Engine in Node.js ? Handlebars is a template engine that is widely used and easy to use. The pages contain .hbs extension and there are many other template engines in the market like EJS, Mustache, etc. Installation of hbs module: You can visit the link Install hbs module. You can install this package by using this com
2 min read
How to Setup View Engine in Node.js ? View engines are useful for rendering web pages. There are many view engines available in the market like Mustache, Handlebars, EJS, etc but the most popular among them is EJS which simply stands for Embedded JavaScript. It is a simple templating language/engine that lets its user generate HTML with
2 min read